title: How to handle the response time using OKHttp on Android
body: <p>I have a problem in using OKHttp library, I'm not sure if it is a problem of code, or the library works that way, but here's the thing, my webapi is FullRest using ASP.NET WEB Api and a want to consume this service, and I was able to make a POST and GET easily, the problem is, if I put a breakpoint in m API if the android doesn't receive the response in less then 4 seconds it get lost, and that is not good, because imagine if someone has a bad connection and the Android must to wait a little longer to receive the response to the user, I came from C# and theres a HttpClient Class which waits much longer to receive the response than in Android. Here's my Code :</p>
  
  <pre><code>public class PostBase {
  
  private static final MediaType JSON = MediaType.parse("application/json; charset=utf-8");
  private static final MediaType urlEnconde = MediaType.parse("application/x-www-form-urlencoded; charset=utf-8");
  //private String Url="http://192.168.137.1:3000/";
  
   OkHttpClient client = new OkHttpClient();
  
   int POST(final PostModel model) throws IOException{
       Gson gson = new Gson();
       String modelJson = gson.toJson(model);
  
       RequestBody body =  RequestBody.create(JSON,modelJson);
       Request request = new Request.Builder()
               .url(URLb.GetUrl() + "api/gone/POST")
               .post(body)
               .build();
       Response response = client.newCall(request).execute();
       return response.code();
  }
  </code></pre>
  
  <p>And my Activity :</p>
  
  <pre><code>public void MakePost(final PostModel model){
      new AsyncTask&lt;Void,Void,String&gt;(){
          @Override
          protected void onPreExecute() {
              super.onPreExecute();
  
              progressDialog = new ProgressDialog(SignUpActivity.this,R.style.Theme_MyDialog);
              progressDialog.setMessage("Sending....");
              progressDialog.setCancelable(false);
              progressDialog.setCanceledOnTouchOutside(false);
              progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
              progressDialog.show();
          }
          @Override
          protected String doInBackground(Void... params) {
              try{
                  PostBase postBase = new PostBase();
                  statusCode = postBase.POST(model);
                  if(statusCode == 201){
                      return "OK";
                  }
                  if(statusCode == 409){
                      return "Conflict";
                  }
                  else {
                      return "ERROR";
                  }
              }catch (IOException e){
                  e.printStackTrace();
                  return "ERROR";
              }
          }
          @Override
          protected void onPostExecute(String s) {
              super.onPostExecute(s);
              switch (s) {
                  case "OK":
                      progressDialog.dismiss();
                      AlertDialog.Builder builder1 = new AlertDialog.Builder(SignUpActivity.this);
                      builder1.setTitle("Gone");
                      builder1.setMessage("Fine");
                      builder1.setCancelable(true);
  
                      builder1.setPositiveButton(
                              "ok",
                              new DialogInterface.OnClickListener() {
                                  public void onClick(DialogInterface dialog, int id) {
                                      dialog.cancel();
                                  }
                              });
  
                      AlertDialog alert11 = builder1.create();
                      alert11.show();
                      //finish();
                      break;
                  case "Conflict":
                      progressDialog.dismiss();
                      new DialogMessage(SignUpActivity.this, "already exist");
                      break;
                  default:
                      progressDialog.dismiss();
                      new DialogMessage(SignUpActivity.this, "something went wrong");
                      break;
              }
          }
      }.execute();
  }
  </code></pre>
  
  <p>Thanks!</p>