如何通过OkHttp将查询参数添加到HTTP GET请求?

时间:2015-05-09 16:53:49

标签: java http-get okhttp query-parameters

我使用的是最新的okhttp版本: okhttp-2.3.0.jar

如何在java中的okhttp中向GET请求添加查询参数?

我找到了一个关于android的related question,但这里没有答案!

7 个答案:

答案 0 :(得分:29)

对于okhttp3:

condition

答案 1 :(得分:19)

这是我的拦截器

    private static class AuthInterceptor implements Interceptor {

    private String mApiKey;

    public AuthInterceptor(String apiKey) {
        mApiKey = apiKey;
    }

    @Override
    public Response intercept(Chain chain) throws IOException {
        HttpUrl url = chain.request().httpUrl()
                .newBuilder()
                .addQueryParameter("api_key", mApiKey)
                .build();
        Request request = chain.request().newBuilder().url(url).build();
        return chain.proceed(request);
    }
}

答案 2 :(得分:9)

正如在另一个答案中所提到的,okhttp v2.4提供了新功能,可以实现这一目标。

请参阅http://square.github.io/okhttp/2.x/okhttp/com/squareup/okhttp/HttpUrl.Builder.html#addQueryParameter-java.lang.String-java.lang.String-

当前版本的okhttp there is no method provided that will handle this for you无法做到这一点。

接下来最好的事情是构建一个url字符串或URL对象(在java.net.URL中找到)并自己包含查询,并将其传递给okhttp的请求构建器。

enter image description here

如您所见,Request.Builder可以使用String或URL。

有关如何构建网址的示例,请访问What is the idiomatic way to compose a URL or URI in Java?

答案 3 :(得分:7)

我终于完成了我的代码,希望以下代码可以帮助你们。我首先使用

构建URL
  

HttpUrl httpUrl = new HttpUrl.Builder()

然后将网址传递给Request requesthttp希望它有所帮助。

public class NetActions {

    OkHttpClient client = new OkHttpClient();

    public String getStudentById(String code) throws IOException, NullPointerException {

        HttpUrl httpUrl = new HttpUrl.Builder()
                .scheme("https")
                .host("subdomain.apiweb.com")
                .addPathSegment("api")
                .addPathSegment("v1")
                .addPathSegment("students")
                .addPathSegment(code) // <- 8873 code passthru parameter on method
                .addQueryParameter("auth_token", "71x23768234hgjwqguygqew")
                // Each addPathSegment separated add a / symbol to the final url
                // finally my Full URL is: 
                // https://subdomain.apiweb.com/api/v1/students/8873?auth_token=71x23768234hgjwqguygqew
                .build();

        System.out.println(httpUrl.toString());

        Request requesthttp = new Request.Builder()
                .addHeader("accept", "application/json")
                .url(httpUrl) // <- Finally put httpUrl in here
                .build();

        Response response = client.newCall(requesthttp).execute();
        return response.body().string();
    }
}

答案 4 :(得分:4)

截至目前(okhttp 2.4),HttpUrl.Builder现在有方法addQueryParameter和addEncodedQueryParameter。

答案 5 :(得分:1)

您可以从现有的HttoUrl创建newBuilder并在那里添加查询参数。示例拦截器代码:

    Request req = it.request()
    return chain.proceed(
        req.newBuilder()
            .url(
                req.url().newBuilder()
                .addQueryParameter("v", "5.60")
                .build());
    .build());

答案 6 :(得分:-1)

使用HttpUrl类的函数:

//adds the pre-encoded query parameter to this URL's query string
addEncodedQueryParameter(String encodedName, String encodedValue)

//encodes the query parameter using UTF-8 and adds it to this URL's query string
addQueryParameter(String name, String value)

更详细:https://stackoverflow.com/a/32146909/5247331