How do I get “nameValuePairs” in a JSON request using Retrofit?

前端 未结 1 358
猫巷女王i
猫巷女王i 2020-12-18 07:26

How do I post post a JSONObject request like below?

Original REQUEST:

    {
        \"pObj\": [],
        \"robj\": [
            {
           


        
1条回答
  •  既然无缘
    2020-12-18 07:49

    Change In your My APi Interface for Retrofit

    public interface ApiInterface {
    
        @Headers( "Content-Type: application/json; charset=utf-8")
        @POST("re_clientdata")
    
    
        Call 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 call =mAPIService.savePost(myreqbody);
    
    
                call.enqueue(new Callback() {
                    @Override
                    public void onResponse(Callcallback,Responseresponse) {
                        String res = response.body();
                        Log.e("DEMO", "post submitted to API." + response);
                    }
                    @Override
                    public void onFailure(Call 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 .

    0 讨论(0)
提交回复
热议问题