【问题标题】:Problems with HttpClient when the function is used by various devices at same time多个设备同时使用该功能时HttpClient的问题
【发布时间】:2012-05-21 08:48:41
【问题描述】:

使用 httpclient(apache 凭据)连接下载各种设备的各种位图失败....其中一个工作正常,¿为什么?

我正在正确关闭连接?我不确定,我正在盯着这些凭据 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 ); 是给我一个空位图,我不明白为什么,因为contententitystatusLineresponse 不是空的。

这些是位图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();

或者我必须添加更多内容?

谢谢

【问题讨论】:

  • 你能发布完整的异常堆栈跟踪吗?
  • 没有例外,它只给了我一个空位图......它在问题上进行了解释
  • 关闭连接的正确方法是添加此代码?内容.close(); entity.consumeContent();

标签: java android httpclient httpresponse


【解决方案1】:

从阅读您的问题描述来看,问题似乎出在后端。因为您正在添加更多彼此独立工作的客户端,这些客户端同时查询后端,所以您的后端似乎无法处理多个并发请求。
虽然,没有足够的信息来判断。而我的结论是基于数据不足。

【讨论】:

  • 我只用两三个设备测试了问题,我认为问题不在后端,因为它必须允许来自数千个用户的连接,而不仅仅是来自一两个用户跨度>
  • 我正在正确关闭连接?
  • 不,你不是。您需要关闭作为输入流的内容。
  • 关闭连接的正确方法是添加此代码?内容.close(); entity.consumeContent();
  • 您可以使用content.close(),尽管如果您查看源代码,entity.consumeContent() 对 BasicHttpEntity 的作用相同(对非流式实体没有任何作用)。不要同时使用它们。
【解决方案2】:

试试下面的,

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-10-22
    • 2018-06-02
    • 2020-10-05
    • 1970-01-01
    • 1970-01-01
    • 2017-10-08
    • 2018-09-30
    相关资源
    最近更新 更多