Java Android , HttpGet error - Host name may not b

2019-08-27 01:00发布

问题:

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.

回答1:

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.



回答2:

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.



回答3:

Well there was no Java or Android problem, it results that the server changes the request when it comes from a device...



回答4:

Please try a host name without the use of hyphens "-" or underscores "_", see if it works .