2015-05-09 44 views

回答

7

正如其他答案中提到的,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可以是一個字符串或URL。關於如何建立一個URL

實例可以在What is the idiomatic way to compose a URL or URI in Java?

+2

我們正在添加一個新的HttpUrl類,可以在下一個版本中執行此操作。 –

+0

@JesseWilson這是一個不錯的改進:-) –

+0

網址被破壞.. –

4

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

0

使用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

15

這裏是我的截擊

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); 
    } 
} 
+3

我使用OkHttp 3.0.1,並且獲取'HttpUr'的方法是'.url()'而不是' .httpUrl()'。感謝您的上課。 –

0

您可以從現有的HttoUrl創建newBuilder並添加查詢參數存在。示例代碼攔截器:

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

對於okhttp3:

private static final OkHttpClient client = new OkHttpClient().newBuilder() 
    .connectTimeout(10, TimeUnit.SECONDS) 
    .readTimeout(30, TimeUnit.SECONDS) 
    .build(); 

public static void get(String url, Map<String,String>params, Callback responseCallback) { 
    HttpUrl.Builder httpBuider = HttpUrl.parse(url).newBuilder(); 
    if (params != null) { 
     for(Map.Entry<String, String> param : params.entrySet()) { 
      httpBuider.addQueryParameter(param.getKey(),param.getValue()); 
     } 
    } 
    Request request = new Request.Builder().url(httpBuider.build()).build(); 
    client.newCall(request).enqueue(responseCallback); 
} 
+0

這正是我需要的。謝謝! –

+0

@harsh_v,很樂意幫忙;) –

1

我終於做到我的代碼,希望下面的代碼可以幫助你們。我先建使用

HttpUrl httpUrl = new HttpUrl.Builder()

然後將URL傳遞Request requesthttp希望它可以幫助的URL。

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(); 
    } 
} 
相關問題