Android OkHttp with Basic Authentication

Update Code for okhttp3: import okhttp3.Authenticator; import okhttp3.Credentials; import okhttp3.MediaType; import okhttp3.OkHttpClient; import okhttp3.Request; import okhttp3.Response; import okhttp3.Route; public class NetworkUtil { private final OkHttpClient.Builder client; { client = new OkHttpClient.Builder(); client.authenticator(new Authenticator() { @Override public Request authenticate(Route route, Response response) throws IOException { if (responseCount(response) >= 3) { return null; // If we’ve failed … Read more

OkHttp Post Body as JSON

Just use JSONObject.toString(); method. And have a look at OkHttp’s tutorial: public static final MediaType JSON = MediaType.parse(“application/json; charset=utf-8”); OkHttpClient client = new OkHttpClient(); String post(String url, String json) throws IOException { RequestBody body = RequestBody.create(json, JSON); // new // RequestBody body = RequestBody.create(JSON, json); // old Request request = new Request.Builder() .url(url) .post(body) .build(); … Read more

Download binary file from OKHTTP

For what it’s worth, I would recommend response.body().source() from okio (since OkHttp is already supporting it natively) in order to enjoy an easier way to manipulate a large quantity of data that can come when downloading a file. @Override public void onResponse(Call call, Response response) throws IOException { File downloadedFile = new File(context.getCacheDir(), filename); BufferedSink … Read more

How to retry HTTP requests with OkHttp/Retrofit?

For Retrofit 2.x; You can use Call.clone() method to clone request and execute it. For Retrofit 1.x; You can use Interceptors. Create a custom interceptor OkHttpClient client = new OkHttpClient(); client.setConnectTimeout(CONNECT_TIMEOUT_MILLIS, TimeUnit.MILLISECONDS); client.setReadTimeout(READ_TIMEOUT_MILLIS, TimeUnit.MILLISECONDS); client.interceptors().add(new Interceptor() { @Override public Response intercept(Chain chain) throws IOException { Request request = chain.request(); // try the request Response response … Read more

OkHttp how to log request body

Nikola’s answer did not work for me. My guess is the implementation of ByteString#toString() changed. This solution worked for me: private static String bodyToString(final Request request){ try { final Request copy = request.newBuilder().build(); final Buffer buffer = new Buffer(); copy.body().writeTo(buffer); return buffer.readUtf8(); } catch (final IOException e) { return “did not work”; } } From … 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

Okhttp3 – RequestBody.create(contentType, content) Deprecated

Java Solution: Use create(String, MediaType) instead of create(MediaType, String) for example Kotlin Solution: Use the extension function content.toRequestBody(contentType); for the File type file.asRequestBody(contentType) Note: I’m using kotlin, but my IDE just doesn’t automatically import the class or method like import okhttp3.RequestBody.Companion.toRequestBody, so I import it manually…then use it as the example given by Saeed Younus … Read more

How to use OKHTTP to make a post request?

As per the docs, OkHttp version 3 replaced FormEncodingBuilder with FormBody and FormBody.Builder(), so the old examples won’t work anymore. Form and Multipart bodies are now modeled. We’ve replaced the opaque FormEncodingBuilder with the more powerful FormBody and FormBody.Builder combo. Similarly we’ve upgraded MultipartBuilder into MultipartBody, MultipartBody.Part, and MultipartBody.Builder. So if you’re using OkHttp 3.x … Read more

Retrofit2 Authorization – Global Interceptor for access token

You have two choices — you can add it as a parameter to your call — @GET(“api/Profiles/GetProfile?id={id}”) Call<UserProfile> getUser(@Path(“id”) String id, @Header(“Authorization”) String authHeader); This can be a bit annoying because you will have to pass in the “Bearer” + token on each call. This is suitable if you don’t have very many calls that … Read more

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