简体   繁体   中英

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

I am using HttpGet to get the contents of some images. 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

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. 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:

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.

Did you encode URI? For example remove spaces or other special characters that break stuff? Try android.net.Uri.encode(String s)

HttpGet mHttpGet = new HttpGet(new 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.

I think you are using the wrong constructor. The three argument URI constructor has this signature:

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

but you are providing a URL as the 2nd argument rather than an SSP. I think you should be using the one argument constructor that takes a URL string as its argument.

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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