简体   繁体   English

Java Android,HttpGet错误-主机名不能为null

[英]Java Android , HttpGet error - Host name may not be null

I am using HttpGet to get the contents of some images. 我正在使用HttpGet来获取某些图像的内容。 It has worked well but now it gives me errors with a new server I am using which returns images on the form of: 它运行良好,但是现在我使用的新服务器给我错误,该服务器以以下形式返回图像:

https://amx-sfs.qpass.com/d/da/207b3f6f-4fda-4faf-b7ae-a186530e1245 https://amx-sfs.qpass.com/d/da/207b3f6f-4fda-4faf-b7ae-a186530e1245

The error is Host name may not be null 错误是主机名不能为空

I have researched and it seems that the error is common when underscores appear on the URL, however my URL only contains hyphen which i think is accepted by URI. 我已经研究过,当URL上出现下划线时,错误似乎很常见,但是我的URL仅包含连字符,我认为该字符已被URI接受。 Anyway i also tried using the following to escape the url with no success: 无论如何,我也尝试使用以下命令来成功逃脱网址:

HttpGet mHttpGet = new HttpGet(new URI(null,item.getImageUrl(),null));

What else can be causing the problem and how can i solve it? 还有什么可能导致该问题,我该如何解决?

Thanks in advance 提前致谢

EDIT 编辑

I first tried directly with the Url using 我首先尝试使用

new HttpGet(item.getImageUrl())

Then I also tried with 4 args: 然后我还尝试了4个参数:

HttpGet mHttpGet = new HttpGet(new URI("https","amx-sfs.qpass.com",item.getImageUrl().substring(25),null));

With the same result, even if I debug the HttpGet Object, and watch the URI object it has, the host property appears ok (amx-sfs.qpass.com) , but I still get the same exception. 以相同的结果,即使我调试HttpGet对象并观察它具有的URI对象,host属性也可以正常显示(amx-sfs.qpass.com),但是我仍然会遇到相同的异常。

Did you encode URI? 您是否编码了URI? For example remove spaces or other special characters that break stuff? 例如删除空格或其他破坏字符的特殊字符? Try android.net.Uri.encode(String s) 尝试android.net.Uri.encode(String s)

HttpGet mHttpGet = new HttpGet(new URI(null,android.net.Uri.encode(item.getImageUrl()),null)); HttpGet mHttpGet = new HttpGet(新URI(null,android.net.Uri.encode(item.getImageUrl()),null));

Best way to debug this to always print the URL and analyze the ones that give an error. 调试此错误的最佳方法是始终打印URL并分析给出错误的URL。

I think you are using the wrong constructor. 我认为您使用的是错误的构造函数。 The three argument URI constructor has this signature: URI构造函数的三个参数具有以下签名:

public URI(String scheme, String ssp, String fragment)

but you are providing a URL as the 2nd argument rather than an SSP. 但您提供的是网址作为第二个参数,而不是SSP。 I think you should be using the one argument constructor that takes a URL string as its argument. 我认为您应该使用一个以URL字符串作为参数的参数构造函数。

好吧,这没有Java或Android问题,它导致服务器在来自设备的情况下更改了请求...

请尝试使用不带连字符“-”或下划线“ _”的主机名,看看它是否有效。

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

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