I use Retrofit 2.0.0
. This is the code that builds my HTTP client
protected OkHttpClient getHttpClient(){
OkHttpClient.Builder builder = new OkHttpClient.Builder();
builder.addInterceptor(
chain -> {
Request original = chain.request();
Request.Builder requestBuilder = original.newBuilder();
requestBuilder.header("Accept-Language", App.getInstance().getPrefs().getSelectedLanguage().toLowerCase());
requestBuilder.header("Accept-Encoding", "gzip");
if(API.this instanceof PrivateAPI){
LoginResponse loginResponse = AuthManager.getInstanse().getLoginResponse();
requestBuilder.header("Authorization", String.format("%s %s",
loginResponse.getTokenType(),
loginResponse.getAccessToken()));
}
requestBuilder.method(original.method(), original.body());
Request request = requestBuilder.build();
return chain.proceed(request);
});
if(BuildConfig.DEBUG){
HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor();
loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
builder.addInterceptor(loggingInterceptor);
}
return builder.build();
}
And here is how I use it to init my Retrofit API interface:
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create(getGson()))
.addCallAdapterFactory(RxJavaCallAdapterFactory.create())
.client(getHttpClient())
.build();
apiService = retrofit.create(PrivateAPIInterface.class);
Until yesterday I was getting full log in logcat as it supposed to be. But suddenly I begin to get empty response bodies in my log. So here is an example of log I get now:
D/OkHttp: --> GET http://MY_DOMAIN/utility/gaz?branchid=&phone=21919&customerid= HTTP/1.1
D/OkHttp: Accept-Language: hy
D/OkHttp: Accept-Encoding: gzip
D/OkHttp: Authorization: 0yXK65Go_hRPT32WIP4DAKjmzIaM0riSLlybHhusvwmYJSxTdTVBrkL5CaqopXgrOuNFL5xvR5uVwCbprWrxvfYTMJ1I_AUmd5TKdNL4ptkUrJuOy1kGiule_yveCFv0GbTiAXhYjdfynJICJEVWc54ibnIv_Q14dLpPuVle5IaRuHmza2Pavkrhzi42sfMsK0Qpxaueu8eRPysk6MP9WkTnSKYIAThuq3sHq8RGAnkaLBx
D/OkHttp: --> END GET
D/OkHttp: <-- 200 OK http://MY_DOMAIN/utility/gaz?branchid=&phone=21919&customerid= (394ms)
D/OkHttp: Content-Length: 8527
D/OkHttp: Content-Type: application/json; charset=utf-8
D/OkHttp: Server: Microsoft-IIS/7.5
D/OkHttp: X-Powered-By: ASP.NET
D/OkHttp: Date: Wed, 16 Mar 2016 12:30:37 GMT
D/OkHttp: OkHttp-Sent-Millis: 1458131438086
D/OkHttp: OkHttp-Received-Millis: 1458131438438
D/OkHttp: }
D/OkHttp: <-- END HTTP (8527-byte body)
As you can see everything I get instead of response body is D/OkHttp: }
.
But in Postman I see a normal response body and in my app everything works fine so the problem is only with logging.
Can anybody help me to understand why this can start happening?
P.S. In case of requests that has body I see the request body the problem is only with response bodys.
With retrofit-2 you are supposed to use OkHttp3 for getting full logs. Add this to your dependecies: compile 'com.squareup.okhttp3:logging-interceptor:3.0.1'
HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
OkHttpClient client = new OkHttpClient.Builder().addInterceptor(interceptor).build();
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("https://example.com")
.client(client)
.addConverterFactory(GsonConverterFactory.create())
.build();
return retrofit.create(ApiClient.class);
ResClient.class
public class RestClient {
private static ApiInterface apiInterface;
static {
setupRestClient();
}
private static void setupRestClient() {
OkHttpClient httpClient = new OkHttpClient();
RestAdapter restAdapter = new RestAdapter.Builder()
.setEndpoint(NetworkConstants.URL_BASE_LIVE)
.setLogLevel(RestAdapter.LogLevel.FULL)
.setLog(new AndroidLog("Retrofit"))
.setClient(new OkClient(httpClient))
.build();
apiInterface = restAdapter.create(ApiInterface.class);
}
public static ApiInterface getAdapter(){
return apiInterface;
}
}
ApiInterface.class
public interface ApiInterface {
// result api
//All result api async
@Headers({"Authorization: ######################"}) //for the header authorization if needed.
@GET(NetworkConstants.URL_GET_RESULTS)
void getResult(@QueryMap Map <String, String> params, Callback<ArrayList<Results>> callback);
}
Now call it like
// Fetching result through Api Hit
RestClient.getAdapter().getResult(getJobsParams(), new Callback<ArrayList<Result>>() {
@Override
public void success(ArrayList<Result> result, retrofit.client.Response response) {
// Do your Work
}
@Override
public void failure(RetrofitError error) {
// show server or other error to user
}
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With