【问题标题】:Android java.io.IOException: unexpected end of stream on Connection on Html POSTAndroid java.io.IOException:Html POST 上的 Connection 上的流意外结束
【发布时间】:2026-01-15 17:40:01
【问题描述】:

我的应用有问题。 我设法得到一个没有问题的响应的html“GET”,但是当我尝试“POST”它得到java.io.IOException:连接上的流意外结束 GET 和 POST 代码在同一个活动中,GET 在 onCreate() 中,而 POST 是通过按钮单击。

似乎每次它都试图获取有关响应码、响应消息、getInputStream 等的信息......

我的代码:

private class PostClass2 extends AsyncTask<String, Void, Void> {
    private final Context context;
    public PostClass2(Context c){
        this.context = c;
    }
    @Override
    protected Void doInBackground(String... params) {
    try {
        URL url = new URL("http://speedport.ip/data/InternetConnectionS1P1.json");
        HttpURLConnection connection2 = (HttpURLConnection) url.openConnection();
        String urlParameters = "t_password="+t_passwordout+"&other_name="+providerout+"&other_user="+usernameout+"&other_password="+passout+"&showpw="+showpwin+"&always_online=1&idle_time=2";
        connection2.setRequestMethod("POST");
        connection2.setRequestProperty("Expert Info (Chat/Sequence)", "/data/InternetConnectionS1P1.json");
        connection2.setRequestProperty("Request URI", "/data/InternetConnectionS1P1.json");
        connection2.setRequestProperty("Request Version", "HTTP/1.1");
        connection2.setRequestProperty("Host", "speedport.ip");
        connection2.setRequestProperty("Connection", "keep-alive");
        connection2.setRequestProperty("Accept", "application/json, text/javascript, */*");
        connection2.setRequestProperty("Origin","http://speedport.ip");
        connection2.setRequestProperty("X-Requested-With", "XMLHttpRequest");
        connection2.setRequestProperty("USER-AGENT", "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/48.0.2564.116 Safari/537.36");
        connection2.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
        connection2.setRequestProperty("Referer", "http://speedport.ip/html/assistent/internet/internetconnection_wizard_step1_part1.html");
        connection2.setRequestProperty("Accept-Encoding", "gzip, deflate");
        connection2.setRequestProperty("ACCEPT-LANGUAGE", "hu-HU,hu;q=0.8,en-US;q=0.6,en;q=0.4");
        connection2.setRequestProperty("Cookie", cookietoheader);
        connection2.setDoOutput(true);
        DataOutputStream dStream = new DataOutputStream(connection2.getOutputStream());
        dStream.writeBytes(urlParameters);
        dStream.flush();
        dStream.close();
        System.out.println("\nSending 'POST' request to URL : " + url);
        System.out.println("Params: "+urlParameters);
        final StringBuilder output = new StringBuilder("Request URL " + url);
        output.append(System.getProperty("line.separator") + "Type " + "GET");
        output.append(System.getProperty("line.separator") + "Cookie " + cookietoheader);
        BufferedReader br = new BufferedReader(new InputStreamReader(connection2.getInputStream()));
        String line = "";
        StringBuilder responseOutput = new StringBuilder();
        System.out.println("output===============" + br);
        while ((line = br.readLine()) != null) {
            responseOutput.append(line);
        }
        br.close();
        output.append(System.getProperty("line.separator") + "Response " + System.getProperty("line.separator") + System.getProperty("line.separator") + responseOutput.toString());
        outtotext2 = responseOutput.toString();
        } catch (MalformedURLException e) {
            e.printStackTrace();

        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
    protected void onPostExecute(Void ignored) {
        t.setText(textout1);
    }
}

Logcat:

04-10 19:49:24.395  21986-22074/com.dxir1p.molnardk.telekom0224 W/System.err﹕ java.io.IOException: unexpected end of stream on Connection{speedport.ip:80, proxy=DIRECT@ hostAddress=192.168.1.254 cipherSuite=none protocol=http/1.1} (recycle count=0)
04-10 19:49:24.400  21986-22074/com.dxir1p.molnardk.telekom0224 W/System.err﹕ at com.android.okhttp.internal.http.HttpConnection.readResponse(HttpConnection.java:210)
04-10 19:49:24.400  21986-22074/com.dxir1p.molnardk.telekom0224 W/System.err﹕ at com.android.okhttp.internal.http.HttpTransport.readResponseHeaders(HttpTransport.java:80)
04-10 19:49:24.400  21986-22074/com.dxir1p.molnardk.telekom0224 W/System.err﹕ at com.android.okhttp.internal.http.HttpEngine.readNetworkResponse(HttpEngine.java:904)
04-10 19:49:24.400  21986-22074/com.dxir1p.molnardk.telekom0224 W/System.err﹕ at com.android.okhttp.internal.http.HttpEngine.readResponse(HttpEngine.java:788)
04-10 19:49:24.400  21986-22074/com.dxir1p.molnardk.telekom0224 W/System.err﹕ at com.android.okhttp.internal.huc.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:443)
04-10 19:49:24.400  21986-22074/com.dxir1p.molnardk.telekom0224 W/System.err﹕ at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:388)
04-10 19:49:24.400  21986-22074/com.dxir1p.molnardk.telekom0224 W/System.err﹕ at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getResponseMessage(HttpURLConnectionImpl.java:497)
04-10 19:49:24.400  21986-22074/com.dxir1p.molnardk.telekom0224 W/System.err﹕ at com.dxir1p.molnardk.telekom0224.MainSetupActivity$PostClass2.doInBackground(MainSetupActivity.java:274)
04-10 19:49:24.400  21986-22074/com.dxir1p.molnardk.telekom0224 W/System.err﹕ at com.dxir1p.molnardk.telekom0224.MainSetupActivity$PostClass2.doInBackground(MainSetupActivity.java:233)
04-10 19:49:24.400  21986-22074/com.dxir1p.molnardk.telekom0224 W/System.err﹕ at android.os.AsyncTask$2.call(AsyncTask.java:295)
04-10 19:49:24.400  21986-22074/com.dxir1p.molnardk.telekom0224 W/System.err﹕ at java.util.concurrent.FutureTask.run(FutureTask.java:237)
04-10 19:49:24.400  21986-22074/com.dxir1p.molnardk.telekom0224 W/System.err﹕ at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:234)
04-10 19:49:24.400  21986-22074/com.dxir1p.molnardk.telekom0224 W/System.err﹕ at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
04-10 19:49:24.400  21986-22074/com.dxir1p.molnardk.telekom0224 W/System.err﹕ at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
04-10 19:49:24.400  21986-22074/com.dxir1p.molnardk.telekom0224 W/System.err﹕ at java.lang.Thread.run(Thread.java:818)
04-10 19:49:24.400  21986-22074/com.dxir1p.molnardk.telekom0224 W/System.err﹕ Caused by: java.io.EOFException: \n not found: size=0 content=...
04-10 19:49:24.400  21986-22074/com.dxir1p.molnardk.telekom0224 W/System.err﹕ at com.android.okhttp.okio.RealBufferedSource.readUtf8LineStrict(RealBufferedSource.java:200)
04-10 19:49:24.400  21986-22074/com.dxir1p.molnardk.telekom0224 W/System.err﹕ at com.android.okhttp.internal.http.HttpConnection.readResponse(HttpConnection.java:191)
04-10 19:49:24.400  21986-22074/com.dxir1p.molnardk.telekom0224 W/System.err﹕ ... 14 more

