So, back when I was using Koush's Ion, I was able to add a json body to my posts with a simple .setJsonObjectBody(json).asJsonObject()
I'm moving over to OkHttp, and I really don't see a good way to do that. I'm getting error 400's all over the place.
Anyone have any ideas?
I've even tried manually formatting it as a json string.
String reason = menuItem.getTitle().toString(); JsonObject json = new JsonObject(); json.addProperty("Reason", reason); String url = mBaseUrl + "/" + id + "/report"; Request request = new Request.Builder() .header("X-Client-Type", "Android") .url(url) .post(RequestBody .create(MediaType .parse("application/json"), "{\"Reason\": \"" + reason + "\"}" )) .build(); client.newCall(request).enqueue(new com.squareup.okhttp.Callback() { @Override public void onFailure(Request request, IOException throwable) { throwable.printStackTrace(); } @Override public void onResponse(Response response) throws IOException { if (!response.isSuccessful()) throw new IOException( "Unexpected code " + response); runOnUiThread(new Runnable() { @Override public void run() { Toast.makeText(context, "Report Received", Toast.LENGTH_SHORT).show(); } }); } }); /*Ion.with(getContext(), url) .setHeader("X-Client-Type", "Android") .setJsonObjectBody(json) .asJsonObject() .setCallback(new FutureCallback<JsonObject>() { @Override public void onCompleted(Exception e, JsonObject result) { Toast.makeText(context, "Report Received", Toast.LENGTH_SHORT).show(); } });*/
Create a MediaType variable named FORM: public static final MediaType FORM = MediaType. parse("multipart/form-data"); Create a RequestBody using the FORM variable and your unparsed params (String):
RequestBody requestBody = new MultipartBody. Builder() . setType(MultipartBody. FORM) .
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(); }
You can create your own JSONObject
then toString()
.
Remember run it in the background thread like doInBackground
in AsyncTask
.
OkHttp version > 4:
import okhttp3.MediaType.Companion.toMediaType // create your json here JSONObject jsonObject = new JSONObject(); try { jsonObject.put("KEY1", "VALUE1"); jsonObject.put("KEY2", "VALUE2"); } catch (JSONException e) { e.printStackTrace(); } val client = OkHttpClient() val mediaType = "application/json; charset=utf-8".toMediaType() val body = jsonObject.toString().toRequestBody(mediaType) val request: Request = Request.Builder() .url("https://YOUR_URL/") .post(body) .build() var response: Response? = null try { response = client.newCall(request).execute() val resStr = response.body!!.string() } catch (e: IOException) { e.printStackTrace() }
OkHttp version 3:
// create your json here JSONObject jsonObject = new JSONObject(); try { jsonObject.put("KEY1", "VALUE1"); jsonObject.put("KEY2", "VALUE2"); } catch (JSONException e) { e.printStackTrace(); } OkHttpClient client = new OkHttpClient(); MediaType JSON = MediaType.parse("application/json; charset=utf-8"); // put your json here RequestBody body = RequestBody.create(JSON, jsonObject.toString()); Request request = new Request.Builder() .url("https://YOUR_URL/") .post(body) .build(); Response response = null; try { response = client.newCall(request).execute(); String resStr = response.body().string(); } catch (IOException e) { e.printStackTrace(); }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With