Not getting logs of Retrofit request and response in Android Studio - android

i am not getting logs of requests and response requests in android studio.
I am even using HttpLogginInterceptor in Retrofit2. But i am not able to see logs of request body and response body in Logcat.
Heres my Retrofit Client:
import java.util.concurrent.TimeUnit;
import okhttp3.OkHttpClient;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
public class RestClient {
private static RestClient restClient;
private Retrofit retrofitAuth;
private Retrofit retrofit;
/**
* only to be used for auth
*/
public static WebServices webServices() {
return getRestClient().getRetrofitInstance().create(WebServices.class);
}
public static WebServices webAuthServices() {
return getRestClient().getAuthRetrofitInstance().create(WebServices.class);
}
private static RestClient getRestClient() {
if (restClient == null) {
restClient = new RestClient();
}
return restClient;
}
private Retrofit getRetrofitInstance() {
if (retrofit == null) {
retrofit = new Retrofit.Builder()
.baseUrl(BuildConfig.API_URL)
.addConverterFactory(GsonConverterFactory.create())
.client(getOkHttpClient())
.build();
}
return retrofit;
}
private Retrofit getAuthRetrofitInstance() {
if (retrofitAuth == null) {
retrofitAuth = new Retrofit.Builder()
.baseUrl(BuildConfig.API_URL)
.client(getAuthOkHttpClient())
.addConverterFactory(GsonConverterFactory.create())
.build();
}
return retrofitAuth;
}
private OkHttpClient getOkHttpClient() {
return new OkHttpClient.Builder()
.readTimeout(60, TimeUnit.SECONDS)
.connectTimeout(60, TimeUnit.SECONDS)
.addInterceptor(new ConnectivityInterceptor())
.addInterceptor(new HeaderAdder())
.addInterceptor(new AuthInterceptor())
.addInterceptor(getHttpLoggingInterceptor())
.build();
}
private OkHttpClient getAuthOkHttpClient() {
return new OkHttpClient.Builder()
.readTimeout(60, TimeUnit.SECONDS)
.connectTimeout(60, TimeUnit.SECONDS)
.addInterceptor(new HeaderAdder())
.addInterceptor(new ConnectivityInterceptor())
.addInterceptor(getHttpLoggingInterceptor())
.build();
}
private HttpLoggingInterceptor getHttpLoggingInterceptor() {
HttpLoggingInterceptor logging = new HttpLoggingInterceptor();
logging.setLevel(HttpLoggingInterceptor.Level.BODY);
return logging;
}
public static void destroyRestClient() {
restClient = null;
}
}
As you can see the second last Function which returns HttpLoggingInterceptor,
and i give it to my okhttp client and then that client is added to retrofit instance. But still i am not able to see logs. Please help.

Please Add this Lib in Gradle file
// Retrofit-2
implementation 'com.squareup.okhttp3:logging-interceptor:3.9.1'
val interceptor = HttpLoggingInterceptor()
interceptor.level = HttpLoggingInterceptor.Level.BODY
val client = OkHttpClient.Builder()
.connectTimeout(30, TimeUnit.SECONDS)
.writeTimeout(30, TimeUnit.SECONDS)
.readTimeout(30, TimeUnit.SECONDS)
.addInterceptor(interceptor)
.build()
GlobalApp.api = Retrofit.Builder()
.baseUrl(END_URL)
.addConverterFactory(GsonConverterFactory.create())
.client(client)
.build().create(Api::class.java)

Related

How to solve failed connection to service?

I want to get to the service in specific URL .... I use retrofit to get it but it give me an error connection to the URL...while the URL is active in chrome.
in the code I write ??? instead to correct one ^__^ (this code is class APIClient)
and to be sure that I work in correct way I was built an api in mocky site with the same json file .... and it is work
so can any one help me please
public static final String BASE_URL = "http://111.222.1.12:0000/???/???/????/";
//database
public static final String BASE_URL2 = "http://www.mocky.io/v2/";
public static Boolean URL=true;
private static Retrofit retrofit = null;
public static Retrofit getClient(){
if (retrofit==null) {
if(URL)
{
retrofit = new Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.build();
}
else
{
retrofit = new Retrofit.Builder()
.baseUrl(BASE_URL2)
.addConverterFactory(GsonConverterFactory.create())
.build();
}
}
return retrofit;
}
Hello you can try using my ApiClient, I'm using rxJava so you need to redesign the APIClient to your need
public class ApiClient {
private static Retrofit retrofit = null;
private static int REQUEST_TIMEOUT = 120;
private static OkHttpClient okHttpClient;
public static Retrofit getClient(Context context) {
if (okHttpClient == null)
initOkHttp(context);
if (retrofit == null) {
retrofit = new Retrofit.Builder()
.baseUrl(API_PATH)
.client(okHttpClient)
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.addConverterFactory(GsonConverterFactory.create())
.build();
}
return retrofit;
}
private static void initOkHttp(Context context) {
ConnectionSpec spec = new ConnectionSpec.Builder(ConnectionSpec.MODERN_TLS)
.tlsVersions(TlsVersion.TLS_1_2)
.cipherSuites(
CipherSuite.TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
CipherSuite.TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256,
CipherSuite.TLS_DHE_RSA_WITH_AES_128_GCM_SHA256)
.build();
OkHttpClient.Builder httpClient = new OkHttpClient().newBuilder()
.connectTimeout(REQUEST_TIMEOUT, TimeUnit.SECONDS)
.readTimeout(REQUEST_TIMEOUT, TimeUnit.SECONDS)
.writeTimeout(REQUEST_TIMEOUT, TimeUnit.SECONDS);
HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
httpClient.addInterceptor(interceptor);
httpClient.addInterceptor(new Interceptor() {
#Override
public Response intercept(Chain chain) throws IOException {
Request original = chain.request();
Request.Builder requestBuilder = original.newBuilder()
.addHeader("Accept", "application/json")
.addHeader("Request-Type", "Android")
.addHeader("Content-Type", "application/json");
Request request = requestBuilder.build();
return chain.proceed(request);
}
});
okHttpClient = httpClient.build();
}
public static void resetApiClient() {
retrofit = null;
okHttpClient = null;
}
}
Hope this reference can help you solve your problems

How to increase upload time for retrofit library in android

I am using retrofit library to upload file to server. When file was uploaded it shows error:: timeout. How to increase upload time so that I can upload full file in server.
Here try this: In below code you will pass your custom OKHTTP client with your custom timeout
public class RetrofitClient {
private static final OkHttpClient okHttpClient = new OkHttpClient.Builder()
.connectTimeout(60, TimeUnit.SECONDS)
.writeTimeout(60, TimeUnit.SECONDS)
.readTimeout(60, TimeUnit.SECONDS)
.build();
public static Retrofit retrofit = null;
public static Retrofit getClient(String baseUrl) {
if (retrofit == null) {
retrofit = new Retrofit.Builder()
.client(okHttpClient)
.baseUrl(baseUrl)
.addConverterFactory(GsonConverterFactory.create())
.build();
}
return retrofit;
}
}
You also need this dependency:
implementation 'com.squareup.okhttp3:okhttp:3.10.0'// OKHTTP \\ Add this to your gradle file
Add the following class in your project.
public class Api{
static Gson gson = new GsonBuilder()
.setLenient()
.create();
public static Retrofit adapter = new Retrofit.Builder()
.baseUrl(BuildConfig.BASE_URL) //Set the Root URL
.addConverterFactory(GsonConverterFactory.create(gson))
.client(configureTimeouts())
.build(); //Finally building the adapter
public static OkHttpClient configureTimeouts() {
OkHttpClient okHttpClient = new OkHttpClient.Builder()
.connectTimeout(90, TimeUnit.SECONDS) // Set your timeout duration here.
.writeTimeout(90, TimeUnit.SECONDS)
.readTimeout(90, TimeUnit.SECONDS)
.build();
return okHttpClient;
}
}
In the above configureTimeouts() function, you can set the duration after which the timeout occurs according to your requirements.
Also, don't forget to put these dependencies in your app level build.gradle file
compile 'com.squareup.retrofit2:retrofit:2.4.0'
compile 'com.squareup.retrofit2:converter-gson:2.4.0'
public RestAdapter providesRestAdapter(Gson gson) {
final OkHttpClient okHttpClient = new OkHttpClient();
okHttpClient.setReadTimeout(60, TimeUnit.SECONDS);
okHttpClient.setConnectTimeout(60, TimeUnit.SECONDS);
return new RestAdapter.Builder()
.setEndpoint(BuildConfig.BASE_URL)
.setConverter(new GsonConverter(gson))
.setClient(new OkClient(okHttpClient))
.build();
}
private static OkHttpClient.Builder httpClient = new OkHttpClient.Builder().readTimeout(120, TimeUnit.SECONDS).connectTimeout(120, TimeUnit.SECONDS)
.writeTimeout(120, TimeUnit.SECONDS);
private static Retrofit retrofit;
static {
Builder addConverterFactory = new Builder().baseUrl(API_BASE_URL).addConverterFactory(GsonConverterFactory.create());
builder = addConverterFactory;
retrofit = addConverterFactory.build();
}
public static <S> S createService(Class<S> serviceClass) {
return createService(serviceClass, null, null);
}
public static <S> S createService(Class<S> serviceClass, String username, String password) {
if (TextUtils.isEmpty(username) || TextUtils.isEmpty(password)) {
return createService(serviceClass, null);
}
return createService(serviceClass, Credentials.basic(username, password));
}
public static <S> S createService(Class<S> serviceClass, String authToken) {
if (!TextUtils.isEmpty(authToken)) {
AuthenticationInterceptor interceptor = new AuthenticationInterceptor(authToken);
if (!httpClient.interceptors().contains(interceptor)) {
httpClient.addInterceptor(interceptor);
builder.client(httpClient.build());
retrofit = builder.build();
}
}
return retrofit.create(serviceClass);
}

How to set header to all of requests?

In my application I used Retrofit for get requests from server.
I want set header to all of requests, I write below codes :
public class ApiClient {
private static final String BASE_URL = "http://example.com/api/v1.0.0.1/Api/";
private static Retrofit retrofit = null;
public static Gson gson = new GsonBuilder().create();
public static Retrofit getClient() {
HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
OkHttpClient.Builder builder = new OkHttpClient.Builder();
builder.interceptors().add(interceptor);
builder.addInterceptor(new Interceptor() {
#Override
public Response intercept(Interceptor.Chain chain) throws IOException {
Request request = chain.request().newBuilder().addHeader("Device", "2").build();
return chain.proceed(request);
}
});
OkHttpClient client = builder
.connectTimeout(120, TimeUnit.SECONDS)
.writeTimeout(120, TimeUnit.SECONDS)
.readTimeout(120, TimeUnit.SECONDS)
.build();
if (retrofit == null) {
retrofit = new Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create(gson))
.client(client)
.build();
}
return retrofit;
}
}
But when run application, not set this header!
How can I it? please help me

How can I set connection timeout in Retrofit library for Android?

