retrofit
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 …
How do I get Response body when there is an error when using Retrofit 2.0 Observables
Just check if the throwable is an instance of HttpException and then you can access the retrofit response if (e instanceof HttpException) { ResponseBody body = ((HttpException) e).response().errorBody(); … } Then you can use the converter to deserialize it (or do it yourself).
How to send a HTTP-delete with a body in retrofit?
A more simplified answer. @HTTP(method = “DELETE”, path = “/api/analysis_delete”, hasBody = true) Call<Analysis_Delete_RequestResult_Api10> analysis_delete_api10(@Field(“seq”) String seq); This will do the trick.
Retrofit API to retrieve a png image
Of course we usually use Picasso to load image, but sometimes we really need use Retrofit to load a special image (like fetch a captcha image), you need add some header for request, get some value from header of response (of course you can also use Picasso + OkHttp, but in a project you have …
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 …
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 …
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); …
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 …
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 …