在android上发起异步http请求的最简单方法

问:

I've seen any number of examples and they all seem to solve this problem differently. Basically I just want the simplest way to make the request that won't lock the main thread and is cancelable.

It also doesn't help that we have (at least) 2 HTTP libraries to choose from, java.net.* (such as HttpURLConnection) and org.apache.http.*.

Is there any consensus on what the best practice is?

回答:

The Android 1.5 SDK introduced a new class, AsyncTask designed to make running tasks on a background thread and communicating a result to the UI thread a little simpler. An example given in theAndroid Developers Blog gives the basic idea on how to use it:

public void onClick(View v) {
   new DownloadImageTask().execute("http://example.com/image.png");
}

private class DownloadImageTask extends AsyncTask {
   protected Bitmap doInBackground(String... urls) {
      return loadImageFromNetwork(urls[0]);
   }

   protected void onPostExecute(Bitmap result) {
      mImageView.setImageBitmap(result);
   }
}

The doInBackgroundThread method is called on a separate thread (managed by a thread pooledExecutorService) and the result is communicated to the onPostExecute method which is run on the UI thread. You can call cancel(boolean mayInterruptIfRunning) on your AsyncTasksubclass to cancel a running task.

As for using the java.net or org.apache.http libraries for network access, it's up to you. I've found the java.net libraries to be quiet pleasant to use when simply trying to issue a GET and read the result. The org.apache.http libraries will allow you to do almost anything you want with HTTP, but they can be a little more difficult to use and I found them not to perform as well (on Android) for simple GET requests.


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在C++中,可以使用libcurl库来发起异步请求。libcurl提供了一个易于使用的多接口功能,可以同时处理多个传输。其中,使用curl_multi_add_handle()函数向curl_multi句柄添加一个新的curl_easy_handle,从而实现异步请求。 下面是一个简单的示例代码,用于发起异步请求: ``` #include <curl/curl.h> int main() { CURL *easy_handle = curl_easy_init(); CURLM *multi_handle = curl_multi_init(); int still_running = 0; curl_easy_setopt(easy_handle, CURLOPT_URL, "https://www.example.com/"); curl_easy_setopt(easy_handle, CURLOPT_FOLLOWLOCATION, 1L); curl_multi_add_handle(multi_handle, easy_handle); curl_multi_perform(multi_handle, &still_running); while (still_running) { struct timeval timeout; int rc; fd_set fdread; fd_set fdwrite; fd_set fdexcep; int maxfd = -1; long curl_timeo = -1; curl_multi_timeout(multi_handle, &curl_timeo); if(curl_timeo >= 0) { timeout.tv_sec = curl_timeo / 1000; timeout.tv_usec = (curl_timeo % 1000) * 1000; } else { timeout.tv_sec = 1; timeout.tv_usec = 0; } FD_ZERO(&fdread); FD_ZERO(&fdwrite); FD_ZERO(&fdexcep); curl_multi_fdset(multi_handle, &fdread, &fdwrite, &fdexcep, &maxfd); rc = select(maxfd+1, &fdread, &fdwrite, &fdexcep, &timeout); switch(rc) { case -1: break; case 0: default: curl_multi_perform(multi_handle, &still_running); break; } } curl_multi_remove_handle(multi_handle, easy_handle); curl_easy_cleanup(easy_handle); curl_multi_cleanup(multi_handle); return 0; } ``` 在上述代码中,首先初始化了一个curl_easy_handle对象,并设置了请求的URL和是否跟随重定向。然后,使用curl_multi_add_handle()函数将curl_easy_handle添加到curl_multi句柄中,并调用curl_multi_perform()函数来发起异步请求。 之后,进入while循环中,使用select()函数来监听文件描述符的变化。当文件描述符有变化时,调用curl_multi_perform()函数来处理请求。当所有请求都完成时,退出循环。 最后,使用curl_multi_remove_handle()函数从curl_multi句柄中移除curl_easy_handle,并分别调用curl_easy_cleanup()和curl_multi_cleanup()函数来释放资源。 相关问题: 1. libcurl库是什么? 2. 如何使用libcurl库发送同步请求? 3. 如何设置请求头和请求体? 4. 如何处理响应数据?

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值