【发布时间】:2012-09-21 20:08:14
【问题描述】:
我收到错误“目标主机不能为空,或在参数中设置”。
- 我确实在我的清单文件中有 Internet 权限
- 我在我的网址前添加了“http://”
- 我愿意对网址进行编码
这是我的代码:
String url = "http://maps.google.com/maps/api/directions/json?origin=1600 Pennsylvania Avenue NW, Washington, DC 20500&destination=1029 Vermont Ave NW, Washington, DC 20005&sensor=false";
HttpClient httpclient = new DefaultHttpClient();
String goodURL = convertURL(url);//change weird characters for %etc
HttpPost httppost = new HttpPost(goodURL);
HttpResponse response = httpclient.execute(httppost);
在第 5 行(上面的最后一行),我的程序抛出异常。这是确切的错误:
java.lang.IllegalArgumentException: Host name may not be null
我确实在方法 convertURL 中对我的字符串进行编码...
goodURL= http://maps.google.com/maps/api/directions/json?origin=3%20Cedar%20Ave%2c%20Highland%20Park%2c%20NJ%2008904&destination=604%20Bartholomew%20Road%2c%20Piscataway%2c%20New%20Jersey%2008854&sensor=false
有什么建议吗? 谢谢!
【问题讨论】:
-
网址中不能有空格!让我看看你是如何编码你的 URL 的
-
您的问题可能出在您的 convertURL 函数中...使用调试器并检查返回的内容。
-
嗨!我传递了整个 URL,我修改了它,但它仍然给我错误... goodURL=
http://maps.google.com/maps/api/directions/json?origin=3%20Cedar%20Ave%2c%20Highland%20Park%2c%20NJ%2008904&destination=604%20Bartholomew%20Road%2c%20Piscataway%2c%20New%20Jersey%2008854&sensor=false -
好的,你们是对的!我正在传递整个 URL...我修复了它,但现在它给了我这个异常! android.os.NetworkOnMainThreadException 但我可以研究一下这是什么意思...谢谢!
标签: android http-post httpresponse illegalargumentexception