HttpURLConnection 连接网络出现 EOFException

五柳--先生 2014-12-01 02:58:45
HttpURLConnection 连接网络时候出现 EOFException这个异常,看完之后没有一个比较好的有效的解决办法

	public String getRequestResult(String requestUrl,
Map<String, String> parameter) throws TimeoutException,
IOException, EOFException {

String json = "";
if (parameter != null && !parameter.isEmpty()) {
for (Map.Entry<String, String> entry : parameter.entrySet()) {
// 获取对应value
json = entry.getValue();
}
}

// try{
LogUtil.d("requestUrl = " + requestUrl);
parameter.clear();
parameter.put("json", json);
json.replace(" ", "");
// Post请求的url,与get不同的是不需要带参数
URL postUrl = new URL(requestUrl);
// 打开连接
HttpURLConnection mHttpURLConn = (HttpURLConnection) postUrl
.openConnection();
// 设置是否向connection输出,因为这个是post请求,参数要放在http正文内,因此需要设为true
mHttpURLConn.setDoOutput(true);
// Read from the connection. Default is true.
mHttpURLConn.setDoInput(true);
mHttpURLConn.setRequestMethod("POST");
mHttpURLConn.setRequestProperty("Accept-Encoding", "");
// Post 请求不能使用缓存
mHttpURLConn.setUseCaches(false);
mHttpURLConn.setInstanceFollowRedirects(true);
mHttpURLConn.setConnectTimeout(connectTimeout);
mHttpURLConn.setReadTimeout(readTimeout);
// 配置本次连接的Content-type,配置为application/x-www-form-urlencoded的
// 意思是正文是urlencoded编码过的form参数,下面我们可以看到我们对正文内容使用URLEncoder.encode进行编码
mHttpURLConn.setRequestProperty("Content-Type",
"application/x-www-form-urlencoded");
mHttpURLConn.setRequestProperty("Connection", "keep-alive");
if (Build.VERSION.SDK != null && Build.VERSION.SDK_INT > 13) {
mHttpURLConn.setRequestProperty("Connection", "close");
}

// 连接,从postUrl.openConnection()至此的配置必须要在connect之前完成,
// 要注意的是connection.getOutputStream会隐含的进行connect。
mHttpURLConn.connect();
DataOutputStream out = new DataOutputStream(
mHttpURLConn.getOutputStream());
String content = "json=" + URLEncoder.encode(json, "UTF-8");
// DataOutputStream.writeBytes将字符串中的16位的unicode字符以8位的字符形式写到流里面
out.writeBytes(content);
out.flush();
out.close();
System.setProperty("http.keepAlive", "false");
LogUtil.e(
"",
"mHttpURLConn.getResponseCode() = "
+ mHttpURLConn.getResponseCode());


if (mHttpURLConn.getResponseCode() == HttpURLConnection.HTTP_OK) {
InputStream is = mHttpURLConn.getInputStream();
BufferedReader in = new BufferedReader(new InputStreamReader(is));
StringBuffer buffer = new StringBuffer();
String line = "";
while ((line = in.readLine()) != null) {
buffer.append(line);
}
in.close();
is.close();
mHttpURLConn.disconnect();
if (buffer.toString().length() == 0) {
return null;
}
LogUtil.d("request return = " + buffer.toString());
return buffer.toString();
}

return null;
}


红色部分是几乎所有网上给出的办法,试过之后,还是会有这个异常。

不知道谁有没有解决过这个问题可以分享一下的?

ps,不用建议我换Httpclient 了哦,遇到问题我不想绕过去,而且Httpclient 会有更多的bug,类似 NohttpResponseException等

...全文
203 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
bigobingo 2014-12-26
  • 打赏
  • 举报
回复
实在不行自己通过 socklet封装一个 HttpURLConnection 了

80,362

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