title: Android Okhttp asynchronous calls
body: <p>I am attempting to use the Okhttp library to connect my android app to my server via API.</p>
  <p>My API call is happening on a button click and I am receiving the following <strong>android.os.NetworkOnMainThreadException</strong>. I understand that this is due the fact I am attempting network calls on the main thread but I am also struggling to find a clean solution on Android as to how make this code use another thread (async calls).</p>
  <pre><code>@Override
  public void onClick(View v) {
      switch (v.getId()){
          //if login button is clicked
          case R.id.btLogin:
              try {
                  String getResponse = doGetRequest(&quot;http://myurl/api/&quot;);
              } catch (IOException e) {
                  e.printStackTrace();
              }
              break;
      }
  }
  
  String doGetRequest(String url) throws IOException{
      Request request = new Request.Builder()
              .url(url)
              .build();
  
      Response response = client.newCall(request).execute();
      return response.body().string();
  
  }
  </code></pre>
  <p>Above is my code, and the exception is being thrown on the line</p>
  <pre><code>Response response = client.newCall(request).execute();
  </code></pre>
  <p>I've also read that Okhhtp supports Async requests but I really can't find a clean solution for Android as most seem to use a new class that uses <strong>AsyncTask&lt;&gt;</strong>?</p>