对于普通json
{
"text1":"going",
"text2":"sending"
}
我使用okhttp3
作为
RequestBody body = new MultipartBody.Builder()
.setType(MultipartBody.FORM)
.addFormDataPart("text1",xxx)
.addFormDataPart("text2","yyy")
.build();
我如何使用它来发送json之类的
{"Text1":"aaa","text2":[{"module":"bbb","Text":"xxx","params":{"lang": "eng"}}]}
答案 0 :(得分:0)
尝试一下
添加Gradle取决于compile 'com.squareup.okhttp3:okhttp:3.2.0'
public static JSONObject foo(String url, JSONObject json) {
JSONObject jsonObjectResp = null;
try {
MediaType JSON = MediaType.parse("application/json; charset=utf-8");
OkHttpClient client = new OkHttpClient();
okhttp3.RequestBody body = RequestBody.create(JSON, json.toString());
okhttp3.Request request = new okhttp3.Request.Builder()
.url(url)
.post(body)
.build();
okhttp3.Response response = client.newCall(request).execute();
String networkResp = response.body().string();
if (!networkResp.isEmpty()) {
jsonObjectResp = parseJSONStringToJSONObject(networkResp);
}
} catch (Exception ex) {
String err = String.format("{\"result\":\"false\",\"error\":\"%s\"}", ex.getMessage());
jsonObjectResp = parseJSONStringToJSONObject(err);
}
return jsonObjectResp;
}