我正在處理一個API,我需要在POST請求中提供JSON數組作爲查詢參數。報價必須在網址中,但OkHttp和HttpURL不斷將其轉換爲%22
。該API不會識別這個並返回一個錯誤代碼。如何讓OkHttp保留請求中的引用。我處理看起來像這樣的代碼:如何使用OkHttp保留雙引號
public static void post(String url, String[][] requestParams, Callback callback) {
HttpUrl httpUrl = addParamsToURL(getAbsoluteUrl(url), requestParams);
Request request = new Request.Builder()
.url(httpUrl)
.addHeader("app", Prefs.getString(Constants.HEADER_APP, null))
.addHeader("authoToken", Prefs.getString(Constants.HEADER_AUTH_TOKEN, null))
.post(RequestBody.create(MediaType.parse("application/json"), ""))
.build();
okHttpClient.newCall(request).enqueue(callback);
}
private static HttpUrl addParamsToURL(String url, String[][] requestParams) {
HttpUrl httpUrl = HttpUrl.parse(url);
HttpUrl.Builder urlBuilder = httpUrl.newBuilder();
for(int i = 0, size = requestParams.length; i < size; i++) {
urlBuilder.addEncodedQueryParameter(requestParams[i][0], requestParams[i][1]);
}
return urlBuilder.build();
}
我需要的URL的請求轉到看起來像https://www.baseurl.com/path?queryName=["[email protected]"]
而是它看起來像https://www.baseurl.com/path/?queryName=[%[email protected]%22]
它進一步證明,問題是_not_的雙引號,但是是一個錯字了線。不過,我知道這對許多服務器設置不佳的人來說是個問題,所以我正在選擇你的答案。 – drawinfinity