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:
mSyncClient.post("http://example.com", params, new JsonHttpResponseHandler() {
@Override
public void onStart() {
// you can do something here before request starts
}
@Override
public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
// success logic here
}
@Override
public void onFailure(int statusCode, Header[] headers, Throwable e, JSONObject errorResponse) {
// handle failure here
}
});
// the statements after this comment line will be executed after onSuccess (or onFailure ) callback.