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

How to set connection timeout with OkHttp

As of OkHttp3 you can do this through the Builder like so client = new OkHttpClient.Builder() .connectTimeout(10, TimeUnit.SECONDS) .writeTimeout(10, TimeUnit.SECONDS) .readTimeout(30, TimeUnit.SECONDS) .build(); You can also view the recipe here. For older versions, you simply have to do this OkHttpClient client = new OkHttpClient(); client.setConnectTimeout(15, TimeUnit.SECONDS); // connect timeout client.setReadTimeout(15, TimeUnit.SECONDS); // socket timeout Request … 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

Automatic cookie handling with OkHttp 3

If you want to use the new OkHttp 3 CookieJar and get rid of the okhttp-urlconnection dependency you can use this PersistentCookieJar. You only need to create an instance of PersistentCookieJar and then just pass it to the OkHttp builder: CookieJar cookieJar = new PersistentCookieJar(new SetCookieCache(), new SharedPrefsCookiePersistor(context)); OkHttpClient okHttpClient = new OkHttpClient.Builder() .cookieJar(cookieJar) .build();

How to implement cookie handling on Android using OkHttp?

For OkHttp3, a simple accept-all, non-persistent CookieJar implementation can be as follows: OkHttpClient client = new OkHttpClient.Builder() .cookieJar(new CookieJar() { private final HashMap<HttpUrl, List<Cookie>> cookieStore = new HashMap<>(); @Override public void saveFromResponse(HttpUrl url, List<Cookie> cookies) { cookieStore.put(url, cookies); } @Override public List<Cookie> loadForRequest(HttpUrl url) { List<Cookie> cookies = cookieStore.get(url); return cookies != null ? cookies … Read more

Trusting all certificates with okHttp

Just in case anyone falls here, the (only) solution that worked for me is creating the OkHttpClient like explained here. Here is the code: private static OkHttpClient getUnsafeOkHttpClient() { try { // Create a trust manager that does not validate certificate chains final TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() { @Override public void … 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

javax.net.ssl.SSLException: Read error: ssl=0x9524b800: I/O error during system call, Connection reset by peer

Recently I faced the issue while working on some legacy code. After googling I found that the issue is everywhere but without any concrete resolution. I worked on various parts of the exception message and analyzed below. Analysis: SSLException: exception happened with the SSL (Secure Socket Layer), which is implemented in javax.net.ssl package of the … Read more