如何获得"nameValuePairs"在使用翻新的JSON请求中? [英] How do I get "nameValuePairs" in a JSON request using Retrofit?

查看:70
本文介绍了如何获得"nameValuePairs"在使用翻新的JSON请求中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何发布如下所示的JSONObject请求?

How do I post post a JSONObject request like below?

    {
        "pObj": [],
        "robj": [
            {
                "l_index": "1",
                "user_id": "111",
                "vername": "1",
                "fcmtoken": "ghkghkhkh"
            }
        ],
        "uobject": [],
        "pname": "y6y68uuy7"
    }

在Volley中,我可以成功发布JSONObject请求.但是,当我在Retrofit中使用它时,我的请求将如下更改:

In Volley I can successfully post JSONObject request. But when I am using this in Retrofit my request is changed as below:

  {
        "nameValuePairs": {
            "pObj": {
                "values": []
            },
            "robj": {
                "values": [
                    {
                        "nameValuePairs": {
                            "l_index": "1",
                            "user_id": "111",
                            "vername": "1",
                            "fcmtoken": "ghkghkhkh"
                        }
                    }
                ]
            },
            "uobject": {
                "values": []
            },
                    "pname": "y6y68uuy7"
        }
    }

我收到来自服务器端的错误请求.

And I am getting bad request from server side.

public interface ApiInterface {

    @Headers( "Content-Type: application/json; charset=utf-8")
    @POST("re_clientdata")

    Call<String> savePost(@Body JSONObject  req);
}

我的APIClient

public class ApiClient {

    private static Retrofit retrofit = null;

    public static Retrofit getClient(String baseUrl) {
        HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
        interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
        OkHttpClient client = new OkHttpClient.Builder()
                .addInterceptor(interceptor).build();

        if (retrofit==null) {
            retrofit = new Retrofit.Builder()
                    .baseUrl(baseUrl)
                    .client(client)
                    .addConverterFactory(GsonConverterFactory.create())
                    .build();
        }
        return retrofit;
    }
}

MainActivity代码:

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());

    Call<String> call =mAPIService.savePost(requestobject);
    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));
        }
    });

}

注意:我也尝试使用HashMap,但会自动添加相同的nameValuePairs参数.如果我使用Gson JsonObject,则请求在序列化中进行转换.所以我不想在服务器端进行任何更改,因为使用Volley可以正常工作.但是现在我想通过相同的请求使用Retrofit.

Note: I also tried using HashMap but same nameValuePairs parameter added automatically. And If I use Gson JsonObject then request convert in serialize. So I don't want to any change in server side cause it's works fine using Volley. But now I want to use Retrofit using same request.

推荐答案

更改My APi界面以进行改装

public interface ApiInterface {

    @Headers( "Content-Type: application/json; charset=utf-8")
    @POST("re_clientdata")


    Call<String> savePost(@Body RequestBody req);
}

也可以这样更改主要活动代码

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));
                }
            });

        }

有关更多详细信息,请检查此 答案由以下人员建议 Pratik Vyas .

For more details check this answer suggested by Pratik Vyas .

这篇关于如何获得"nameValuePairs"在使用翻新的JSON请求中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