I have used Retrofit library for my Android application.I need to set connection timeout as 120 Sec. How can I do ?
Version:
compile 'com.squareup.retrofit2:retrofit:2.1.0'
compile 'com.squareup.retrofit2:converter-gson:2.1.0'
compile 'com.google.code.gson:gson:2.8.0'
compile 'com.squareup.retrofit2:adapter-rxjava:2.1.0'
OperatingApiClient:
public class OperatingApiClient {
public static final String BASE_URL = "http://172.16.2.39/";
private static Retrofit retrofit = null;
public static Retrofit getClient() {
if (retrofit==null) {
retrofit = new Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.build();
}
return retrofit;
}
}
TargetFileApiInterface:
public interface TargetFileApiInterface {
#GET("NNGOperating/GetTargetFileList")
Call<TargetFileApiResponse> getTargetFileList(#Query("api_key")
String apiKey);
}
TargetFileApiResponse:
public class TargetFileApiResponse {
#SerializedName("TargetFileList")
private List<TargetFile> targetfileslist;
public TargetFileApiResponse(List<TargetFile> targetfileslist) {
this.targetfileslist = targetfileslist;
}
public List<TargetFile> getTargetfileslist() {
return targetfileslist;
}
public void setTargetfileslist(List<TargetFile> targetfileslist) {
this.targetfileslist = targetfileslist;
}
}
You can set configuration for OkHttp
OkHttpClient client = new OkHttpClient.Builder()
.connectTimeout(yourTime, TimeUnit.SECONDS)
.writeTimeout(yourTime, TimeUnit.SECONDS)
.readTimeout(yourTime, TimeUnit.SECONDS)
.build();
after that, set client for retrofit
retrofit = new Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.client(client)
.build();
It is working.
public class OperatingApiClient {
public static final String BASE_URL = "http://172.16.2.39/";
private static Retrofit retrofit = null;
public static Retrofit getClient() {
if (retrofit==null) {
retrofit = new Retrofit.Builder()
.baseUrl(BASE_URL)
.client(okHttpClient)
.addConverterFactory(GsonConverterFactory.create())
.build();
}
return retrofit;
}
public static OkHttpClient okHttpClient = new OkHttpClient.Builder()
.readTimeout(120, TimeUnit.SECONDS)
.connectTimeout(120, TimeUnit.SECONDS)
.build();
}
Try the following, set httpClient for your retrofit builder.
OkHttpClient.Builder httpClient = new OkHttpClient.Builder()
.readTimeout(60, TimeUnit.SECONDS)
.connectTimeout(60, TimeUnit.SECONDS); // I am using 60 seconds you can user your own
public static Retrofit getClient() {
if (retrofit==null) {
retrofit = new Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.client(httpClient.build()) //setting your custom client
.build();
}
return retrofit;
}

Code implementation Singleton Design Pattern: Bill Pugh on a Retrofit object

I have been trying to implement the Bill Pugh Singleton Design Pattern using a private static class to return the singleton retrofit instance. SO far I have been able to come up with the following code. Can someone help me review this code and confirm if this is a proper implementation of the singleton design pattern suggested by Mr. Bill Pugh Himself or suggest me a proper way of implementing this.
public class SingletonRetrofit {
private SingletonRetrofit(){}
private static class SingletonRetrofitHelper {
private static OkHttpClient okHttpClient;
private static Retrofit retrofit;
private static Retrofit makeRetrofit() {
HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
okHttpClient = new OkHttpClient.Builder()
.addInterceptor(interceptor)
.readTimeout(20, TimeUnit.SECONDS)
.connectTimeout(15, TimeUnit.SECONDS)
.build();
retrofit = new Retrofit.Builder()
.baseUrl("http://ci.draftserver.com/hornsbyapp/webservice/")
.client(okHttpClient)
.addConverterFactory(ScalarsConverterFactory.create())
.addConverterFactory(GsonConverterFactory.create())
.build();
return retrofit;
}
}
public static Retrofit getInstance() {
return SingletonRetrofitHelper.makeRetrofit();
}
}
USE this one:
public class Apiclient {
private static final String BASE_URL ="URL";
private static Retrofit retrofit = null;
public static Retrofit getClient() {
if (retrofit == null) {
OkHttpClient.Builder httpClient2 = new OkHttpClient.Builder();
httpClient2.addNetworkInterceptor(new Interceptor() {
#Override
public Response intercept(Chain chain) throws IOException {
Request.Builder builder = chain.request().newBuilder();
builder.addHeader("site_id","1");
return chain.proceed(builder.build());
}
});
OkHttpClient client = httpClient2.build();
retrofit = new Retrofit.Builder()
.baseUrl(BASE_URL)
.client(client)
.addConverterFactory(GsonConverterFactory.create())
.build();
}
return retrofit;
}
}

Categories

Resources