我将非常感谢您对此的帮助,下面是我正在尝试执行的代码,但所有我得到的是这个例外,我做了很多更改,但我无法解决这个问题.
如果你有任何指针,请告诉我,我在Android 4.4.4上运行
httpGet request = new httpGet("https://s3-eu-west-1.amazonaws.com/developer-application-test/cart/List");resp = clIEnt.execute(request);01-22 22:25:03.885: W/System.err(14697): java.lang.IllegalArgumentException: Host name may not be null01-22 22:25:03.886: W/System.err(14697): at org.apache.http.httpHost.<init>(httpHost.java:83)01-22 22:25:03.886: W/System.err(14697): at org.apache.http.impl.clIEnt.AbstracthttpClIEnt.determineTarget(AbstracthttpClIEnt.java:508)01-22 22:25:03.886: W/System.err(14697): at org.apache.http.impl.clIEnt.AbstracthttpClIEnt.execute(AbstracthttpClIEnt.java:498)01-22 22:25:03.886: W/System.err(14697): at org.apache.http.impl.clIEnt.AbstracthttpClIEnt.execute(AbstracthttpClIEnt.java:476)
解决方法:
正如@atish shimpi所提到的,这很可能是由于URL格式不正确造成的.我输入以下代码剪切并在开发电话上调试它:
httpClIEnt httpClIEnt = new DefaulthttpClIEnt();URI url = new URI("https://s3-euwest1.amazonaws.com/developer-applicationtest/cart/List");URI url1 = new URI("https://www.Google.com/");httpGet httpGet = new httpGet(url);httpResponse httpResponse = httpClIEnt.execute(httpGet);httpentity httpentity = httpResponse.getEntity();
如您所见,我添加了另一个URI对象,该对象指向https://www.Google.com/以用作比较.当我调试时,我在创建两个URI对象时设置断点.在为您提供的地址创建相应的URI对象后,主机字段为空…
但是,当我为Google地址创建一个类似的URI对象时,主机字段不为空,这意味着您的地址有问题…
我仍然不太确定为什么方法URI(String spec)无法解析正确的字段.这可能是一个错误,也可能与您的特定URL有关.无论如何,我能够通过获取您提供的链接并手动创建URI对象来最终处理请求,如下所示:
URI uri = new URI("https", "s3-eu-west-1.amazonaws.com", "/developer-application-test/cart/List", null, null);
使用这个手动创建的URI,我可以下载您创建的列表:
"products" : [ { "product_ID" : "1", "name" : "Apples", "price" : 120, "image" : "https://s3-eu-west-1.amazonaws.com/developer-application-test/images/1.jpg" }, { "product_ID" : "2", "name" : "Oranges", "price" : 167, "image" : "https://s3-eu-west-1.amazonaws.com/developer-application-test/images/2.jpg" }, { "product_ID" : "3", "name" : "Bananas", "price" : 88, "image" : "https://s3-eu-west-1.amazonaws.com/developer-application-test/images/3.jpg" },etc....
作为参考点,这是我的最终工作代码:
try{ httpClIEnt httpClIEnt = new DefaulthttpClIEnt(); URI uri = new URI("https", "s3-eu-west-1.amazonaws.com", "/developer-application-test/cart/List", null, null); httpGet httpGet = new httpGet(uri); httpResponse httpResponse = httpClIEnt.execute(httpGet); httpentity httpentity = httpResponse.getEntity(); if (httpentity != null) { inputStream inputStream = httpentity.getContent(); BufferedReader bufferedReader = new BufferedReader(new inputStreamReader(inputStream)); StringBuilder stringBuilder = new StringBuilder(); String currentline = null; while ((currentline = bufferedReader.readline()) != null) { stringBuilder.append(currentline + "\n"); } String result = stringBuilder.toString(); Log.v("http Request Results:",result); inputStream.close(); }}catch (Exception e){ e.printstacktrace();}
总结 以上是内存溢出为你收集整理的java.lang.IllegalArgumentException:在触发get请求时,主机名可能不为null全部内容,希望文章能够帮你解决java.lang.IllegalArgumentException:在触发get请求时,主机名可能不为null所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)