Android:HttpURLConnection抛出EOFException

时间:2014-05-23 11:22:36

标签: android android-asynctask httpurlconnection eofexception

我在AsyncTask中创建与服务器的连接。我正在使用HttpURLConnection。我在url中发送数据。下面是AsyncTask的doInBackground()方法的代码。我尝试重试将数据发送到服务器,但问题没有解决。我也尝试过设置Connection close标头。但是,问题仍然存在。

protected String doInBackground(String... url) {
    JSONObject body = new JSONObject();
    try {
        body.put("body","");
    } catch (JSONException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }

    mUploadChunkURL = url[0];
    System.out.println("length: " + mDataLength + " String length: " + String.valueOf(mDataLength));
    URL myurl;
    HttpsURLConnection con = null;
    while (numberOfTries < MAX_RETRIES) {
        try {
            myurl = new URL(mUploadChunkURL);
            con = (HttpsURLConnection)myurl.openConnection();
            con.setRequestMethod("POST");
            con.setConnectTimeout(1000*60*2);
            con.setRequestProperty("Content-Type","application/x-www-form-urlencoded"); 
            con.setDoOutput(true); 
            con.setDoInput(true);

            if (Build.VERSION.SDK != null
                    && Build.VERSION.SDK_INT > 13) {
                System.out.println("connection close");
                con.setRequestProperty("Connection", "close");
                    }

            DataOutputStream output = new DataOutputStream(con.getOutputStream());  
            output.writeBytes(body.toString());
            output.close();

            System.out.println("Resp Code: "+con.getResponseCode()); 
            System.out.println("Resp Message: "+ con.getResponseMessage()); 

            Log.e("64","Resp Message: "+inputLine.toString());

        } catch (IOException e) {
            Log.e("70", "IO exception: " + e.toString());
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {

            if (con != null)
                con.disconnect();
        }

        numberOfTries++;
    }

    return null;
}

我收到了EOFException。我在这行“System.out.println(”Resp Code:“+ con.getResponseCode());”中获得此异常。下面是错误日志。

05-23 15:46:25.281: W/System.err(16688): java.io.EOFException
05-23 15:46:25.281: W/System.err(16688):    at libcore.io.Streams.readAsciiLine(Streams.java:203)
05-23 15:46:25.281: W/System.err(16688):    at libcore.net.http.HttpEngine.readHeaders(HttpEngine.java:608)
05-23 15:46:25.281: W/System.err(16688):    at libcore.net.http.HttpEngine.readResponseHeaders(HttpEngine.java:561)
05-23 15:46:25.281: W/System.err(16688):    at libcore.net.http.HttpEngine.readResponse(HttpEngine.java:813)
05-23 15:46:25.281: W/System.err(16688):    at libcore.net.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:274)
05-23 15:46:25.281: W/System.err(16688):    at libcore.net.http.HttpURLConnectionImpl.getResponseCode(HttpURLConnectionImpl.java:486)
05-23 15:46:25.281: W/System.err(16688):    at libcore.net.http.HttpsURLConnectionImpl.getResponseCode(HttpsURLConnectionImpl.java:134)
05-23 15:46:25.281: W/System.err(16688):    at com.utility.UploadFileChunk.doInBackground(UploadFileChunk.java:71)
05-23 15:46:25.296: W/System.err(16688):    at com.utility.UploadFileChunk.doInBackground(UploadFileChunk.java:1)
05-23 15:46:25.296: W/System.err(16688):    at android.os.AsyncTask$2.call(AsyncTask.java:287)
05-23 15:46:25.296: W/System.err(16688):    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
05-23 15:46:25.296: W/System.err(16688):    at java.util.concurrent.FutureTask.run(FutureTask.java:137)
05-23 15:46:25.296: W/System.err(16688):    at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
05-23 15:46:25.296: W/System.err(16688):    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
05-23 15:46:25.296: W/System.err(16688):    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
05-23 15:46:25.296: W/System.err(16688):    at java.lang.Thread.run(Thread.java:856)

请帮帮我。

1 个答案:

答案 0 :(得分:3)

实际上网址形成不好。因此我得到了EOFException。我在url中发送Base64数据,在将数据转换为Base64时,我没有给出NO_WRAP标志。因为Base64数据不是单行长的。