使用
FormBody body = new FormBody.Builder()
時,headers中的
"Content-Type","application/x-www-form-urlencoded;"
設置不了,導致有些參數請求異常返回
使用以下方式即可,該問題非常坑人,找了好久的問題,最后抓包一行一行看才發現headers設置未生效。
public static final MediaType JSON = MediaType.get("application/json; charset=utf-8"); OkHttpClient client = new OkHttpClient(); String post(String url, String json) throws IOException { RequestBody body = RequestBody.create(JSON, json); Request request = new Request.Builder() .url(url) .post(body) .build(); try (Response response = client.newCall(request).execute()) { return response.body().string(); } }
這樣使用即可