Я работаю над проектом студии android, и мне нужно получить файл json на сервере.У меня есть 2 разных URL.Когда я пробую один из них, он прекрасно работает.Однако, это дает ошибку во втором, и я не могу это исправить.Когда я пытался увидеть ответные сообщения, в рабочем 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]