1

さまざまなデバイスでさまざまなビットマップをダウンロードするために httpclient (apache クレデンシャル) 接続を使用すると失敗します.... 1 つでも問題なく動作します。なぜですか?

接続を適切に閉じていますか?私はそれについて確信が持てません。私はこれらの種類の資格情報 http 接続を見つめています。

サーバーに接続して50個のビットマップをダウンロードするAndroid(Java)用のアプリを開発しています。毎回この関数を使用して各ビットマップをダウンロードしています:

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;
}

1 つのデバイスでビットマップをダウンロードしようとすると正常に動作しますが、同時に 3 つまたは 4 つのデバイスでビットマップをダウンロードしようとすると、この行bm=BitmapFactory.decodeStream(content );で null ビットマップが返されるため、この機能で何かが失敗します。 、 、およびがnullではないためcontent、理由がわかりません。entitystatusLineresponse

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"     (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();

または、さらに何かを追加する必要がありますか?

ありがとう

4

2 に答える 2

1

問題の説明を読むと、問題はバックエンド側にあるようです。バックエンドに同時にクエリを実行する、互いに独立して動作するクライアントを追加しているため、バックエンドは複数の同時要求を処理できないようです。
とはいえ、判断するのに十分な情報はありません。そして、私の結論は不十分なデータに基づいています。

于 2012-05-21T09:03:40.700 に答える
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リクエストを中止する必要があります。

于 2012-05-21T09:12:19.967 に答える