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();}
You can create your own JSONObject
then toString()
.
Remember run it in the background thread like doInBackground
in AsyncTask
.
OkHttp version > 4:
// create your json hereJSONObject 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? = nulltry { response = client.newCall(request).execute() val resStr = response.body!!.string()} catch (e: IOException) { e.printStackTrace()}
OkHttp version 3:
// create your json hereJSONObject 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(); }
Another approach is by using FormBody.Builder()
.
Here's an example of callback:
Callback loginCallback = new Callback() { @Override public void onFailure(Call call, IOException e) { try { Log.i(TAG, "login failed: " + call.execute().code()); } catch (IOException e1) { e1.printStackTrace(); } } @Override public void onResponse(Call call, Response response) throws IOException { // String loginResponseString = response.body().string(); try { JSONObject responseObj = new JSONObject(response.body().string()); Log.i(TAG, "responseObj: " + responseObj); } catch (JSONException e) { e.printStackTrace(); } // Log.i(TAG, "loginResponseString: " + loginResponseString); }};
Then, we create our own body:
RequestBody formBody = new FormBody.Builder() .add("username", userName) .add("password", password) .add("customCredential", "") .add("isPersistent", "true") .add("setCookie", "true") .build();OkHttpClient client = new OkHttpClient.Builder() .addInterceptor(this) .build();Request request = new Request.Builder() .url(loginUrl) .post(formBody) .build();
Finally, we call the server:
client.newCall(request).enqueue(loginCallback);