android EOFException HttpURLConnection getResponseCode

lipoxie 2012-07-20 05:54:23
我在调用HttpURLConnection 的 getResponseCode方法时候,有的时候会报EOFException这个错误
//07-20 16:21:28.040: W/System.err(11065): java.io.EOFException
//07-20 16:21:28.040: W/System.err(11065): at libcore.io.Streams.readAsciiLine(Streams.java:203)
//07-20 16:21:28.040: W/System.err(11065): at libcore.net.http.HttpEngine.readResponseHeaders(HttpEngine.java:544)
//07-20 16:21:28.040: W/System.err(11065): at libcore.net.http.HttpEngine.readResponse(HttpEngine.java:784)
//07-20 16:21:28.060: W/System.err(11065): at libcore.net.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:274)
//07-20 16:21:28.060: W/System.err(11065): at libcore.net.http.HttpURLConnectionImpl.getResponseCode(HttpURLConnectionImpl.java:479)
偶发的,不是每次出现,有谁遇到过这种情况啊,是不是网络不好导致?谁来帮帮忙啊?
...全文
1183 12 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
哎,真难 2014-05-19
  • 打赏
  • 举报
回复
建议用httpclient吧
s478853630 2014-05-19
  • 打赏
  • 举报
回复
private WebUtil() {
	}

	private static WebUtil bean = new WebUtil();
	private String requestUrl = "";
	private int requestTime = 0;
	private String method = Constant.REQUEST_GET;
	private String encoding = HTTP.UTF_8;

	/**
	 * 构造方法
	 * 
	 * @param requestUrl
	 *            请求的地址
	 * @param requestTime
	 *            请求时长
	 * @param method
	 *            请求方式
	 * @param encoding
	 *            编码格式
	 */
	public WebUtil(String requestUrl, int requestTime, String method, String encoding) {
		this.requestUrl = requestUrl;
		this.requestTime = requestTime;
		this.method = method;
		this.encoding = encoding;
	}

	/**
	 * 单例模式获得bean
	 * 
	 * @return WebUtil
	 */
	public static WebUtil get() {
		return bean;
	}

	/**
	 * 根据url获得页面源码, 调用web接口
	 * 
	 * @param params
	 *            参数列表 post必备, 比如:"name=张三&age=18"
	 * @param sessionInfo
	 *            可以保持session, 默认不保持
	 * @param isLine
	 *            得到的源码是否换行, 默认false
	 * @return String
	 */
	public String request(String params, String sessionInfo, boolean isLine) throws Exception {
		Log.e(Constant.TAG_NAME, "调用web接口:" + requestUrl);
		encoding = (ToolUtil.get().isBlank(encoding) ? HTTP.UTF_8 : encoding);
		method = (ToolUtil.get().isBlank(method) ? Constant.REQUEST_GET : method.toUpperCase());
		String mathStr = "mathRandom=" + Math.random();
		if (method.equals(Constant.REQUEST_GET)) {
			requestUrl += (requestUrl.indexOf("?") != -1 ? "&" : "?") + mathStr;
			if (!ToolUtil.get().isBlank(params)) {
				requestUrl += "&" + params;
			}
		}
		HttpURLConnection conn = (HttpURLConnection) new URL(requestUrl).openConnection();
		if (!ToolUtil.get().isBlank(sessionInfo)) {
			conn.setRequestProperty("Cookie", sessionInfo);
		}
		conn.setRequestMethod(method);
		if (requestTime > 0) {
			conn.setConnectTimeout(isWifi() ? requestTime : requestTime * 3);
			conn.setReadTimeout(isWifi() ? requestTime : requestTime * 3);
		}
		if (method.equals(Constant.REQUEST_POST)) {
			conn.setDoOutput(true);
			OutputStream output = conn.getOutputStream();
			output.write((ToolUtil.get().isBlank(params) ? mathStr : params + "&" + mathStr).getBytes(encoding));
			output.flush();
			output.close();
		}
		String response = FileUtil.get().readFile(conn.getInputStream(), encoding, 1, isLine);
		conn.disconnect();
		return response;
	}
Frank-Shi 2014-05-12
  • 打赏
  • 举报
回复
要的是解决方案哪
Addison 2014-02-19
  • 打赏
  • 举报
回复
这是ksoap2 jar包的bug问题。 请下载3.0 或者 3.1的版本。
yumin1891 2013-08-25
  • 打赏
  • 举报
回复
我也遇到了,求解决办法
Freedom229 2013-08-16
  • 打赏
  • 举报
回复
我也遇到了,求解决办法
劇終男爵 2013-05-23
  • 打赏
  • 举报
回复
我也遇到了 有人解决了没?
OAOAAce 2013-04-30
  • 打赏
  • 举报
回复
相同问题呀? 请问如何解决呢?
nuaa123lhk 2012-11-27
  • 打赏
  • 举报
回复
顶一个,我也遇到啦。android4.0就有的啊
longyingfei000 2012-11-27
  • 打赏
  • 举报
回复
同样遇到该问题,求解决方法
qdkfriend 2012-07-23
  • 打赏
  • 举报
回复
百度是这么回答的:文件结尾了还继续读文件,就导致这个错误抛出。本来EOF的数值就是-1,但是是一种用于错误标识的记号
lipoxie 2012-07-23
  • 打赏
  • 举报
回复
怎么没人呢 顶个

80,471

社区成员

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

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