gpt4 book ai didi

java - 当各种设备同时使用该功能时,HttpClient 出现问题

转载 作者:行者123 更新时间:2023-11-30 04:12:09 25 4
gpt4 key购买 nike

使用 httpclient(apache 凭据)连接在各种设备上下载各种位图失败....其中一个工作正常,原因是什么?

我正确地关闭了连接?我不确定,我正在盯着这些凭据 http 连接。

我正在为 android (java) 开发一个应用程序,它连接到服务器以下载 50 个位图,我每次都使用此函数来下载每个位图:

public static Bitmap getRemoteBitmap(String url) {      
Bitmap bm=null;
HttpClient httpclient = new DefaultHttpClient();
HttpResponse response;
try {
((AbstractHttpClient) httpclient).getCredentialsProvider().setCredentials(
new org.apache.http.auth.AuthScope(null,-1),
new org.apache.http.auth.UsernamePasswordCredentials(MagazineStatus._username, MagazineStatus._password));

response = httpclient.execute(new HttpGet(url));
StatusLine statusLine = response.getStatusLine();
if(statusLine.getStatusCode() == HttpStatus.SC_OK) {
try {
HttpEntity entity = response.getEntity();
InputStream content = entity.getContent();
bm=BitmapFactory.decodeStream(content );
}catch(Exception ex) {Log.e("DBF Error",ex.toString());}
}else {
response.getEntity().getContent().close();
throw new IOException(statusLine.getReasonPhrase());
}
}catch(ClientProtocolException cpe) {
Log.e("ClientProtocolException @ at FPT",cpe.toString());
} catch(Exception ex) {
Log.e("Exception at FETCHPROJECTASK",ex.toString());
}
return bm;
}

如果我尝试用一​​个设备下载位图,它工作正常,但如果我尝试同时下载 3 或 4 个设备的位图,那么,这个函数会失败,因为这一行 bm =BitmapFactory.decodeStream(content ); 给我一个空位图,我不明白为什么,因为 content, entity, statusLineresponse 不为空。

这些是位图 bm 为空时这些变量的值:

回复:

"response"   (id=830078599368)  
entity BasicManagedEntity (id=830078617768)
headergroup HeaderGroup (id=830078599408)
locale Locale (id=830078292336)
params ClientParamsStack (id=830079041944)
reasonCatalog EnglishReasonPhraseCatalog (id=830004685872)
statusline BasicStatusLine (id=830078599344)

状态行:

"statusLine"     (id=830078599344)  
protoVersion HttpVersion (id=830004713800)
reasonPhrase "OK" (id=830078599288)
statusCode 200

实体:

"entity"     (id=830078617768)  
attemptReuse false
managedConn null
wrappedEntity BasicHttpEntity (id=830078612272)

内容:

"content"    (id=830078617792)  
skipBuf null
eofWatcher BasicManagedEntity (id=830078617768)
selfClosed false

我做错了什么?连接是否正确关闭?这可以改进吗?我们将不胜感激。

谢谢

编辑:

关闭连接的正确方法是添加这段代码?

content.close();
entity.consumeContent();

或者我必须添加更多内容?

谢谢

最佳答案

从阅读您的问题描述来看,问题似乎出在后端。因为您正在添加更多彼此独立工作的客户端同时查询后端,所以您的后端似乎无法处理多个并发请求。
虽然,没有足够的信息来判断。我的结论是基于不充分的数据。

关于java - 当各种设备同时使用该功能时,HttpClient 出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10682078/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com