Nathiel Paulino Nathiel Paulino - 1 month ago 12
Android Question

How to handle the response time using OKHttp on Android

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 :

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();
}


And my Activity :

public void MakePost(final PostModel model){
new AsyncTask<Void,Void,String>(){
@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();
}


Thanks!

Answer

Try to set bigger timeout

client.setConnectTimeout(15, TimeUnit.SECONDS); // connect timeout
client.setReadTimeout(15, TimeUnit.SECONDS);    // socket timeout