Wireshark:

POST /data/InternetConnectionS1P1.json HTTP/1.1\r\n
    [Expert Info (Chat/Sequence): POST /data/InternetConnectionS1P1.json HTTP/1.1\r\n]
    Request Method: POST
    Request URI: /data/InternetConnectionS1P1.json
    Request Version: HTTP/1.1
Host: speedport.ip\r\n
Connection: keep-alive\r\n
Content-Length: 168\r\n
Accept: application/json, text/javascript, */*\r\n
Origin: http://speedport.ip\r\n
X-Requested-With: XMLHttpRequest\r\n
User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.110 Safari/537.36\r\n
Content-Type: application/x-www-form-urlencoded\r\n
Referer: http://speedport.ip/html/assistent/internet/internetconnection_wizard_step1_part1.html\r\n
Accept-Encoding: gzip, deflate\r\n
Accept-Language: hu-HU,hu;q=0.8,en-US;q=0.6,en;q=0.4\r\n
Cookie: session_id=09F261CE13FBAAC7\r\n

【问题讨论】:

  • 在 onPostExecute() 中进行任何打印
  • 目前在我的代码中我不做任何表演?
  • 您将哪些数据作为“POST”数据发送?
  • 我想发送 url 参数。我想更改密码等
  • 我添加了这段代码,但仍然没有出现同样的错误:

标签: android json post response ioexception


【解决方案1】:

我设法让它工作,问题是,参数中需要一个加号参数,这是我到目前为止没有看到的(它来自网页的源代码,这就是为什么)

【讨论】: