首页上一页 1 下一页尾页 8 条记录 1/1页
学习《Android从入门到精通》14.1.2使用httpclient遇到问题
发表在Android图书答疑
2014-10-09
是否精华
是
否
版块置顶:
是
否
设置时间:
非永久
永久
起始时间:
结束时间:
是否扣分:
是
否
14.1.2使用httpclient访问网络,不带符号“_”的URL都能正常访问,遇到带有“_”符号的URL程序就出异常了,请问老师有什么方法解决?
精彩评论 8
2014-10-09
4L
回复:
是我大意了,没仔细看书中的例子也有带_符号的url。
老师书中例子的url运行正常,不闪退,但是运行这样的url就闪退了,,
public void send() {
String target = "http://hero_189hi_1.yytou.com:51921/gCmd.do?cmd=11&sid=gvhoj42hqcb6k91qp8nb2"; //要提交的目标地址
HttpClient httpclient = new DefaultHttpClient();//创建HttpClient对象
HttpGet httpRequest = new HttpGet(target); //创建HttpGet连接对象
HttpResponse httpResponse;
try {
httpResponse = httpclient.execute(httpRequest); //执行HttpClient请求
if (httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK){
result = EntityUtils.toString(httpResponse.getEntity()); //获取返回的字符串
}else{
result="请求失败!";
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
开发环境:ADT-22.6.3
android-sdk_r22.6.2-windows
eclipse-java-kepler-SR2-win32
2014-10-10
5L
10-10 11:01:01.272: E/AndroidRuntime(9045): FATAL EXCEPTION: Thread-11083
10-10 11:01:01.272: E/AndroidRuntime(9045): Process: com.autoweb, PID: 9045
10-10 11:01:01.272: E/AndroidRuntime(9045): java.lang.IllegalArgumentException: Host name may not be null
10-10 11:01:01.272: E/AndroidRuntime(9045): at org.apache.http.HttpHost.<init>(HttpHost.java:83)
10-10 11:01:01.272: E/AndroidRuntime(9045): at org.apache.http.impl.client.AbstractHttpClient.determineTarget(AbstractHttpClient.java:497)
10-10 11:01:01.272: E/AndroidRuntime(9045): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
10-10 11:01:01.272: E/AndroidRuntime(9045): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
10-10 11:01:01.272: E/AndroidRuntime(9045): at com.autoweb.MainActivity.send(MainActivity.java:387)
10-10 11:01:01.272: E/AndroidRuntime(9045): at com.autoweb.MainActivity.read(MainActivity.java:357)
10-10 11:01:01.272: E/AndroidRuntime(9045): at com.autoweb.MainActivity$3$1.run(MainActivity.java:215)
10-10 11:01:01.272: E/AndroidRuntime(9045): at java.lang.Thread.run(Thread.java:841)
10-10 11:01:01.272: E/AndroidRuntime(9045): Process: com.autoweb, PID: 9045
10-10 11:01:01.272: E/AndroidRuntime(9045): java.lang.IllegalArgumentException: Host name may not be null
10-10 11:01:01.272: E/AndroidRuntime(9045): at org.apache.http.HttpHost.<init>(HttpHost.java:83)
10-10 11:01:01.272: E/AndroidRuntime(9045): at org.apache.http.impl.client.AbstractHttpClient.determineTarget(AbstractHttpClient.java:497)
10-10 11:01:01.272: E/AndroidRuntime(9045): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
10-10 11:01:01.272: E/AndroidRuntime(9045): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
10-10 11:01:01.272: E/AndroidRuntime(9045): at com.autoweb.MainActivity.send(MainActivity.java:387)
10-10 11:01:01.272: E/AndroidRuntime(9045): at com.autoweb.MainActivity.read(MainActivity.java:357)
10-10 11:01:01.272: E/AndroidRuntime(9045): at com.autoweb.MainActivity$3$1.run(MainActivity.java:215)
10-10 11:01:01.272: E/AndroidRuntime(9045): at java.lang.Thread.run(Thread.java:841)