如何在请求中使用OkHttp的HttpUrl?

时间:2015-11-21 10:54:27

标签: java okhttp

我有一个http网址:

HttpUrl httpurl = new HttpUrl.Builder()
.scheme("https")
.host("www.google.com")
.addPathSegment("search")
.addQueryParameter("q", "polar bears")
.build();

如何在请求中使用它?也就是说,如何向httpurl

构建的网址发送请求

2 个答案:

答案 0 :(得分:2)

使用Request.Builder#url(HttpUrl)

Request r = new Request.Builder()
  .url(httpurl).build();

然后按here

答案 1 :(得分:2)

试试这个:

Request request = new Request.Builder()
            .method(method, requestBody)
            .url(httpurl)
            .addHeader(headerName, headerValue);
            .build();

OkHttpClient client = new OkHttpClient();
client.newCall(request).enqueue(new Callback() {
    @Override
    public void onFailure(Request request, IOException e) {

    }

    @Override
    public void onResponse(Response response) throws IOException {

    }
});