![](/img/trans.png)
[英]Use apache httpclient 4.3.2 and httpclient 4.1.2 at the same time
[英]Problems with HttpClient when the function is used by various devices at same time
使用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 );
給我一個statusLine
圖,我不明白為什么,因為content
, entity
, statusLine
和response
不為空。
這些是位圖bm
為null時這些變量的值:
響應:
"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:
"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();
或者我必須添加更多內容?
謝謝
通過閱讀問題描述,問題似乎出在后端 。 因為您要添加更多彼此獨立工作的客戶端,這些客戶端同時查詢后端,所以后端似乎無法處理多個並發請求。
雖然,沒有足夠的信息來判斷。 我的結論是基於數據不足。
嘗試以下方法
HttpGet httpRequest = new HttpGet(URI.create(path) );
HttpClient httpclient = new DefaultHttpClient();
HttpResponse response = (HttpResponse) httpclient.execute(httpRequest);
HttpEntity entity = response.getEntity();
BufferedHttpEntity bufHttpEntity = new BufferedHttpEntity(entity);
bmp = BitmapFactory.decodeStream(bufHttpEntity.getContent());
httpRequest.abort();
注意:路徑類型是字符串。
問題在於,一旦您從HttpUrlConnection使用了InputStream,就無法后退並再次使用相同的InputStream。 因此,您必須為圖像的實際采樣創建一個新的InputStream。 否則,我們必須中止http請求 。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.