How do I post post a JSONObject
request like below?
{
\"pObj\": [],
\"robj\": [
{
Change In your My APi Interface for Retrofit
public interface ApiInterface {
@Headers( "Content-Type: application/json; charset=utf-8")
@POST("re_clientdata")
Call<String> savePost(@Body RequestBody req);
}
Also change Mainactivity code like this
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
sendPost(jRequest);
}
public void sendPost(JSONObject requestobject) {
Log.e("requestobject",requestobject.toString());
RequestBody myreqbody = null;
try {
myreqbody = RequestBody.create(okhttp3.MediaType.parse("application/json; charset=utf-8"),
(new JSONObject(String.valueOf(requestobject))).toString());
} catch (JSONException e) {
e.printStackTrace();
}
Call<String> call =mAPIService.savePost(myreqbody);
call.enqueue(new Callback<String>() {
@Override
public void onResponse(Call<String>callback,Response<String>response) {
String res = response.body();
Log.e("DEMO", "post submitted to API." + response);
}
@Override
public void onFailure(Call<String> call, Throwable t) {
Log.e("DEMO", "Unable to submit post to API.",t);
Log.e("call", String.valueOf(call));
}
});
}
For more details check this answer suggested by Pratik Vyas .