Android 使用 volley 发送 JSON 原始正文的 POST

标签 android json post android-volley

我必须用 voley 发送帖子,但是当我尝试按请求发送原始正文,而不是响应时,会出现此错误

******com.android.volley.ServerError******: {"message":"No user account data for registration received."}

我在 postman 中尝试了相同的方法,它工作完美,我如何在我的代码中修复它?

在 postman 中工作的原始主体 ->

    {
    "camp1": {
        "value": "value"
    },
    "camp2": {
        "value": "value2"
    }
}

这就是我的代码中的内容 ->

    public void requestRegistrationInfo(@NonNull final String camp1, @NonNull final String camp2,final Listener listener) {
            RequestQueue requestQueue = Volley.newRequestQueue(context);
            requestQueue.add(new JsonObjectRequest(
                    Request.Method.POST, URL,
                    new Response.Listener<JSONObject>() {
                        @Override
                        public void onResponse(JSONObject response) {
                            Log.v("IT WORK");
                            listener.onSuccess();
                        }
                    },
                    new Response.ErrorListener() {
                        @Override
                        public void onErrorResponse(VolleyError error) {
                            Log.e("******" + error.toString() + "******", getErrorMessage(error));
                            listener.onFailure();
                        }
                    })
{

                @Override
                protected Map<String,String> getParams() {

                    Map<String, String> map = new HashMap<>();
                    map.put("{camp1", "value");
                    map.put("camp2", "value");

                    return map;
                }

                @Override
                public Map<String, String> getHeaders() throws AuthFailureError {
                    Map<String, String> map = new HashMap<>();
                    map.put("header1", "header1");
                    map.put("header2", "header2");
                    return map;
                }
            });
        }

我该怎么做才能正确发送原始 json 并且不显示错误?

最佳答案

正常情况下 JSONObject 请求不会命中 getParams() 方法,该方法仅适用于 String 请求并传递键值对数据负载。如果您想传递带有 JSON 数据的原始正文,首先您必须将数据格式化为服务器接受的格式。 在您的情况下,这是您的数据

{
  "camp1":{
   "value":"value1"
  },
  "camp2":{
    "value2":"value2"
  }
}

您必须将数据转换为服务器接受的 JSON 格式,如下所示

                JSONObject jsonObject = new JSONObject();
                jsonObject.put("value", "value1");
                JSONObject jsonObject1 = new JSONObject();
                jsonObject1.put("value2", "value2");
                JSONObject jsonObject2 = new JSONObject();
                jsonObject2.put("camp1", jsonObject);
                jsonObject2.put("camp2",jsonObject1);

 //jsonObject2 is the payload to server here you can use JsonObjectRequest 

 String url="your custom url";

 JsonObjectRequest jsonObjectRequest = new JsonObjectRequest
                        (Request.Method.POST,url, jsonObject2, new com.android.volley.Response.Listener<JSONObject>() {

                            @Override
                            public void onResponse(JSONObject response) {

                                try {
                                   //TODO: Handle your response here
                                }
                                catch (Exception e){
                                    e.printStackTrace();
                                }
                                System.out.print(response);

                            }
                        }, new com.android.volley.Response.ErrorListener() {

                            @Override
                            public void onErrorResponse(VolleyError error) {
                                // TODO: Handle error
                                error.printStackTrace();

                            }


                        });

JsonObjectRequest 将在我们将传递数据的 url 参数之后在其构造函数中接受 json 形式的有效负载

关于Android 使用 volley 发送 JSON 原始正文的 POST,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43184769/

相关文章:

javascript - 使用 React.js 更新 JSON 数组

android - 绑定(bind)远程服务 (AIDL)

android - 在 onSaveInstanceState 中保存对象列表

java - admob 插页式按钮按下后立即显示?

android - 在 android 中使用 Volley 从 JSON 下载大数据

java - 使用 HttpClient 发布文件内容?

android - Android、Windows Phone 和 iOS 可以使用的内部和外部数据库有哪些?

javascript - 如何在 json 负载中格式化 Map<Entity,integer>

php - 如何显示来自特定类别的一个特定 WP 帖子(提取的 ID)

ruby-on-rails - 如何同时以 GET 和 POST 方式传递值?