java httpurlconnection不能使连接保持活动状态

时间:2018-10-23 18:38:13

标签: java android android-studio http server

我正在处理一个android studio项目,我需要在服务器上获取一个json文件。 我有2个不同的网址。当我尝试其中之一时,效果很好。但是,它在第​​二个错误中给出了错误,我无法修复。当我尝试查看响应消息时,正在工作的url连接仍然有效。但是,在第二个连接是关闭的。我的功能如下:

public static String myFunction(String sessionId)
{
    String json = "";
    HttpURLConnection conn = null;
    String requestURL = String.format(getSubscribedChannelsRequestURL, sessionId);
    System.out.println("request url : " + requestURL);
    try
    {
        URL url = new URL(requestURL);
        conn = (HttpURLConnection)url.openConnection();
        if (conn != null)
        {
            conn.setReadTimeout(3 * 1000);
            if (conn.getInputStream() != null)
            {
                InputStream in = conn.getInputStream();                    
                ByteArrayOutputStream bytes = new ByteArrayOutputStream();
                byte[] buffer = new byte[1024];
                int length;
                while ((length = in.read(buffer)) != -1)
                    bytes.write(buffer, 0, length);
                json = bytes.toString("UTF-8");
            }
        }

    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        conn.disconnect();
    }

    return json;
}

错误消息如下:

W/System.err: java.net.ProtocolException: unexpected end of stream
W/System.err:     at com.android.okhttp.internal.http.Http1xStream$FixedLengthSource.read(Http1xStream.java:396)
W/System.err:     at com.android.okhttp.okio.RealBufferedSource$1.read(RealBufferedSource.java:371)       at java.io.InputStream.read(InputStream.java:101)

最后是我的回复消息:

I/System.out: 1********This is the working one*************************************************************************************************************************
I/System.out: Key : null ,Value : [HTTP/1.1 200 OK]
I/System.out: Key : Connection ,Value : [Keep-Alive]
Key : Content-Type ,Value : [text/html; charset=UTF-8]
Key : Date ,Value : [Tue, 23 Oct 2018 18:06:09 GMT]
Key : Keep-Alive ,Value : [timeout=5, max=100]
Key : Server ,Value : [Apache/2.4.6 (CentOS) OpenSSL/1.0.2k-fips PHP/5.4.16]
I/System.out: Key : Transfer-Encoding ,Value : [chunked]
Key : X-Android-Received-Millis ,Value : [1540317969304]
I/System.out: Key : X-Android-Response-Source ,Value : [NETWORK 200]
I/System.out: Key : X-Android-Selected-Protocol ,Value : [http/1.1]
Key : X-Android-Sent-Millis ,Value : [1540317969293]
I/System.out: Key : X-Powered-By ,Value : [PHP/5.4.16]
2*********************************************************************************************************************************

I/System.out: *******This is the broken one**************************************************************************************************************************
I/System.out: Key : null ,Value : [HTTP/1.1 200 OK]
Key : Access-Control-Allow-Origin ,Value : [*]
Key : Cache-Control ,Value : [no-store, no-cache, must-revalidate, post-check=0, pre-check=0]
Key : Connection ,Value : [close]
Key : Content-Length ,Value : [3023]
I/System.out: Key : Content-Type ,Value : [text/html; charset=UTF-8]
Key : Date ,Value : [Tue, 23 Oct 2018 17:59:30 GMT]
Key : Expires ,Value : [Thu, 19 Nov 1981 08:52:00 GMT]
Key : Pragma ,Value : [no-cache]
Key : Server ,Value : [Apache/2.2.15 (CentOS)]
Key : Set-Cookie ,Value : [PHPSESSID=hsn83haevoktg06uqll7enn2v4; path=/]
Key : X-Android-Received-Millis ,Value : [1540317570749]
Key : X-Android-Response-Source ,Value : [NETWORK 200]
I/System.out: Key : X-Android-Selected-Protocol ,Value : [http/1.1]
Key : X-Android-Sent-Millis ,Value : [1540317570728]
Key : X-Powered-By ,Value : [PHP/5.3.3]

0 个答案:

没有答案