HttpClient发生IOException的问题

guxue365 2014-04-10 05:35:02
每次执行int statusCode = httpClient.executeMethod(httpGet);就会发生错误,请问这是为什么?

private static HttpClient getHttpClient() {
HttpClient httpClient = new HttpClient();
// 设置 HttpClient 接收 Cookie,用与浏览器一样的策略
httpClient.getParams().setCookiePolicy(CookiePolicy.BROWSER_COMPATIBILITY);
// 设置 默认的超时重试处理策略
httpClient.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler());
// 设置 连接超时时间
httpClient.getHttpConnectionManager().getParams().setConnectionTimeout(20000/*TIMEOUT_CONNECTION*/);
// 设置 读数据超时时间
httpClient.getHttpConnectionManager().getParams().setSoTimeout(20000/*TIMEOUT_SOCKET*/);
// 设置 字符集
httpClient.getParams().setContentCharset("UTF-8"/*UTF_8*/);
return httpClient;
}

private static GetMethod getHttpGet(String url, String cookie, String userAgent) {
GetMethod httpGet = new GetMethod(url);
// 设置 请求超时时间
httpGet.getParams().setSoTimeout(20000/*TIMEOUT_SOCKET*/);
httpGet.setRequestHeader("Host", "www.baidu.com"
/*URLs.HOST*/);
httpGet.setRequestHeader("Connection","Keep-Alive");
httpGet.setRequestHeader("Cookie", cookie);
httpGet.setRequestHeader("User-Agent", userAgent);
return httpGet;
}

private static void RunHttp()
{
new Thread() {
public void run() {
HttpClient httpClient = null;
GetMethod httpGet = null;
String newUrl = "http://www.baidu.com";
try
{
httpClient = getHttpClient();;
httpGet = getHttpGet(newUrl, null, null);


int statusCode = httpClient.executeMethod(httpGet);
if (statusCode != HttpStatus.SC_OK)
{
// throw AppException.http(statusCode);
int a = 5;
}
}
catch(IOException e)
{
e.printStackTrace();
}
finally
{
// 释放连接
httpGet.releaseConnection();
httpClient = null;
}
}
}.start();
}
...全文
563 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
Darcy杨 2014-04-11
  • 打赏
  • 举报
回复
你这个貌似是没加网络权限吧
nj_dobetter 2014-04-11
  • 打赏
  • 举报
回复
04-10 11:04:31.651: W/System.err(413): java.net.UnknownHostException: www.baidu.com
风吹得好舒服 2014-04-10
  • 打赏
  • 举报
回复
可以看下这个 http://honda418.iteye.com/blog/337052/
guxue365 2014-04-10
  • 打赏
  • 举报
回复
04-10 11:03:03.101: W/ActivityThread(413): Application com.example.testhttpclient2 is waiting for the debugger on port 8100... 04-10 11:03:03.121: I/System.out(413): Sending WAIT chunk 04-10 11:03:03.262: I/dalvikvm(413): Debugger is active 04-10 11:03:03.321: I/System.out(413): Debugger has connected 04-10 11:03:03.321: I/System.out(413): waiting for debugger to settle... 04-10 11:03:03.531: I/System.out(413): waiting for debugger to settle... 04-10 11:03:03.731: I/System.out(413): waiting for debugger to settle... 04-10 11:03:03.931: I/System.out(413): waiting for debugger to settle... 04-10 11:03:04.131: I/System.out(413): waiting for debugger to settle... 04-10 11:03:04.331: I/System.out(413): waiting for debugger to settle... 04-10 11:03:04.532: I/System.out(413): waiting for debugger to settle... 04-10 11:03:04.731: I/System.out(413): waiting for debugger to settle... 04-10 11:03:04.942: I/System.out(413): waiting for debugger to settle... 04-10 11:03:05.143: I/System.out(413): waiting for debugger to settle... 04-10 11:03:05.346: I/System.out(413): debugger has settled (1321) 04-10 11:03:07.901: I/ActivityManager(61): Displayed com.example.testhttpclient2/.MainActivity: +5s533ms 04-10 11:03:13.021: D/dalvikvm(130): GC_EXPLICIT freed 127K, 51% free 2903K/5831K, external 2142K/2590K, paused 63ms 04-10 11:03:22.791: D/SntpClient(61): request time failed: java.net.SocketException: Address family not supported by protocol 04-10 11:04:31.651: W/System.err(413): java.net.UnknownHostException: www.baidu.com 04-10 11:04:31.681: W/System.err(413): at java.net.InetAddress.lookupHostByName(InetAddress.java:506) 04-10 11:04:31.692: W/System.err(413): at java.net.InetAddress.getAllByNameImpl(InetAddress.java:294) 04-10 11:04:31.692: W/System.err(413): at java.net.InetAddress.getAllByName(InetAddress.java:256) 04-10 11:04:31.711: W/System.err(413): at java.net.Socket.tryAllAddresses(Socket.java:130) 04-10 11:04:31.711: W/System.err(413): at java.net.Socket.<init>(Socket.java:209) 04-10 11:04:31.731: W/System.err(413): at org.apache.commons.httpclient.protocol.DefaultProtocolSocketFactory.createSocket(DefaultProtocolSocketFactory.java:80) 04-10 11:04:31.731: W/System.err(413): at org.apache.commons.httpclient.protocol.DefaultProtocolSocketFactory.createSocket(DefaultProtocolSocketFactory.java:122) 04-10 11:04:31.741: W/System.err(413): at org.apache.commons.httpclient.HttpConnection.open(HttpConnection.java:707) 04-10 11:04:31.741: W/System.err(413): at org.apache.commons.httpclient.HttpMethodDirector.executeWithRetry(HttpMethodDirector.java:387) 04-10 11:04:31.741: W/System.err(413): at org.apache.commons.httpclient.HttpMethodDirector.executeMethod(HttpMethodDirector.java:171) 04-10 11:04:31.751: W/System.err(413): at org.apache.commons.httpclient.HttpClient.executeMethod(HttpClient.java:397) 04-10 11:04:31.751: W/System.err(413): at org.apache.commons.httpclient.HttpClient.executeMethod(HttpClient.java:323) 04-10 11:04:31.761: W/System.err(413): at com.example.testhttpclient2.MainActivity$2.run(MainActivity.java:143)
MR-LEE 2014-04-10
  • 打赏
  • 举报
回复
给个loget看看啊。。

80,360

社区成员

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

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