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

前端 未结 1 357
猫巷女王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<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 .

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