2014-08-28 73 views
3

我一直坚持这两天了,最后决定在这里发帖。如何使用loopJ SyncHttpClient进行同步调用?

我看到loopj库被用于异步调用和大量的例子和解释。

但是因为我不能在Android中的IntentSerive中使用异步调用,所以我不得不使用SyncHttpClient,但它似乎不起作用,因为我使用SyncHttpClient时只调用onFailure回调函数。

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

此问题也将讨论here

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

+0

什么解决方案你用过吗? 'SyncHttpClient'似乎不能像它应该那样工作。你使用什么解决方案/方法? – Sufian 2014-10-27 05:36:42

+0

完全摆脱了loopj,并使用简单的DefaultHttpClient,这是一个同步调用。 – 2014-10-27 10:25:33

+1

不幸的是我不能这样做,因为我必须上传文件。我用另一种方式。我会将其作为答案发布。它对我来说工作得很好,并且希望将来对其他人有所帮助。 – Sufian 2014-10-27 10:59:20

回答

6

您以与异步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. 
0

您可以使用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();