使用httpclient(apache憑證)連接下載各種設備的各種位圖失敗....一個工作正常,爲什麼?HttpClient在各種設備同時使用該功能時的問題
我正確關閉連接?我不確定,我正在盯着這些憑證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
,statusLine
和response
不爲空。
這些是這些變量的值時,位圖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();
或者我還要添加更多東西?
感謝
你能發佈完整的異常堆棧跟蹤嗎? – yorkw
也不例外,它只給了我一個空位圖......它在問題 – NullPointerException
上解釋關閉連接的正確方法是添加此代碼? content.close(); entity.consumeContent(); – NullPointerException