简体   繁体   English

当各种设备同时使用该功能时,HttpClient出现问题

[英]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? 使用httpclient(apache凭据)连接下载各种设备的各种位图失败。...一种工作正常,为什么?

I'm closing the connection properly? 我正在正确关闭连接吗? i'm not sure about it, i'm staring on these kind of credential http connections. 我不确定,我盯着这些凭证http连接。

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: 我正在开发一个用于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;
}

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 ); 如果我尝试使用一个设备下载位图,则可以正常工作,但是如果我尝试同时下载3个或4个设备的位图,则此功能将失败,因为此行bm=BitmapFactory.decodeStream(content ); is giving me aa null bitmap, and i dont understand why, because content , entity , statusLine and response are not null. 给我一个statusLine图,我不明白为什么,因为contententitystatusLineresponse不为空。

These are the values for these variables when the bitmap bm is null: 这些是位图bm为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:

"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 backend-side . 通过阅读问题描述,问题似乎出在后端 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. 问题在于,一旦您从HttpUrlConnection使用了InputStream,就无法后退并再次使用相同的InputStream。 Therefore you have to create a new InputStream for the actual sampling of the image. 因此,您必须为图像的实际采样创建一个新的InputStream。 Otherwise we have to abort the http request . 否则,我们必须中止http请求

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 同时使用apache httpclient 4.3.2和httpclient 4.1.2 - Use apache httpclient 4.3.2 and httpclient 4.1.2 at the same time HttpClient同时设置SSLHostnameVerifier和ConnectionManager - HttpClient set up SSLHostnameVerifier and ConnectionManager at the same time HIbernate Group by 和 Order By 同时使用不工作? - HIbernate Group by and Order By not working when used at the same time? 在多个设备上同时运行方法-Android - Run Method On Multiple Devices at the Same Time - Android 同时使用MouseListener和KeyListener - MouseListener and KeyListener used at the same time 在服务器上为各种设备调整图像大小 - Resize images on server for various devices apache commons httpclient 4.23表单登录问题在不同请求中使用了不同的会话cookie - apache commons httpclient 4.23 form login problems different session cookies used in different requests HTTPclient和cookie的问题 - Problems with HTTPclient and cookies 三星设备更改文字大小时呈现问题 - Samsung devices rendering problems when changing text size 使功能(在活动中使用时执行相同功能的特定视图)在 Android (Java) 中通用 - Making a function (A particular view to perform same function when used in activities) universal in Android (Java)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM