-1

for normal json like

{
    "text1":"going",
    "text2":"sending"
}

i'm using okhttp3 as

RequestBody body = new MultipartBody.Builder()
                  .setType(MultipartBody.FORM)
                  .addFormDataPart("text1",xxx)
                  .addFormDataPart("text2","yyy")
                  .build();

how do i use it to send for jsons like

{"Text1":"aaa","text2":[{"module":"bbb","Text":"xxx","params":{"lang": "eng"}}]}
2

1 Answer 1

0

Try this

Add Gradle depends 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;
    }

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.