【问题标题】:Unexpected host with OkHttp使用 OkHttp 的意外主机
【发布时间】:2017-07-22 11:02:57
【问题描述】:

我正在尝试通过获取请求发送查询,问题是我不断收到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();

感谢大家的帮助:)

【问题讨论】:

  • 你在使用 wamp 服务器吗?
  • @Akhilesh Patil nope,使用 docker,我知道该 url 是正确的,因为我从那里得到了信息,没有任何查询。
  • 你用的是模拟器还是设备?
  • 对不起,我应该补充一下,特别是我正在使用 nginx xD
  • 我正在使用模拟器

标签: android get okhttp


【解决方案1】:

您的问题是 .host(string) 方法只需要 host 部分的 url。删除路径段将起作用。您的代码应如下所示:

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();

【讨论】:

    【解决方案2】:

    我尝试了这段代码,它成功了

    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();
    

    所以,您的主机有问题。请在 Postman 上测试您的主机或为其打开新端口。我也ping那个主机

    【讨论】:

      【解决方案3】:

      HttpUrl 有一个解析字符串的 parse 方法,我发现它要短得多。

      HttpUrl url = HttpUrl.parse("http://10.0.2.2/api/7")
                           .addQueryParameter("lat", deviceLat[0])
                           .addQueryParameter("long", deviceLong[0])
                           .build();
      

      【讨论】:

        【解决方案4】:

        所以我不知道我无法构建 URL 的问题是什么,但是第二次我手动在字符串中创建了 url,它工作正常,一切都没有问题。

        基本上我是从这个改过来的

        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();
        

        到这里

        Request request = new Request.Builder()
                                        .url("10.0.2.2" + "/api/" + 7 + "?long=" + deviceLong[0] + "&lat=" + deviceLat[0])
                                        .build();
        

        效果很好

        【讨论】:

        • 主机不应该包含/API/7,这是路径。
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2015-02-25
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-07-25
        • 2017-11-12
        相关资源
        最近更新 更多