Android用截击发送JSON原始体的帖子

Android用截击发送JSON原始体的帖子,android,json,post,android-volley,Android,Json,Post,Android Volley,我必须用voley发送帖子,但当我尝试按请求发送原始正文时,而不是响应时,会出现此错误 ******com.android.volley.ServerError******:{“消息”:“未收到用于注册的用户帐户数据”。} 我在《邮递员》中也尝试了同样的方法,效果非常好,我如何在代码中修复它 在邮递员中工作的原始身体-> { "camp1": { "value": "value" }, "camp2": { "value": "va

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

******com.android.volley.ServerError******:{“消息”:“未收到用于注册的用户帐户数据”。}

我在《邮递员》中也尝试了同样的方法,效果非常好,我如何在代码中修复它

在邮递员中工作的原始身体->

    {
    "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;
                }
            });
        }
public void requestRegistrationInfo(@NonNull final String camp1,@NonNull final String camp2,final Listener){
RequestQueue RequestQueue=Volley.newRequestQueue(上下文);
添加(新的JsonObjectRequest(
Request.Method.POST,URL,
新的Response.Listener(){
@凌驾
公共void onResponse(JSONObject响应){
Log.v(“IT工作”);
listener.onSuccess();
}
},
新的Response.ErrorListener(){
@凌驾
公共无效onErrorResponse(截击错误){
Log.e(“*******”+error.toString()+“*******”,getErrorMessage(error));
onFailure();
}
})
{
@凌驾
受保护的映射getParams(){
Map Map=newhashmap();
map.put(“{camp1”,“value”);
地图出售(“camp2”、“价值”);
返回图;
}
@凌驾
公共映射getHeaders()引发AuthFailureError{
Map Map=newhashmap();
地图放置(“header1”、“header1”);
地图放置(“header2”、“header2”);
返回图;
}
});
}

如何才能正确发送原始json而不显示错误?

这是经过测试的代码,请尝试以下操作:

try {
RequestQueue requestQueue = Volley.newRequestQueue(this);
String URL = "http://...";
JSONObject jsonBody = new JSONObject();
jsonBody.put("Title", "Android Volley Demo");
jsonBody.put("Author", "BNK");
final String requestBody = jsonBody.toString();

StringRequest stringRequest = new StringRequest(Request.Method.POST, URL, new Response.Listener<String>() {
    @Override
    public void onResponse(String response) {
        Log.i("VOLLEY", response);
    }
}, new Response.ErrorListener() {
    @Override
    public void onErrorResponse(VolleyError error) {
        Log.e("VOLLEY", error.toString());
    }
}) {
    @Override
    public String getBodyContentType() {
        return "application/json; charset=utf-8";
    }

    @Override
    public byte[] getBody() throws AuthFailureError {
        try {
            return requestBody == null ? null : encodeParameters(requestBody , getParamsEncoding());
        } catch (UnsupportedEncodingException uee) {
            VolleyLog.wtf("Unsupported Encoding while trying to get the bytes of %s using %s", requestBody, "utf-8");
            return null;
        }
    }

    @Override
    protected Response<String> parseNetworkResponse(NetworkResponse response) {
        String responseString = "";
        if (response != null) {
            responseString = String.valueOf(response.statusCode);
            // can get more details such as response.headers
        }
        return Response.success(responseString, HttpHeaderParser.parseCacheHeaders(response));
    }
};

requestQueue.add(stringRequest);
} catch (JSONException e) {
 e.printStackTrace();
}
 private void multipartRequestWithVolly() {
        String urll = "your_url";

        progressDialog.show();
        StringRequest request = new StringRequest(Request.Method.POST, urll, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                progressDialog.dismiss();
                if (!TextUtils.isEmpty(response)) {
                    Log.e(TAG, "onResponse: " + response);
                    textView.setText(response);
                } else {
                    Log.e(TAG, "Response is null");
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                progressDialog.dismiss();
                Log.e(TAG, "onErrorResponse: " + error.toString());
            }
        }) {

            @Override
            protected Map<String, String> getParams() throws AuthFailureError {
                hashMap = new HashMap<>();
                hashMap.put("OPERATIONNAME", "bplan");
                hashMap.put("mcode", "298225816992");
                hashMap.put("deviceid", "dfb462ac78317846");
                hashMap.put("loginip", "192.168.1.101");
                hashMap.put("operatorid", "AT");
                hashMap.put("circleid", "19");
                return hashMap;
            }
        };
        AppController.getInstance().addToRequestQueue(request);
    }
