我正在开发这个android应用程序,它基本上将图像上传到网络服务。
我有一个异步任务,使用以下代码将文件发送到服务器:
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();
}
当然,这段代码包在try-catch、catching IOExceptions等中。
问题是当我中断连接时
之后
我看到了第一个日志文本,从来没有抛出异常,或者只有在很长一段时间(大约20分钟左右)之后才会抛出异常,这根本没有任何意义。
HttpURLConnection设置如下:
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();
有趣的是,这种情况只有在中断EDGE/3G连接时才会发生。当我中断wifi连接时,会立即抛出异常(当然,这要方便得多)。
对此有什么想法吗?