I'm trying to use setUseSynchronousMode on loopj to wait for results of http call before continuing in one case. I tried:
AsyncHttpResponseHandler responseHandler = new AsyncHttpResponseHandler() {
@Override
public void onSuccess(int statusCode, Header[] headers, byte[] response) {
Log.d("TEST", "Got results");
}
};
AsyncHttpClient client = new AsyncHttpClient();
responseHandler.setUseSynchronousMode(true);
client.get("http://www.google.com", responseHandler);
Log.d("TEST", "Don't want to get here until after getting results");
But the result is:
07-11 19:48:05.631 D/TEST﹕ Don't want to get here until after getting results
07-11 19:48:05.814 D/TEST﹕ Got results
Am I misunderstanding what setUseSynchronousMode should do?
You should have used
SyncHttpClient
instead ofAsyncHttpClient
.setUseSynchronousMode
doesn't have the desired effect forAsyncHttpClient
.To have synchronous version of
AsyncHttpClient
with an ability to cancel it, I do everything on the main thread. Previously I was running it inAsyncTask
and as soon asAsyncHttpClient.post()
was called, theAsyncTask
would finish and I was unable to keep track theAsyncHttpClient
instance.SyncHttpClient
didn't allow me to cancel the uploading so I knew I had to useAsyncHttpClient
and make appropriate changes.Following is my class to upload a file which uses
AsyncHttpClient
and allows cancellation:This is how you can run it:
To cancel the upload, just call
cancel()
like: