【问题标题】:How to use loopJ SyncHttpClient for synchronous calls?如何使用 loopJ SyncHttpClient 进行同步调用?
【发布时间】:2014-08-28 13:17:53
【问题描述】:

我已经坚持了两天了,终于决定在这里发帖。

我看到 loopj 库被用于异步调用,并且有很多示例和解释。

但是由于我无法在 Android 的 IntentServe 中使用异步调用,我不得不使用 SyncHttpClient,但它似乎不起作用,因为当我使用 SyncHttpClient 时只调用了 onFailure 回调。

文档中也没有使用 SyncHttpClient 的示例。

here 也讨论了这个问题。

那么有人可以给出正确的方法吗?

【问题讨论】:

  • 您使用了什么解决方案? SyncHttpClient 似乎无法正常工作。您使用了什么解决方案/方法?
  • 完全摆脱了 loopj 并使用了简单的 DefaultHttpClient 这是一个同步调用。
  • 不幸的是我不能这样做,因为我必须上传一个文件。我用了另一种方式。我会将其发布为答案。它对我来说很好,希望将来对其他人有所帮助。

标签: android http service loopj


【解决方案1】:

您的操作方式与异步 http 客户端相同,您提供实现 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.

【讨论】:

    【解决方案2】:

    您可以使用SyncHttpClient,但您无法取消它。我创建了一个使用AsyncHttpClient 上传文件的类,但它比SyncHttpClient 类有优势——它允许取消。我已经在other thread 中发布了代码。

    来自同一线程的代码:

    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();
    

    【讨论】:

      猜你喜欢
      • 2016-05-16
      • 1970-01-01
      • 2017-09-09
      • 2014-08-23
      • 1970-01-01
      • 1970-01-01
      • 2011-02-25
      • 2017-11-23
      • 1970-01-01
      相关资源
      最近更新 更多