I have been stuck on this for two days now, finally decided to post here.
I see loopj library being used for Async Calls and with lots of examples and explanations.
But since I cannot use async calls in IntentSerive in Android I am forced to use SyncHttpClient, but it does not seem to work as only the onFailure callback is called when I use SyncHttpClient.
There are no examples of using SyncHttpClient also in the documentation.
This issue is also discussed here.
So can someone give the right way to do it?
You do it the same way as async http client, you provide your handler that implements ResponseHandlerInterface, but now the request will be executed within the same thread. You can easily check it yourself by setting the debugger to the next statement after your sync http client call and see that debugger will hit this statement after your onSuccess / onFailure callback executed. In case of async debugger will hit this even before your onStart method, cause it is going to be executed in a separate thread.
Example:
You can use
SyncHttpClient
but you won't have the option to cancel it. I made a class which usesAsyncHttpClient
to upload file but it has an edge over theSyncHttpClient
class - it allows cancellation. I've already posted the code in other thread.Code from the same thread:
This is how you can run it:
To cancel the upload, just call
cancel()
like: