简体   繁体   English

如何将HttpResponse提取到单独的线程中?

[英]How to extract HttpResponse into a separate thread?

I have in Android: 我在Android中:

HttpResponse httpGetResponse = client.execute(request);
...     
Log.e("HEADER: ",  httpGetResponse.getStatusLine().toString() );

ie I execute a HttpGet request and then do something with the response. 即我execute一个HttpGet请求,然后对响应做一些事情。 Yet I want to somehow place it into a separate thread, so that the UI thread won't freeze while the server responds. 但是我想以某种方式将其放入一个单独的线程中,以便在服务器响应时不会冻结UI线程。 What do you think would be the best way to do that? 您认为哪种方法最好呢?

Thansk! 谢谢!

Wrap your request into an AsyncTask and do the actual request in the doInBackground. 将您的请求包装到AsyncTask中,然后在doInBackground中执行实际的请求。 As you probably have to use the output on the UI thread you probably should return the data you need from within the doInBackground and use the postExecute option in AsyncTask. 由于您可能必须在UI线程上使用输出,因此您可能应该从doInBackground内部返回所需的数据,并使用AsyncTask中的postExecute选项。

最简单的方法是使用AsyncTask。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM