Getting Header from Response (Retrofit / OkHttp Client)

With Retrofit 1.9.0, if you use the Callback asynchronous version of the interface, @GET(“/user”) void getUser(Callback<User> callback) Then your callback will receive a Response object Callback<User> user = new Callback<User>() { @Override public void success(User user, Response response) { } @Override public void failure(RetrofitError error) { } } Which has a method called getHeaders() Callback<User> … Read more

Retrofit 2: Catch connection timeout exception

For Retrofit 2 Define a listener in your web service instance: public interface OnConnectionTimeoutListener { void onConnectionTimeout(); } Add an interceptor to your web service: public WebServiceClient() { OkHttpClient client = new OkHttpClient(); client.setConnectTimeout(10, TimeUnit.SECONDS); client.setReadTimeout(30, TimeUnit.SECONDS); client.interceptors().add(new Interceptor() { @Override public Response intercept(Chain chain) throws IOException { return onOnIntercept(chain); } }); Retrofit retrofit = … Read more

Retrofit2: Modifying request body in OkHttp Interceptor

I using this to add post parameter to the existing ones. OkHttpClient client = new OkHttpClient.Builder() .protocols(protocols) .addInterceptor(new Interceptor() { @Override public Response intercept(Chain chain) throws IOException { Request request = chain.request(); Request.Builder requestBuilder = request.newBuilder(); RequestBody formBody = new FormEncodingBuilder() .add(“email”, “[email protected]”) .add(“tel”, “90301171XX”) .build(); String postBodyString = Utils.bodyToString(request.body()); postBodyString += ((postBodyString.length() > 0) … Read more

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

How does OkHttp get Json string?

try { OkHttpClient client = new OkHttpClient(); Request request = new Request.Builder() .url(urls[0]) .build(); Response responses = null; try { responses = client.newCall(request).execute(); } catch (IOException e) { e.printStackTrace(); } String jsonData = responses.body().string(); JSONObject Jobject = new JSONObject(jsonData); JSONArray Jarray = Jobject.getJSONArray(“employees”); for (int i = 0; i < Jarray.length(); i++) { JSONObject object … Read more

Can’t get OkHttp’s response.body.toString() to return a string

You have use .string() function to print the response in System.out.println(). But at last in Log.i() you are using .toString(). So please use .string() on response body to print and get your request’s response, like: response.body().string(); NOTE: .toString(): This returns your object in string format. .string(): This returns your response. I think this solve your … 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

How to make a reproduction of a bug for OkHttp

Make a Kotlin script in Intellij, place it outside any source folders and make sure it ends with .main.kts filename. example.main.kts #!/usr/bin/env kotlin @file:Repository(“https://repo1.maven.org/maven2/”) @file:DependsOn(“com.squareup.okhttp3:okhttp:4.9.0”) @file:CompilerOptions(“-jvm-target”, “1.8”) import okhttp3.OkHttpClient import okhttp3.Request val client = OkHttpClient() val request = Request.Builder() .url(“https://raw.github.com/square/okhttp/master/README.md”) .build() val body = client.newCall(request).execute().use { it.body!!.string() } println(body) The #! line means it will … Read more

Android Picasso library, How to add authentication headers?

Since Picasso 2.5.0 OkHttpDownloader class has been changed, assuming you are using OkHttp3 (and so picasso2-okhttp3-downloader), so you have to do something like this: OkHttpClient client = new OkHttpClient.Builder() .addInterceptor(new Interceptor() { @Override public Response intercept(Chain chain) throws IOException { Request newRequest = chain.request().newBuilder() .addHeader(“X-TOKEN”, “VAL”) .build(); return chain.proceed(newRequest); } }) .build(); Picasso picasso = … Read more