OkHttp: avoid leaked connection warning

By upgrading to OkHttp 3.7, Eclipse started warning me of potential resource leaks. I found my problem to be in this method I wrote: public static Response getResponse(HttpUrl url, OkHttpClient client) throws IOException { Builder request = new Request.Builder().url(url); Response response = client.newCall(request.build()).execute(); if (!response.isSuccessful()) { boolean repeatRequest = handleHttpError(response); if (repeatRequest) return getResponse(url, client, … Read more

Retrofit 2 example tutorial but GsonConverterFactory display error “Cannot resolve symbol”

EDIT retrofit 2 is now stable. Use compile ‘com.squareup.retrofit2:retrofit:2.3.0’ compile ‘com.squareup.retrofit2:converter-gson:2.3.0’ in your build.gradle dependency section old answer with Retrofit 2.0 you have to declare in your build.gradle the convert factory you want to use. Add compile ‘com.squareup.retrofit:converter-gson:2.0.0-beta2’ to your gradle and sync it again

Does Retrofit make network calls on main thread?

Retrofit methods can be declared for either synchronous or asynchronous execution. A method with a return type will be executed synchronously. @GET(“/user/{id}/photo”) Photo getUserPhoto(@Path(“id”) int id); Asynchronous execution requires the last parameter of the method be a Callback. @GET(“/user/{id}/photo”) void getUserPhoto(@Path(“id”) int id, Callback<Photo> cb); On Android, callbacks will be executed on the main thread. … Read more

Retrofit Uploading multiple images to a single key

We can use MultipartBody.Part array to upload an array of images to a single key. Here is the solution WebServicesAPI @Multipart @POST(WebServices.UPLOAD_SURVEY) Call<UploadSurveyResponseModel> uploadSurvey(@Part MultipartBody.Part[] surveyImage, @Part MultipartBody.Part propertyImage, @Part(“DRA”) RequestBody dra); Here is the method for uploading the files. private void requestUploadSurvey () { File propertyImageFile = new File(surveyModel.getPropertyImagePath()); RequestBody propertyImage = RequestBody.create(MediaType.parse(“image/*”), propertyImageFile); … Read more

Handle exceptions thrown by a custom okhttp Interceptor in Kotlin Coroutines

You should subclass IOException and use that to send information from your interceptors to your calling code. We consider other exceptions like IllegalStateException to be application crashes and do not send them over thread boundaries because we don’t want to burden most callers with catching them.

Android Retrofit 2, differences between addInterceptor & addNetworkInterceptor for editing responses

The differences are in the names. NetworkInterceptor hooks in at the network level and is an ideal place to put retry logic and anything that doesn’t rely on the actual content of the response. If what you do depends on the contents of the response (like in your case), using a ApplicationInterceptor is more useful, … Read more

Retrofit and OkHttp basic authentication

Find the Solution 1.Write a Interceptor class import java.io.IOException; import okhttp3.Credentials; import okhttp3.Interceptor; import okhttp3.Request; import okhttp3.Response; public class BasicAuthInterceptor implements Interceptor { private String credentials; public BasicAuthInterceptor(String user, String password) { this.credentials = Credentials.basic(user, password); } @Override public Response intercept(Chain chain) throws IOException { Request request = chain.request(); Request authenticatedRequest = request.newBuilder() .header(“Authorization”, credentials).build(); … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)