Okhttp Post Body as JSON

OkHttp Post Body as JSON

Just use JSONObject.toString(); method.
And have a look at OkHttp's tutorial:

public static final MediaType JSON
= MediaType.parse("application/json; charset=utf-8");

OkHttpClient client = new OkHttpClient();

String post(String url, String json) throws IOException {
RequestBody body = RequestBody.create(json, JSON); // new
// RequestBody body = RequestBody.create(JSON, json); // old
Request request = new Request.Builder()
.url(url)
.post(body)
.build();
Response response = client.newCall(request).execute();
return response.body().string();
}

Send JSON in request OkHttp

The easiest way to map and serialize your object in JSON format is to use the ObjectMapper class of jackson-databind library.

I personally use it to implement integration tests of RestControllers and it works very well. Here is the utility class I realized, you can take it and use it for your purposes:

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

public final class JsonUtils {

public static String json(Object obj) throws JsonProcessingException {
ObjectMapper objectMapper = new ObjectMapper();
return objectMapper.writeValueAsString(obj);
}
}

What you need to have is a POJO class which implements Serializable, and then pass the instance of your class to the json method and it will return the JSON format.

You can definitely use it for Android projects. I found many examples where you can add the dependency, but it depends whether you use Gradle or Maven.

Try that out!!!

Sending JSON body through POST request in OKhttp in Android

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;
}

Parse Response

   private static JSONObject parseJSONStringToJSONObject(final String strr) {

JSONObject response = null;
try {
response = new JSONObject(strr);
} catch (Exception ex) {
// Log.e("Could not parse malformed JSON: \"" + json + "\"");
try {
response = new JSONObject();
response.put("result", "failed");
response.put("data", strr);
response.put("error", ex.getMessage());
} catch (Exception exx) {
}
}
return response;
}

How to POST JSON data using OkHttp

The payload is in request.data so this should be parsed.

JSONParser().parse(request.data)

Make a post request with okHttp

If the response is sent in the body you can get it with:

response.body().string();

You just had to look on the documentation

¡Salud!

How to post form.body in JSONArray using OkHttp?

You can try adding a JSONObject in an array and then pass it to your JSONArray.

Something like this:

JSONArray list = new JSONArray();

JSONObject js = new JSONObject();
try {
js.put("name", "john");
} catch (JSONException e) {
e.printStackTrace();
}
list.put(js);

js = new JSONObject();
try {
js.put("food", "burger");
} catch (JSONException e) {
e.printStackTrace();
}
list.put(js);

Log.e(TAG, "found json array " + list);

and if you have more items, then run the `js' in a loop until you are done with it.



Related Topics



Leave a reply



Submit