tornado的AsyncHTTPClient和python安装requests库库为什么不关闭连接

tornado的AsyncHTTPClient和requests库为什么不关闭连接? - 知乎37被浏览1167分享邀请回答01 条评论分享收藏感谢收起2153人阅读
android(18)
转载说明出处:
之所以使用asynchttpclient呢,我个人认为
一:是因为它很小100K左右。
二:它非常简单,使用非常容易上手。
在介绍使用之前先来看看本框架的特性吧。
Async的网址
主要说的就是呢:
替代了android自带提供的httpclient,支持Api23及以上,异步请求,请求是在UIThread之外,请求是使用线程池去请求的,可以上传和下载文件,保存cookies等。
引入库,在android studio里添加
dependencies {
compile 'com.loopj.android:android-async-http:1.4.9'
每个应用必用的,那就是请求了,再更具需求也可能会用到上传和下载文件。所以今天我学习的这几个。
1:请求:1)Get:
AsyncHttpClient asyncHttpClient = new SyncHttpClient();
asyncHttpClient.get(BASE_URL, AsyncHttpClientRespenseHandler);
这样就完成了Get请求,BASE_URL是地址,AsyncHttpClientRespenseHandler是请求回调。
new AsyncHttpClientRespenseHandler() {
public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {
asyncHttpClient.post(BASE_URL, AsyncHttpClientRespenseHandler)
asyncHttpClient.post(BASE_URL, RequestParams, AsyncHttpClientRespenseHandler)
RequestParams是我们写进去的参数。
RequestParams的用法有必要先说一下:
RequestParams requestParams=new RequestParams();
requestParams.put("key", what);
这里是以键值对的方式存进去的。
好了,现在写出了带参数和不带参数的post,get也可以有带参数的
获取一般的数据就这样,有没有觉得很简单呢?当然在应用里,自己用这样是不够的,我们需要自己封装一下。
3.自我封装
咱们先封装一个请求类(可以用来放所有请求方法):
import android.content.C
import android.os.L
import com.loopj.android.http.AsyncHttpC
import com.loopj.android.http.BinaryHttpResponseH
import com.loopj.android.http.FileAsyncHttpResponseH
import com.loopj.android.http.RequestP
import com.loopj.android.http.SyncHttpC
public class nHttpClient {
private static String BASE_URL = "";
private static AsyncHttpClient asyncHttpClient = new SyncHttpClient();
private static AsyncHttpClient syncHttpClient = new AsyncHttpClient();
* post请求带参数
* myHttpClientRespenseHandler
private static void post(RequestParams requestParams, MyHttpClientRespenseHandler myHttpClientRespenseHandler) {
getClient().setTimeout(20000);
getClient().post(BASE_URL, requestParams, myHttpClientRespenseHandler);
* post请求不带参数
* myHttpClientRespenseHandler
private static void post(MyHttpClientRespenseHandler myHttpClientRespenseHandler) {
syncHttpClient.setTimeout(20000);
syncHttpClient.post(BASE_URL, myHttpClientRespenseHandler);
* myHttpClientRespenseHandler
private static void get( MyHttpClientRespenseHandler myHttpClientRespenseHandler) {
getClient().setTimeout(20000);
getClient().get(BASE_URL, myHttpClientRespenseHandler);
private static AsyncHttpClient getClient() {
if (Looper.myLooper() == null) {
return syncHttpC
return asyncHttpC
* 取消请求(根据context)
public static void cancelRequest(Context context) {
getClient().cancelRequests(context, true);
* 获取数据
public static void getAll(String what ,MyHttpClientRespenseHandler myHttpClientRespenseHandler ) {
RequestParams requestParams=new RequestParams();
requestParams.put("key", what);
post(myHttpClientRespenseHandler);
* 下载文件方式1
public static void downloadFile(Context context, String url,
BinaryHttpResponseHandler response) {
getClient().get(context, url, response);
* 下载文件方式2
public static void downLoadFile2(Context context,String url,FileAsyncHttpResponseHandler fileAsyncHttpResponseHandler) {
getClient().get(context, url, fileAsyncHttpResponseHandler);
上传的操作,官网的描述也很清晰了,其实就是把文件封装到RequestParams里去,再进行请求。三种方式,1:接受inputStream的,第三个参数指定文件名。2:接受File的,直接put进去。3:接受byte[]类型,第三个参数制定文件名。
自己建的这个请求类的前面几个就是请求方式方法和取消请求的方法,我们请求的时候带上Activity类型的Context,这样有利于请求的取消,比如在onDestry里面我们需要取消当前页面还未完成的请求。
接下来我们要封装的就是RespenseHandler类了,先介绍一下它:
1.AsyncHttpResponseHandler
接收请求结果,一般重写onSuccess及onFailure接收请求成功或失败的消息,还有onStart,onFinish等消息
2.TextHttpResponseHandler
继承自AsyncHttpResponseHandler,只是重写了AsyncHttpResponseHandler的onSuccess和onFailure方法,将请求结果由byte数组转换为String
3.JsonHttpResponseHandler
也是继承自TextHttpResponseHandler,同样是重写onSuccess和onFailure方法,将请求结果由String转换为JSONObject或JSONArray
4.BaseJsonHttpResponseHandler
也是继承自TextHttpResponseHandler,是一个泛型类,提供了parseResponse方法,子类需要提供实现,将请求结果解析成需要的类型,子类可以灵活地使用解析方法,可以直接原始解析,使用gson等。
好了,相比大家也知道了这几个的用法了,我了解这个的时候最先了解的是AsyncHttpResponseHandler,但是在实际使用当中一般用的TextHttpResponseHandler。直接上代码:
import android.app.A
import android.util.L
import com.loopj.android.http.TextHttpResponseH
import org.json.JSONE
import org.json.JSONO
public abstract class MyHttpClientRespenseHandler extends TextHttpResponseHandler {
private String TAG = "respensehandler";
public MyHttpClientRespenseHandler(Activity context) {
this.context =
public void onSuccess(int i, cz.msebera.android.httpclient.Header[] headers, String s) {
Log.i(TAG, "请求成功");
JSONObject jsonObject = new JSONObject(s);
success(jsonObject);
} catch (JSONException e) {
e.printStackTrace();
public void onFailure(int i, cz.msebera.android.httpclient.Header[] headers, String s, Throwable throwable) {
Log.i(TAG, "请求失败");
public abstract
void success(JSONObject json);
public abstract
void faile();
这样就把RespenseHandler给封装好了,相当于是以后请求的时候用我自己这个Handler去做回调了就更好了不是吗?得到结果在这里自行转换为了json。使用的时候,我就直接得到json了。
看下使用的代码:
nHttpClient.getAll(MyApplication.Key, new MyHttpClientRespenseHandler(getActivity()) {
public void success(JSONObject json) {
Toast.makeText(getActivity(),
json.toString(), Toast.LENGTH_SHORT).show();
public void faile() {
Toast.makeText(getActivity(), "请求失败",
Toast.LENGTH_SHORT).show();
String[] allowedTypes = new String[]{".*"};
nHttpClient.downloadFile(getActivity(), "/file.png", new BinaryHttpResponseHandler(allowedTypes) {
public void onStart() {
super.onStart();
public void onSuccess(int statusCode, Header[] headers, byte[] binaryData) {
public void onProgress(long bytesWritten,
long totalSize) {
super.onProgress(bytesWritten, totalSize);
public void onFailure(int statusCode, Header[] headers, byte[] binaryData, Throwable error) {
public void onStart() {
super.onStart();
public void onProgress(long bytesWritten, long totalSize) {
super.onProgress(bytesWritten, totalSize);
public void onFailure(int statusCode, Header[] headers, Throwable throwable, File file) {
public void onSuccess(int statusCode, Header[] headers, File file) {
好了,讲到这里。
&&相关文章推荐
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:14710次
排名:千里之外
原创:27篇
<font color="#FF053
(1)(1)(1)(3)(1)(2)(1)(2)(4)(1)(4)(4)(4)(5)tornado.httpclient — 异步 HTTP 客户端 & Tornado 4.3 文档问题对人有帮助,内容完整,我也想知道答案
问题没有实际价值,缺少关键内容,没有改进余地
描述你的问题
我在torando服务端需要使用tornado.httpclient.AsyncHTTPClient通过代理请求一个http服务,但是抛出了NotImplementedError: proxy_host not supported 的错误。
请问有什么好的解决方案吗?难道tornado的异步请求httpclient不支持代理?
贴上相关代码
@asynchronous
@gen.coroutine
def get(self):
req = tornado.httpclient.HTTPRequest('http://****',
proxy_host='Proxy_IP_Address', proxy_port=8888)
response = yield AsyncHTTPClient().fetch(req)
result=response.body
self.write(str(result))
self.finish()
贴上报错信息
NotImplementedError: proxy_host not supported
贴上相关截图
已经尝试过哪些方法仍然没解决(附上相关链接)
答案对人有帮助,有参考价值
答案没帮助,是错误的答案,答非所问
proxy_host (string) – HTTP proxy hostname. To use proxies, proxy_host and proxy_ proxy_username and proxy_pass are optional. Proxies are currently only supported with curl_httpclient.
To select curl_httpclient, call AsyncHTTPClient.configure at startup:
AsyncHTTPClient.configure("tornado.curl_httpclient.CurlAsyncHTTPClient")
稍微看看文档就好了
代理只支持 curl_httpclient
答案对人有帮助,有参考价值
答案没帮助,是错误的答案,答非所问
Proxies are currently only supported with curl_httpclient.
答案对人有帮助,有参考价值
答案没帮助,是错误的答案,答非所问
httpclient.AsyncHTTPClient.configure(
"tornado.curl_httpclient.CurlAsyncHTTPClient"
这个文档有说明,只有curl_httpclient支持代理,你需要这么配置。使用的时候给fetch函数加上代理参数就行proxy_host="127.0.0.1", proxy_port=8787。
同步到新浪微博
分享到微博?
Hi,欢迎来到 SegmentFault 技术社区!⊙▽⊙ 在这里,你可以提出编程相关的疑惑,关注感兴趣的问题,对认可的回答投赞同票;大家会帮你解决编程的问题,和你探讨技术更新,为你的回答投上赞同票。
明天提醒我
关闭理由:
删除理由:
忽略理由:
推广(招聘、广告、SEO 等)方面的内容
与已有问题重复(请编辑该提问指向已有相同问题)
答非所问,不符合答题要求
宜作评论而非答案
带有人身攻击、辱骂、仇恨等违反条款的内容
无法获得确切结果的问题
非开发直接相关的问题
非技术提问的讨论型问题
其他原因(请补充说明)
我要该,理由是:
扫扫下载 App

我要回帖

更多关于 tornado 数据库 的文章

 

随机推荐