Question

I'm developing this android application which basically uploads images to a webservice. I have a asynctask where I send the file to the server, using this code:

protected Boolean doInBackground(byte[]... params) {
     HttpURLConnection connection = getConnection();
     BufferedOutputStream out = new BufferedOutputStream(connection.getOutputStream()); 
     Log.d("OutputStream", "stream created, about to write");
     out.write(params[0]);
     Log.d("OutputStream", "all bytes written");
     out.close();
}

Of course, this block of code is wrapped within a try catch, catching IOExceptions etc. The problem is that when I interrupt the connection after I see the first logtext, an exception is never thrown or only after a really long time (talking about 20 minutes or so), which doesn't make any sense at all.

The HttpURLConnection is set up like this:

urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setRequestMethod("POST");         
urlConnection.setRequestProperty("Content-Type", this.contentType);
urlConnection.setRequestProperty("User-Agent", String.format("custom-user-agent/%d", R.integer.version_code));
urlConnection.addRequestProperty("SessionID", this.sessionID);
urlConnection.setDoOutput(true);
urlConnection.setConnectTimeout(30000);
urlConnection.setReadTimeout(30000);
urlConnection.setChunkedStreamingMode(0);
urlConnection.setUseCaches(false);
urlConnection.connect();

Funny thing is, this only happens when interrupting a EDGE/3G connection. When I interrupt a wifi connection, the exception is thrown immediately (which, of course, is a lot more convenient).

Any idea on this?

Was it helpful?

Solution

A while ago, we've encountered this problem also, but only on a Samsung Galaxy SII with Android 2.3.4. All other devices did not have this issue. There is no way to program around this issue.

OTHER TIPS

You don't need use urlConnection.connect(): the connection is open implicity when yo do the .getOutputStream(). Also, if you make the .connect(), you can't make it before at the .getOutputStream(). In addition, you must set the Content-Type header:

urlConnection.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top