Android的HttpClient的挂在第二次请求到服务器(连接超时)挂在、服务器、Android、HttpClient

2023-09-12 07:19:47 作者:凉七

我挣扎着以下问题: 我的应用程序发出请求,使用HttpClient的http服务器的序列。我用HttpPut用于将数据发送到服务器。 首先要求顺利和迅速,第二次请求挂起40秒,然后我抓住连接超时异常。我想重用我的HttpClient并通过相同的实例发送第二个请求。如果我共同创造新的HttpClient的新的ConnectionManager,然后一切工作正常。

I'm struggling with the following problem: My App makes sequence of requests to the http server using HttpClient. I use HttpPut for sending data to the server. First request goes well and fast, second request hangs for 40 sec and then I catch Connection timed out exception. I'm trying to reuse my HttpClient and send second request through the same instance. If I create new HttpClient together with new ConnectionManager, then everything works fine.

这是怎么回事?而如何解决它,不产生新的HttpClient的每一次?

Why is this happening? And how to fix it and do not create new HttpClient each time?

在此先感谢。

下面是我的code:(如果我评论readClient = newHttpClient(readClient)在doPut,那么问题就出现了。

Here is my code: (if I comment readClient = newHttpClient(readClient) in doPut, then the problem arises.

public class WebTest
{
private HttpClient readClient;
private SchemeRegistry httpreg;
private HttpParams params;

private URI url; //http://my_site.net/data/

protected HttpClient newHttpClient(HttpClient oldClient)
{
    if(oldClient != null)
        oldClient.getConnectionManager().shutdown();

    ClientConnectionManager cm = new SingleClientConnManager(params, httpreg);
    return new DefaultHttpClient(cm, params);
}

protected String doPut(String data)
{
    //****************************
    //Every time we need to send data, we do new connection
    //with new ConnectionManager and close old one
    readClient = newHttpClient(readClient);

    //*****************************


    String responseS = null;
    HttpPut put = new HttpPut(url);
    try
    {
        HttpEntity entity = new StringEntity(data, "UTF-8");
        put.setEntity(entity);
        put.setHeader("Content-Type", "application/json; charset=utf-8");
        put.setHeader("Accept", "application/json");
        put.setHeader("User-Agent", "Apache-HttpClient/WebTest");

        responseS = readClient.execute(put, responseHandler);
    }
    catch(IOException exc)
    {
        //error handling here
    }
    return responseS;
}

public WebTest()
{
    httpreg = new SchemeRegistry();
    Scheme sch = new Scheme("http", PlainSocketFactory.getSocketFactory(), 80);
    httpreg.register(sch);

    params = new BasicHttpParams();
    ConnPerRoute perRoute = new ConnPerRouteBean(10);
    ConnManagerParams.setMaxConnectionsPerRoute(params, perRoute);
    ConnManagerParams.setMaxTotalConnections(params, 50);
    ConnManagerParams.setTimeout(params, 15000);
    int timeoutConnection = 15000;
    HttpConnectionParams.setConnectionTimeout(params, timeoutConnection);
    // Set the default socket timeout (SO_TIMEOUT) 
    // in milliseconds which is the timeout for waiting for data.
    int timeoutSocket = 40000;
    HttpConnectionParams.setSoTimeout(params, timeoutSocket);
}

private ResponseHandler<String> responseHandler = new ResponseHandler<String>() 
{
    @Override
    public String handleResponse(HttpResponse response)
            throws ClientProtocolException, IOException
    {
        StatusLine statusLine = response.getStatusLine();
        if (statusLine.getStatusCode() >= 300) 
        {
            throw new HttpResponseException(statusLine.getStatusCode(),
                    statusLine.getReasonPhrase());
        }

        HttpEntity entity = response.getEntity();
        if(entity == null)
            return null;

        InputStream instream = entity.getContent();
        return this.toString(entity, instream, "UTF-8");
    }

    public String toString(
            final HttpEntity entity, 
            final InputStream instream, 
            final String defaultCharset) throws IOException, ParseException 
    {
        if (entity == null) 
        {
            throw new IllegalArgumentException("HTTP entity may not be null");
        }

        if (instream == null) 
        {
            return null;
        }
        if (entity.getContentLength() > Integer.MAX_VALUE) 
        {
            throw new IllegalArgumentException("HTTP entity too large to be buffered in memory");
        }
        int i = (int)entity.getContentLength();
        if (i < 0) 
        {
            i = 4096;
        }
        String charset = EntityUtils.getContentCharSet(entity);
        if (charset == null) 
        {
            charset = defaultCharset;
        }
        if (charset == null) 
        {
            charset = HTTP.DEFAULT_CONTENT_CHARSET;
        }

        Reader reader = new InputStreamReader(instream, charset);

        StringBuilder buffer=new StringBuilder(i);
        try 
        {
            char[] tmp = new char[1024];
            int l;
            while((l = reader.read(tmp)) != -1) 
            {
                buffer.append(tmp, 0, l);
            }
        } finally 
        {
            reader.close();
        }

        return buffer.toString();
    }
}; 

}

推荐答案

听起来很奇怪,但我有完全相同的问题。该应用程序我工作是做几个连续的请求,下载了一堆缩略图的一个ListView显示,经过第二个它会挂起,如果是在HttpClient的code死锁。

Sounds strange, but I had the exact same problem. The app I was working on was making several successive requests to download a bunch of thumbnail images to display in a ListView, and after the second one it would hang as if there was a dead lock in the HttpClient code.

但奇怪的修复,我发现是使用AndroidHttpClient而不是DefaultHttpClient。当我这样做,我也尝试过走这条路了很多东西,它开始工作得很好。不过,别忘了叫client.close()时,你的要求就大功告成了。

The strange fix that I found was to use AndroidHttpClient instead of DefaultHttpClient. As soon as I did this, and I tried a lot of stuff before going this route, it started working just fine. Just remember to call client.close() when you're done with the request.

AndroidHttpClient是文档的DefaultHttpClient以合理的默认设置和注册计划为Android的描述。由于这是在API 8级(Android 2.2的)介绍,我挖出来复制这些默认设置,这样我可以用它进一步回比API级别的来源。这是我的$ C $下复制默认值和一个辅助类的静态方法安全地关闭它

AndroidHttpClient is described in the documentation as DefaultHttpClient with "reasonable default settings and registered schemes for Android". Since this was introduced in api level 8 (Android 2.2), I dug up the source to duplicate these "default settings" so that I could use it further back than that api level. Here is my code for duplicating the defaults and a helper class with a static method for safely closing it

public class HttpClientProvider {

    // Default connection and socket timeout of 60 seconds. Tweak to taste.
    private static final int SOCKET_OPERATION_TIMEOUT = 60 * 1000;

    public static DefaultHttpClient newInstance(String userAgent)
    {
        HttpParams params = new BasicHttpParams();

        HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
        HttpProtocolParams.setContentCharset(params, HTTP.DEFAULT_CONTENT_CHARSET);
        HttpProtocolParams.setUseExpectContinue(params, true);

        HttpConnectionParams.setStaleCheckingEnabled(params, false);
        HttpConnectionParams.setConnectionTimeout(params, SOCKET_OPERATION_TIMEOUT);
        HttpConnectionParams.setSoTimeout(params, SOCKET_OPERATION_TIMEOUT);
        HttpConnectionParams.setSocketBufferSize(params, 8192);

        SchemeRegistry schReg = new SchemeRegistry();
        schReg.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
        schReg.register(new Scheme("https", SSLSocketFactory.getSocketFactory(), 443));
        ClientConnectionManager conMgr = new ThreadSafeClientConnManager(params, schReg);

        DefaultHttpClient client = new DefaultHttpClient(conMgr, params);

        return client;
    }

}

和另一个类...

public static void safeClose(HttpClient client)
{
    if(client != null && client.getConnectionManager() != null)
    {
        client.getConnectionManager().shutdown();
    }
}