Android java.io.IOException:在Html POST上的Connection上意外结束流

时间:2016-04-10 18:04:31

标签: android json post response ioexception

我的应用有问题。 我设法得到一个html" GET"没有问题的回应,但当我尝试" POST"它得到java.io.IOException:Connection上意外的流结束 GET和POST代码在同一个活动中,GET在onCreate()中,POST在按钮点击。

似乎每次它都试图获取有关responecode,responsemessage,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

1 个答案:

答案 0 :(得分:0)

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