Getting JSON from RetrofitError object using Retrofit

You can use the getBodyAs method of the RetrofitError object. It converts the response to a Java object similarly to other Retrofit conversions. First define a class that describes your JSON error response: class RestError { @SerializedName(“code”) public int code; @SerializedName(“error”) public String errorDetails; } Then use the previously mentioned method to get the object …

Read more

javax.net.ssl.SSLHandshakeException: java.security.cert.CertPathValidatorException: Trust anchor for certification path not found

The reason this occur is the JVM/Dalvik haven’t not confidence in the CA certificates in the system or in the user certificate stores. To fix this with Retrofit, If you are used okhttp, with another client it’s very similar. You’ve to do: A). Create a cert store contain public Key of CA. To do this …

Read more

Retrofit-2 Content-Type Issue

Try this type header for Retrofit 1.9 and 2.0. For Json Content Type. @Headers({“Accept: application/json”}) @POST(“user/classes”) Call<playlist> addToPlaylist(@Body PlaylistParm parm); You can add many more headers i.e @Headers({ “Accept: application/json”, “User-Agent: Your-App-Name”, “Cache-Control: max-age=640000” }) Dynamically Add to headers: @POST(“user/classes”) Call<ResponseModel> addToPlaylist(@Header(“Content-Type”) String content_type, @Body RequestModel req); Call you method i.e mAPI.addToPlayList(“application/json”, playListParam); Or Want …

Read more

How can I debug my retrofit API call?

Use HttpLoggingInterceptor along with Retrofit. If this helps, add this inside your build.gradle – //Retrofit and OkHttp for Networking implementation ‘com.squareup.retrofit2:retrofit:2.9.0’ implementation ‘com.squareup.retrofit2:converter-gson:2.9.0’ //Logging Network Calls implementation ‘com.squareup.okhttp3:logging-interceptor:4.9.1’ Inside your APIClient class add this – public class ApiClient { private static Retrofit retrofit = null; public static Retrofit getClient(){ HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor(); interceptor.setLevel(HttpLoggingInterceptor.Level.BODY); …

Read more

Retrofit – Intercept responses globally

I was able to accomplish that by adding an interceptor to the OkHttpClient that retrofit is using. Kotlin + Retrofit 2.x val clientBuilder = OkHttpClient.Builder() clientBuilder.addInterceptor { chain -> val request = chain.request() val response = chain.proceed(request) if (response.code() == 403) { handleForbiddenResponse() } response } Retrofit 2.x: OkHttpClient.Builder clientBuilder = new OkHttpClient.Builder(); clientBuilder. addInterceptor(new …

Read more

Okhttp refresh expired token when multiple requests are sent to the server

You can do this: Add those as data members: // these two static variables serve for the pattern to refresh a token private final static ConditionVariable LOCK = new ConditionVariable(true); private static final AtomicBoolean mIsRefreshing = new AtomicBoolean(false); and then on the intercept method: @Override public Response intercept(@NonNull Chain chain) throws IOException { Request request …

Read more