How to use loopJ SyncHttpClient for synchronous calls?

I was stuck on this for two days, finally decided to post here.

I see that the loopj library is used for Async Calls and has many examples and explanations.

But since I cannot use asynchronous calls in IntentSerive on Android, I have to use SyncHttpClient, but it does not work, because when using SyncHttpClient, only the onFailure callback is called.

There are no examples of using SyncHttpClient in the documentation either.

This issue is also discussed here .

So can someone give the right way to do this?

+3
source share
2

, async http client, , ResponseHandlerInterface, . , http- , onSuccess/onFailure. , onStart, .

:

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.
+6

SyncHttpClient, . , AsyncHttpClient , SyncHttpClient - . .

:

public class AsyncUploader {
    private String mTitle;
    private String mPath;
    private Callback mCallback;

    public void AsyncUploader(String title, String filePath, MyCallback callback) {
        mTitle = title;
        mPath = filePath;
        mCallback = callback;
    }

    public void startTransfer() {
        mClient = new AsyncHttpClient();
        RequestParams params = new RequestParams();
        File file = new File(mPath);
        try {
            params.put("title", mTitle);
            params.put("video", file);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        mClient.setTimeout(50000);
        mClient.post(mContext, mUrl, params, new ResponseHandlerInterface() {
            @Override
            public void sendResponseMessage(HttpResponse response) throws IOException {
                HttpEntity entity = response.getEntity();
                if (entity != null) {
                    InputStream instream = entity.getContent();
                    // TODO convert instream to JSONObject and do whatever you need to
                    mCallback.uploadComplete();
                }
            }
            @Override
            public void sendProgressMessage(int bytesWritten, int bytesTotal) {
                mCallback.progressUpdate(bytesWritten, bytesTotal);
            }
            @Override
            public void sendFailureMessage(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {
                mCallback.failedWithError(error.getMessage());
            }
        });
    }

    /**
    * Cancel upload by calling this method
    */
    public void cancel() {
        mClient.cancelAllRequests(true);
    }
}

:

AsyncUploader uploader = new AsyncUploader(myTitle, myFilePath, myCallback);
uploader.startTransfer();
/* Transfer started */
/* Upon completion, myCallback.uploadComplete() will be called */

, cancel(), :

uploader.cancel();
0

Source: https://habr.com/ru/post/1627620/


All Articles