============问题描述============
以下是android发送Http请求的代码,但是经常出现:服务器响应很久, android端出现ReadTimeout, android端又会再发一次同样的请求,导致数据重复。有人说是jdk1.4.2的问题,但我用的是1.6.0。请大神们帮忙
URL url = null;
HttpURLConnection conn = null;
String result = "";
try {
String sParam = "&m=" + methodName;
if (vector != null) {
for (int i = 0; i < vector.size(); i++) {
sParam += "&p" + String.valueOf(i) + "=" + vector.elementAt(i).toString();
}
}
byte[] data = sParam.getBytes();
url = new URL(realURL.replace("xx.mobile", methodName+".mobile"));
conn = (HttpURLConnection) url.openConnection();
conn.setConnectTimeout(CONNECT_TIMEOUT);
conn.setReadTimeout(READ_TIMEOUT);
conn.setDoInput(true);
conn.setDoOutput(true);
conn.setRequestMethod(METHOD_POST);
conn.setRequestProperty("Connection", "Keep-Alive");
conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
conn.setRequestProperty("Content-Length", String.valueOf(data.length));
conn.setRequestProperty("Charset", "utf-8");
conn.setUseCaches(false);
OutputStream outStream = conn.getOutputStream();
outStream.write(data);
outStream.flush();
outStream.close();
if (conn.getResponseCode() == HttpURLConnection.HTTP_OK) // ResponseCode:200
{
InputStream inStream = conn.getInputStream();
//int contentLen = conn.getContentLength();
result = readInputStream(inStream,1024);
inStream.close();
} else {
result = "500"; //server error
}
} catch (ConnectTimeoutException e) {
// TODO: handle exception
CommonData.Log("ConnectTimeoutException");
throw new MyException(HTTP_RESULT_CONNECT_TIMEOUT);
} catch (SocketTimeoutException e) {
// TODO: handle exception
CommonData.Log("SocketTimeoutException");
throw new MyException(HTTP_RESULT_READ_TIMEOUT);
} catch (Exception e) {
// TODO: handle exception
CommonData.Log(e.getLocalizedMessage());
throw new MyException(HTTP_RESULT_EXCEPTION);
} finally {
conn.disconnect();
}
============解决方案1============
尝试用 httpclient 吧
看不出什么问题,属于疑难杂症吧
============解决方案2============
底层的实现中 如果发现http请求超时 有重发机制
============解决方案3============
引用 16 楼 Roy_se7en 的回复:
Quote: 引用 15 楼 ysh512 的回复:
Quote: 引用 13 楼 Roy_se7en 的回复:
Quote: 引用 11 楼 ysh512 的回复:
底层的实现中 如果发现http请求超时 有重发机制
请问,这个有资料可查吗?如何能禁用呢?
以前在一个人的blog中看到的,现在找不到了
麻烦帮忙找下,万分感谢。
原来那个找不到了 你看看这个 意思类似
http://blog.csdn.net/hanqunfeng/article/details/4510338
============解决方案4============
http://developer.nokia.com/community/wiki/Using_HTTP_and_HTTPS_in_Java_ME
这个你看下
还有你的j2me 中的相关代码 也请po出来, 前面的是 android中的代码。
============解决方案5============
1:
http://bugs.java.com/view_bug.do?bug_id=6672144
这里面描述的 bug 和你说的很像。
它的解决方案是设置系统属性。
From JDK6 you can set the system property, sun.net.http.retryPost, to false to prevent the HTTP client from silently resending the POST request.
java -Dsun.net.http.retryPost=false ...
2:
http://developer.nokia.com/community/discussion/showthread.php/233689-HTTP-Connection-sending-request-twice-after-30-sec
这个完全是你说的案例。它的建议是后台参与进来
============解决方案6============
引用 32 楼 Roy_se7en 的回复:
Quote: 引用 31 楼 foolsheep 的回复:
连接超时和读取超时时间会不会不一致?
这2者不一致有关系吗?
如果连接还没有超时,而读取时间超时的话,HttpUrlConnection就会再重新发送请求。所以,要解决这个重发问题,可以试一下,把读取超时的时间设置得比连接超时稍长一点,因为有时候,jdk计算的时间会有点偏差,这样可以保证在连接超时之前不会读取超时