I am trying to send a query through a get request, the problem is I keep getting java.lang.IllegalArgumentException: unexpected host
HttpUrl url = new HttpUrl.Builder()
.scheme("http")
.host("10.0.2.2" + "/api/" + 7) // This is where the error is coming in
.addQueryParameter("lat", deviceLat[0])
.addQueryParameter("long", deviceLong[0])
.build();
Request request = new Request.Builder()
.url(url)
.build();
Thanks for all help :)
Your issue there is that the .host(string) method is expecting just the host part of the url. Removing the path segments will work. Your code should look like this:
HttpUrl url = new HttpUrl.Builder()
.scheme("http")
.host("10.0.2.2") //Just the host (like "google.com", not "google.com/api")
.addPathSegment("api")
.addPathSegment("7")
.addQueryParameter("lat", deviceLat[0])
.addQueryParameter("long", deviceLong[0])
.build();
Request request = new Request.Builder()
.url(url)
.build();
I try this code and it worked
HttpUrl url = new HttpUrl.Builder()
.scheme("https")
.host("www.google.com")
.addPathSegment("search")
.addQueryParameter("q", "polar bears")
.build();
Request request = new Request.Builder()
.url(url)
.build();
So, there is something wrong with your host. Please test your host on Postman or open new port for it. I also ping that host

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With