Search in sources :

Example 1 with ResponseHandlerInterface

use of com.loopj.android.http.ResponseHandlerInterface in project android-async-http by loopj.

the class AsyncBackgroundThreadSample method getResponseHandler.

@Override
public ResponseHandlerInterface getResponseHandler() {
    FutureTask<ResponseHandlerInterface> future = new FutureTask<>(new Callable<ResponseHandlerInterface>() {

        @Override
        public ResponseHandlerInterface call() throws Exception {
            Log.d(LOG_TAG, "Creating AsyncHttpResponseHandler on background thread");
            return new AsyncHttpResponseHandler(Looper.getMainLooper()) {

                @Override
                public void onStart() {
                    clearOutputs();
                }

                @Override
                public void onSuccess(int statusCode, Header[] headers, byte[] response) {
                    Log.d(LOG_TAG, String.format("onSuccess executing on main thread : %B", Looper.myLooper() == Looper.getMainLooper()));
                    debugHeaders(LOG_TAG, headers);
                    debugStatusCode(LOG_TAG, statusCode);
                    debugResponse(LOG_TAG, new String(response));
                }

                @Override
                public void onFailure(int statusCode, Header[] headers, byte[] errorResponse, Throwable e) {
                    Log.d(LOG_TAG, String.format("onFailure executing on main thread : %B", Looper.myLooper() == Looper.getMainLooper()));
                    debugHeaders(LOG_TAG, headers);
                    debugStatusCode(LOG_TAG, statusCode);
                    debugThrowable(LOG_TAG, e);
                    if (errorResponse != null) {
                        debugResponse(LOG_TAG, new String(errorResponse));
                    }
                }

                @Override
                public void onRetry(int retryNo) {
                    Toast.makeText(AsyncBackgroundThreadSample.this, String.format("Request is retried, retry no. %d", retryNo), Toast.LENGTH_SHORT).show();
                }
            };
        }
    });
    executor.execute(future);
    ResponseHandlerInterface responseHandler = null;
    try {
        responseHandler = future.get();
        Log.d(LOG_TAG, "Background thread for AsyncHttpResponseHandler has finished");
    } catch (Exception e) {
        e.printStackTrace();
    }
    return responseHandler;
}
Also used : AsyncHttpResponseHandler(com.loopj.android.http.AsyncHttpResponseHandler) FutureTask(java.util.concurrent.FutureTask) Header(cz.msebera.android.httpclient.Header) ResponseHandlerInterface(com.loopj.android.http.ResponseHandlerInterface)

Aggregations

AsyncHttpResponseHandler (com.loopj.android.http.AsyncHttpResponseHandler)1 ResponseHandlerInterface (com.loopj.android.http.ResponseHandlerInterface)1 Header (cz.msebera.android.httpclient.Header)1 FutureTask (java.util.concurrent.FutureTask)1