Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OkHttp Library - NetworkOnMainThreadException on simple post

I want to use OkHttp library for networking in Android. I started with the simple post example as written in their website:

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);   Request request = new Request.Builder()       .url(url)       .post(body)       .build();   Response response = client.newCall(request).execute();   return response.body().string(); } 

With this call:

String response = post("http://www.roundsapp.com/post", json); 

This call ends with NetworkOnMainThreadException.
I could wrap the call with an AsyncTask, but as far as I understand from the examples, the OkHttp library should have already taken care of that.. Am I doing something wrong?

like image 699
Aviv Ben Shabat Avatar asked Jan 25 '15 10:01

Aviv Ben Shabat


People also ask

What is the underlying library of OkHttp?

Beginning with Mobile SDK 4.2, the Android REST request system uses OkHttp (v3. 2.0), an open-source external library from Square Open Source, as its underlying architecture. This library replaces the Google Volley library from past releases.

Is OkHttp asynchronous?

OkHttp doesn't currently offer asynchronous APIs to receive a response body in parts.


1 Answers

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 in the callback (OkHttp 2.x):

post("http://www.roundsapp.com/post", json, new Callback() {   @Override   public void onFailure(Request request, Throwable throwable) {      // Something went wrong   }    @Override public void onResponse(Response response) throws IOException {     if (response.isSuccessful()) {        String responseStr = response.body().string();        // Do what you want to do with the response.     } else {        // Request not successful     }   } }); 

Or OkHttp 3.x/4.x:

post("http://www.roundsapp.com/post", "", new Callback() {         @Override         public void onFailure(Call call, IOException e) {             // Something went wrong         }          @Override         public void onResponse(Call call, Response response) throws IOException {             if (response.isSuccessful()) {                 String responseStr = response.body().string();                 // Do what you want to do with the response.             } else {                 // Request not successful             }         }     }); 

Take a look at their recipes for more examples: http://square.github.io/okhttp/recipes/

like image 122
se_bastiaan Avatar answered Oct 20 '22 06:10

se_bastiaan