android httpclient hangs on second request to the server (connection timed out)

前端 未结 7 2064
后悔当初
后悔当初 2020-11-29 06:16

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 reques

相关标签:
7条回答
  • 2020-11-29 06:51

    Since many of these answers are old and depend upon the now depricated consumeContent() method, I thought I'd answer with an alternative to the problem of Timeout waiting for connection from pool.

        HttpEntity someEntity =  response.getEntity();
    
        InputStream stream = someEntity.getContent();
        BufferedReader rd = new BufferedReader(new InputStreamReader(stream));
    
        StringBuffer result = new StringBuffer();
        String line = "";
        while ((line = rd.readLine()) != null) {
            result.append(line);
        }
        // On certain android OS levels / certain hardware, this is not enough.
        stream.close(); // This line is what is recommended in the documentation
    

    Here is what it shows in the documentation:

    cz.msebera.android.httpclient.HttpEntity
    @java.lang.Deprecated 
    public abstract void consumeContent()
                                throws java.io.IOException
    This method is deprecated since version 4.1. Please use standard java
    convention to ensure resource deallocation by calling
    InputStream.close() on the input stream returned by getContent()
    
    0 讨论(0)
提交回复
热议问题