1

I need to send multipart post request from AsyncTask using loopj, so I'm forced to use SyncHttpClient.

Reading the source code comments, I'm learning that a Request Handle returned from the post method of synchronous request cannot be used to cancel the request because it is already complete by the time this returns (which makes sense). Looks like a catch 22 though.

Is there another way to cancel a currently running Synchronous request? Is it possible to use SyncHttpClient.cancelAllRequests(true); from the UI thread? Well, SyncHttpClient.cancelAllRequests(true) from the UI thread does not work. Is it a bug?

4

1 回答 1

0

如果您希望能够取消正在运行的请求,请确保您可以使用 AsyncHttpRequest。无论您是从另一个线程甚至是 AsyncTask 运行异步 http 请求,您只需要确保在主线程上调用 AsyncHttpRequest 即可。您可以通过使用将其强制进入主线程轻松地做到这一点

Handler mainHandler = new Handler(Looper.getMainLooper());
Runnable myRunnable = new Runnable() {
  public void run() {
// run your AsyncHttpRequest in here. 
// Since it is called from the main thread it will run asynchronously
// and therefore it can be cancelled easily by calling client.cancelAllRequests(true) 
  }
};
mainHandler.post(myRunnable);
于 2014-11-29T10:55:18.817 回答