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

标签 java android httpclient httpresponse

使用 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 ); 给我一个空位图,我不明白为什么,因为 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();

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

谢谢

最佳答案

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

关于java - 当各种设备同时使用该功能时,HttpClient 出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10682078/

相关文章:

java - GWT:无法从我的 onmoduleload 调用 mymain.html 类中的 javascript 方法

android - 无法使用 Android Retrofit 访问 JSON 中的数据

java - 资源 <资源 ID 的编号> 类型 0x12 无效

http - 清理来自 apache HttpClient 的响应记录(没有 15 :44:59. 689 [http-nio-8080-exec-4] DEBUG org.apache.http.wire - http-outgoing-0 <<)

java - Java 中的手动对象序列化

java - 如何在android图库中显示10分钟前拍摄的图像?

android - 失败 : Build failed with exception

java主机名与证书不匹配

java - 如何使用Httpclient的GET方法访问flashplayer

java - 使用 YCrCb 颜色空间进行颜色检测?