Search code examples
androidandroid-async-httploopj

handle timeout exception in loopj


I am having error in handling the timeout exception in asynchttpclient

@Override
public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {

    closeProgressDialog();
    Log.e("DB", String.valueOf(error));
    Log.e("DB", String.valueOf(error.getCause() instanceof ConnectTimeoutException));
}

first log gives the response cz.msebera.android.httpclient.conn.ConnectTimeoutException: Connect to /192.168.0.107:80 timed out

while second log gives null or false I want to handle the timeout exception how should I do it?


Solution

  • Your 2nd log operation is based on

    error.getCause()
    

    which is null or not a ConnectTimeoutException, while within your first log operation you are directly using the Throwable given in method parameter.

    Therefore, I would suggest to use the error and not error.getCause() to handle the ConnectTimeoutException.

    Log.e("DB", String.valueOf(error instanceof ConnectTimeoutException)); // --> Should be true now