为 android 执行的 HttpResponse 中的主机名不能为空

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12537522/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 10:55:10  来源:igfitidea点击:

Host name may not be null in HttpResponse execute for android

androidhttp-posthttpresponseillegalargumentexception

提问by cafesanu

I get the error "Target host must not be null, or set in parameters".

我收到错误“目标主机不能为空,或在参数中设置”。

  • I DOhave Internet permission in my manifest file
  • I have put 'http://' before my Url
  • I DOencode the URL
  • I DO具有Internet权限在我的清单文件
  • 我在我的 URL 之前放了 'http://'
  • 对 URL 进行编码

This is my code:

这是我的代码:

   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);

In 5th line (last line above), my program throws an exception. here is the exact error:

在第 5 行(上面的最后一行),我的程序抛出异常。这是确切的错误:

java.lang.IllegalArgumentException: Host name may not be null

I Do encode my string in method convertURL...

我确实在方法 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

好网址= 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

Any suggestions? Thanks!

有什么建议?谢谢!

采纳答案by Devunwired

I'm not sure what your URL encode method is doing, but if you are using a method from the framework like URLEncoder, you should never pass the full URL, just the parameters list you need to encode to escape special characters.

我不确定您的 URL 编码方法在做什么,但是如果您使用的是框架中的方法,例如URLEncoder则永远不应该传递完整的 URL,只传递您需要编码以转义特殊字符的参数列表。

Encoding the full URL will percent escape every character, including the ://into %3A%2F%2Fand all additional slashes into %2F.

编码完整的 URL 将百分比转义每个字符,包括://into%3A%2F%2F和所有附加斜杠到%2F.

Take a look at the value of your goodUrlstring after you encode it.

goodUrl编码后查看字符串的值。

回答by Samer

Just use:

只需使用:

URLEncoder.encode(YOUR_STRING);

回答by AWT

Encode your URL string before you post the request, but only encode the parameters after the ?:

在发布请求之前对您的 URL 字符串进行编码,但只对 ? 之后的参数进行编码:

String url = "http://maps.google.com/maps/api/directions/json?";
String params = "origin=1600 Pennsylvania Avenue NW, Washington, DC 20500&destination=1029 Vermont Ave NW, Washington, DC 20005&sensor=false";
HttpClient httpclient = new DefaultHttpClient();
String goodParams = convertURL(params);//change weird characters for %etc
HttpPost httppost = new HttpPost(url + goodParams);
HttpResponse response = httpclient.execute(httppost);