Get Http Status Code with OkHttp

You can use HttpResponse class and using that you can access the status code as follows; HttpResponse httpResponse = client.newCall(request).execute(); httpResponse.getStatusLine().getStatusCode(); If you are using com.squareup.okhttp.Response then you can use the code() method to get the HTTP status code. Response httpResponse = client.newCall(request).execute(); httpResponse.code();

Retrofit and OkHttp gzip decode

Replace this: @Headers({ “Accept-Encoding: gzip, deflate”, “Content-Type: application/json;charset=utf-8”, “Accept: application/json” }) With this: @Headers({ “Content-Type: application/json;charset=utf-8”, “Accept: application/json” }) When you provide your own Accept-Encoding header you’re instructing OkHttp that you want to do your own decompression. By omitting it, OkHttp will take care of both adding the header and the decompression.

OkHttp javax.net.ssl.SSLPeerUnverifiedException: Hostname domain.com not verified

I had the same problem, however I needed my application to work on several staging environments, all of which had self signed certs. To make matters worse, they could change those certs on the fly. To fix this, when connecting to staging only, I added a SSLSocketFactory which trusted all certs. This fixed the java … Read more

OkHttp Library – NetworkOnMainThreadException on simple post

You should use OkHttp’s async method. public static final MediaType JSON = MediaType.parse(“application/json; charset=utf-8”); OkHttpClient client = new OkHttpClient(); Call post(String url, String json, Callback callback) { RequestBody body = RequestBody.create(JSON, json); Request request = new Request.Builder() .url(url) .post(body) .build(); Call call = client.newCall(request); call.enqueue(callback); return call; } And then your response would be handled … Read more

Uploading a large file in multipart using OkHttp

Get OkHttp 2.1, and use MultipartBuilder.addFormDataPart() which takes the filename as a parameter. /** * Upload Image * * @param memberId * @param sourceImageFile * @return */ public static JSONObject uploadImage(String memberId, String sourceImageFile) { try { File sourceFile = new File(sourceImageFile); Log.d(TAG, “File…::::” + sourceFile + ” : ” + sourceFile.exists()); //Determining the media … 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”, “Jurassic@Park.com”) .add(“tel”, “90301171XX”) .build(); String postBodyString = Utils.bodyToString(request.body()); postBodyString += ((postBodyString.length() > 0) … 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

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