简体   繁体   English

Android中的IllegalArgumentException

[英]IllegalArgumentException in Android

I am using the following code to obtain an Http Response in an Android app. 我正在使用以下代码在Android应用程序中获取Http响应。

 HttpGet get = new HttpGet(targetURL); 
 HttpClient httpclient = new DefaultHttpClient();
 HttpResponse response = httpclient.execute(get);
 String responseBody = EntityUtils.toString(response.getEntity());

But HttpResponse response = httpclient.execute(get); 但是HttpResponse response = httpclient.execute(get); is throwing an java.lang.IllegalArgumentException: Host name may not be null 引发java.lang.IllegalArgumentException: Host name may not be null

Any help on what is going wrong? 有什么问题的帮助吗?

You are missing the "scheme" part of your URI. 您缺少URI的“方案”部分。 All URIs require a scheme: 所有URI都需要一个方案:

http://en.wikipedia.org/wiki/Uniform_resource_identifier#Syntax http://en.wikipedia.org/wiki/Uniform_resource_identifier#Syntax

The URI syntax consists of a URI scheme name (such as "http", "ftp", "mailto", "crid" or "file") followed by a colon character, and then by a scheme-specific part. URI语法由URI方案名称(例如“ http”,“ ftp”,“ mailto”,“ crid”或“ file”)组成,后跟冒号,然后是方案特定的部分。

Add http:// on to your String, so make it http://localhost:8080 在您的String上添加http:// ,因此将其设置为http://localhost:8080

HttpGet throws an IllegalArgumentException if the URI is invalid, so that's exactly what is happening. 如果URI无效,则HttpGet抛出IllegalArgumentException ,这就是正在发生的情况。

HttpGet documentation HttpGet文档

From http://developer.android.com/reference/org/apache/http/client/methods/HttpGet.html#HttpGet%28java.lang.String%29 来自http://developer.android.com/reference/org/apache/http/client/methods/HttpGet.html#HttpGet%28java.lang.String%29

Your targetUrl is invalid. 您的targetUrl无效。 So maybe the String is wrong. 因此,也许String是错误的。 Another possibility is the answer to: 另一个可能的答案是:

Java Android , HttpGet error - Host name may not be null Java Android,HttpGet错误-主机名不能为null

There the problem was that the request was changed by the server when it came from a mobile device. 那里的问题是,当请求来自移动设备时,服务器已更改了该请求。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM