2012-05-21 21 views
1

使用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,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(); 

或者我還要添加更多東西?

感謝

+0

你能發佈完整的異常堆棧跟蹤嗎? – yorkw

+0

也不例外,它只給了我一個空位圖......它在問題 – NullPointerException

+0

上解釋關閉連接的正確方法是添加此代碼? content.close(); entity.consumeContent(); – NullPointerException

回答

1

閱讀您的問題說明問題似乎是在後端側。因爲您正在添加更多的客戶端,這些客戶端彼此獨立工作,併發查詢後端,您的後端似乎無法處理多個併發請求。
雖然沒有足夠的信息來判斷。我的結論是基於數據不足。

+0

我得到的問題只有兩個或三個設備測試,我認爲問題不在後端,因爲它必須允許來自數千用戶的連接,而不僅僅是從一個或兩個 – NullPointerException

+0

我正確地關閉連接? – NullPointerException

+0

不,你不是。您需要關閉輸入流的內容。 – Siarhei

0

嘗試以下,

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請求