0

I have given the dependencies in my project gradle file which are given below.

compile 'com.google.code.gson:gson:2.4'

compile 'com.squareup.okhttp3:okhttp:3.1.2'

I am getting the exception android.os.NetwokOnMainThreadException

I am not able to get how can i solve that issue because i alreday review the OKHTTP recipes form given below link. https://github.com/square/okhttp/wiki/Recipes

    public class MainActivity extends AppCompatActivity {

    private final OkHttpClient client = new OkHttpClient();
    private final Gson gson = new Gson();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        try {
            Request request = new Request.Builder()
                    .url("https://api.github.com/gists/c2a7c39532239ff261be")
                    .build();
            Response response = client.newCall(request).execute();
            if (!response.isSuccessful())
                Toast.makeText(getApplicationContext(),"false",Toast.LENGTH_LONG).show();

            Gist gist = gson.fromJson(response.body().charStream(), Gist.class);
            for (Map.Entry<String, GistFile> entry : gist.files.entrySet()) {
                Toast.makeText(getApplicationContext(),entry.getKey().toString(),Toast.LENGTH_LONG).show();
            }
        }catch (Exception e){
            Toast.makeText(getApplicationContext(),e.toString(),Toast.LENGTH_LONG).show();
        }
    }

    static class Gist {
        Map<String, GistFile> files;
    }

    static class GistFile {
        String content;
    }

}
SourabhTech
  • 1,357
  • 12
  • 17
  • 1
    use asynctask or thread to run network call, you are not supposed to run Network calls on the UI thread – Pankaj Nimgade Mar 09 '16 at 07:24
  • According to the OkHttp docs: It supports both synchronous blocking calls and async calls with callbacks. I know in Android since version 3.0 throws that exception if you try to do network calls on main thread. Using Gosn or retrofit you can perform that task. And also if i want to write the asynk task code then why i'll use okhhtp. – SourabhTech Mar 09 '16 at 07:28
  • 1
    check [this](http://stackoverflow.com/questions/6343166/how-to-fix-android-os-networkonmainthreadexception) also provide `Logcat` of errors – ELITE Mar 09 '16 at 07:31
  • @ELITE right explain write your code in AsyncTask.. –  Mar 09 '16 at 07:35
  • @ ELITE @PAnkaj Please review link which are given below . i hope after reading you will better understand what is the use of OKHTTP. http://stackoverflow.com/questions/28135338/okhttp-library-networkonmainthreadexception-on-simple-post – SourabhTech Mar 09 '16 at 07:37
  • @ELITE OkHttp has an async enqueue with a callback that is much more efficient. – SourabhTech Mar 09 '16 at 07:40
  • @SourabhTech, you got a point, I haven't used the OkHttp, Just wanted to inform you about the error you are getting – Pankaj Nimgade Mar 09 '16 at 07:43
  • 1
    Why Close vote could you explain. – SourabhTech Mar 09 '16 at 08:00
  • Because there are a thousand questions about `NetworkOnMainThreadException`, and if you used `call.enqueue()` instead of `call.execute()` you wouldn't even have this problem. – EpicPandaForce Mar 10 '16 at 11:01

1 Answers1

3

Use enqueue() instead of execute().

Execute runs it on the same thread (which in this case is the UI thread).

Enqueue runs it on a background thread.

You want to call networking operations on a background thread, and NOT on the UI thread.

See the Call interface here.

EpicPandaForce
  • 79,669
  • 27
  • 256
  • 428