how to set timeout at http post using android-async-http:1.4.7?

1.4k views Asked by At

I am trying to set a timeout at http post using android-async-http:1.4.7 to manage the server down but setting httpClient.setTimeout(1000) it doesn't work and onFailure callback doesn't catch an exception. here my code

AsyncHttpClient httpClient = new AsyncHttpClient();
    RequestParams requestParams = new RequestParams();
    String s= sharedPrefs.getString("type_nn", "VGG");
    requestParams.put("network", s);

    if (destination== null) {
        destination = new File(path);
    }
    requestParams.put("file", destination);
    httpClient.setTimeout(1000);

    httpClient.post("http://url", requestParams, new JsonHttpResponseHandler() {


            @Override
            public void onFailure(int statusCode, Header[] headers, java.lang.Throwable throwable, org.json.JSONArray response) {
                Toast.makeText(activity, "Error", Toast.LENGTH_SHORT).show();
                progressChargePhoto.setVisibility(View.GONE);


            }

            @Override
            public void onSuccess(int statusCode, Header[] headers, org.json.JSONArray response) {
                ...
            }
         }
1

There are 1 answers

1
isma3l On BEST ANSWER

Try adding this override:

@Override
public void onFailure(int statusCode, Header[] headers, java.lang.Throwable throwable, org.json.JSONObject response) {
    Toast.makeText(getBaseContext(), "Error", Toast.LENGTH_SHORT).show();
    ....

}

There is another one you can override but i don't think you need it, but just in case:

@Override
public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
            super.onFailure(statusCode, headers, responseString, throwable);
            ....
}

And be careful with your on success, maybe you need to add the one with the JSONObject in the signature.