private void multipartRequestWithVolly(){
字符串urll=“您的url”;
progressDialog.show();
StringRequest=newStringRequest(request.Method.POST、urll、new Response.Listener()){
@凌驾
公共void onResponse(字符串响应){
progressDialog.disclose();
如果(!TextUtils.isEmpty(响应)){
Log.e(标签“onResponse:”+响应);
setText(响应);
}否则{
Log.e(标记“响应为空”);
}
}
},new Response.ErrorListener(){
@凌驾
公共无效onErrorResponse(截击错误){
progressDialog.disclose();
Log.e(标记“onErrorResponse:+error.toString());
}
}) {
@凌驾
受保护的映射getParams()引发AuthFailureError{
hashMap=新的hashMap();
hashMap.put(“操作名”、“bplan”);
hashMap.put(“mcode”,“298225816992”);
hashMap.put(“设备ID”、“dfb462ac78317846”);
hashMap.put(“loginip”,“192.168.1.101”);
hashMap.put(“operatorid”、“AT”);
hashMap.put(“圈”、“19”);
返回hashMap;
}
};
AppController.getInstance().addToRequestQueue(请求);
}

在正常情况下,JSONObject请求没有命中getParams()方法,此方法仅适用于字符串请求和传递键值对数据负载。如果要传递包含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();

                            }


                        });
JSONObject JSONObject=new JSONObject();
jsonObject.put(“值”、“值1”);
JSONObject JSONObject 1=新的JSONObject();
jsonObject1.put(“value2”、“value2”);
JSONObject JSONObject 2=新的JSONObject();
jsonObject 2.put(“camp1”,jsonObject);
jsonObject2.put(“camp2”,jsonObject1);
//jsonObject2是服务器的有效负载,您可以在这里使用JsonObjectRequest
String url=“您的自定义url”;
JsonObjectRequest JsonObjectRequest=新JsonObjectRequest
(Request.Method.POST、url、jsonObject2、new com.android.volley.Response.Listener(){
@凌驾
公共void onResponse(JSONObject响应){
试一试{
//TODO:在这里处理您的响应
}
捕获(例外e){
e、 printStackTrace();
}
系统输出打印(响应);
}
},new com.android.volley.Response.ErrorListener(){
@凌驾
公共无效onErrorResponse(截击错误){
//TODO:句柄错误
错误。printStackTrace();
}
});

JsonObjectRequest将在其构造函数中的url参数后接受作为json的有效负载。如果调用任何REST-API,我们将传递数据,然后注意,该有效负载始终为json格式。因此,您可以像这样对有效负载使用对象体

HashMap<String, String> params = new HashMap<String, String>();
params.put("username", input_loginId.getText().toString());
params.put("password", input_password.getText().toString());
HashMap params=newhashmap();
参数put(“用户名”,输入_loginId.getText().toString());
参数put(“password”,输入_password.getText().toString());
你可以这样传递这个方法

JsonObjectRequest logInAPIRequest = new JsonObjectRequest(Request.Method.POST, YOUR-URL,
                         new JSONObject(params), new Response.Listener<JSONObject>() {
 @Override
                     public void onResponse(JSONObject response) {    
                         input_errorText.setText(response.toString());
                     }
                 }, new Response.ErrorListener() {
                     @Override
                     public void onErrorResponse(VolleyError error) {
                         input_errorText.setText("Error: " + error.getMessage());
                     }
                 });
JsonObjectRequest logInAPIRequest=新的jsonobjectrequues
JsonObjectRequest logInAPIRequest = new JsonObjectRequest(Request.Method.POST, YOUR-URL,
                         new JSONObject(params), new Response.Listener<JSONObject>() {
 @Override
                     public void onResponse(JSONObject response) {    
                         input_errorText.setText(response.toString());
                     }
                 }, new Response.ErrorListener() {
                     @Override
                     public void onErrorResponse(VolleyError error) {
                         input_errorText.setText("Error: " + error.getMessage());
                     }
                 });