简体   繁体   中英

Problems with HttpClient when the function is used by various devices at same time

Using httpclient (apache credentials) connection to download various bitmaps with various devices is failing.... with one is working OK, ¿why?

I'm closing the connection properly? i'm not sure about it, i'm staring on these kind of credential http connections.

i'm developing an app for android (java) that it is connecting to a server to download 50 bitmaps, and i'm using this function each time to download each bitmap:

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

If i try to download the bitmaps with one device, it works fine, but if i try to download the bitmaps with 3 or 4 devices at the same time, then, something fails on this function because this line bm=BitmapFactory.decodeStream(content ); is giving me aa null bitmap, and i dont understand why, because content , entity , statusLine and response are not null.

These are the values for these variables when the bitmap bm is null:

response:

"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:

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

content:

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

I'm doing something wrong? is the connection closed properly? can this be improved? Any help will be apreciated.

Thanks

EDIT:

The properly way to close the connection is to add this code?

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

Or i have to add something more?

thanks

From reading your problem description the problem seems to be on the . Because you are adding more clients that work independently from each other that query the backend concurrently your back-end seem to be unable to deal with multiple concurrent requests.
Although, there is not enough information to judge. And my conclusion is based on insufficient data.

Try the following,

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

Note: type of path is string.

The problem was that once you've used an InputStream from a HttpUrlConnection, you can't rewind and use the same InputStream again. Therefore you have to create a new InputStream for the actual sampling of the image. Otherwise we have to abort the http request .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM