2

I am creating connection with server inside AsyncTask. I am using HttpURLConnection. I am sending the data in url. Below is the code of doInBackground() method of AsyncTask. I tried retrying to send the data to server but the issue was not fixed. I also tried setting Connection close header. However, the issue still persists.

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;
}

I am getting EOFException. I am getting this exception in this line "System.out.println("Resp Code: "+con.getResponseCode());". Below is Error Log.

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)

Please help me out.

Nitesh Kumar
  • 5,370
  • 5
  • 37
  • 54
  • did you add internet permission in manifest? – Selvin May 23 '14 at 11:25
  • internet permissions are fine – Nitesh Kumar May 23 '14 at 11:37
  • Try moving your `output.close();` behind the 2 println's. The end of file exception means your con.getresponsecode is trying to read from something that has already reached its end. Not sure if this will actually help btw, just guessing as to what the cause could be. – SvenT23 May 23 '14 at 15:03

1 Answers1

3

Actually the url was not well formed. Hence I was getting EOFException. I was sending Base64 data in url and while converting the data into Base64 I was not giving NO_WRAP flag. Becasue of that the Base64 data was not coming in single long line.

Nitesh Kumar
  • 5,370
  • 5
  • 37
  • 54