Java 用JSON截击帖子-获取422状态代码
我试图使用发送一个JSON对象到使用MongoDB的环回应用程序API。我可以看到数据已成功保存在数据库中。但是截击反应总是显示状态码是正确的。有人能指出我做错了什么吗 这是错误消息:Java 用JSON截击帖子-获取422状态代码,java,android,json,android-volley,loopbackjs,Java,Android,Json,Android Volley,Loopbackjs,我试图使用发送一个JSON对象到使用MongoDB的环回应用程序API。我可以看到数据已成功保存在数据库中。但是截击反应总是显示状态码是正确的。有人能指出我做错了什么吗 这是错误消息: BasicNetwork.performRequest:的意外响应代码422http://localhost:3001/api/Persons 这是我试图发送的JSON: { "map_coordinates": { "lat": xxxxxx, "lng": xxxxxxx }, "
BasicNetwork.performRequest:的意外响应代码422http://localhost:3001/api/Persons
这是我试图发送的JSON:
{
"map_coordinates": {
"lat": xxxxxx,
"lng": xxxxxxx
},
"first_name": "xxxxx",
"middle_name": "",
"family_name": "xxxxxx",
"gender": "xxxxxx",
"role": "xxxxxx",
"dob": "2017-09-28T05:51:40.836Z",
"picture_of_person": "",
"username": "ajxxxx7@gmail.com",
"deviceToken": "xxxxxxxx",
"portal_address": "xxxxx",
"email": "ajxxxxx7@gmail.com",
"password": "xxxxxxx",
"vcode": 562991,
"mobile": "+91xxxxxxxx"
}
我认为JSON没有问题,因为如果我从环回浏览器发布相同的对象,那么请求将成功通过并返回状态代码200
这是我用来发送JSON请求的代码
public void postJsonRequest(String url, JSONObject data, final Callback<JSONObject, VolleyError> callback) {
JsonObjectRequest objectRequest = new JsonObjectRequest(Request.Method.POST,
url, data, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
callback.onSuccess(response);
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
callback.onFailure(error);
}
});
makeJsonRequest(objectRequest);
}
private void makeJsonRequest(JsonObjectRequest request) {
RequestQueue queue = Volley.newRequestQueue(EngageMyTime.getInstance().getEMTContext());
queue.add(request);
}
public void postJsonRequest(字符串url、JSONObject数据、最终回调){
JsonObjectRequest objectRequest=新的JsonObjectRequest(Request.Method.POST,
url,数据,新响应。侦听器(){
@凌驾
公共void onResponse(JSONObject响应){
callback.onSuccess(响应);
}
},new Response.ErrorListener(){
@凌驾
公共无效onErrorResponse(截击错误){
callback.onFailure(错误);
}
});
makeJsonRequest(objectRequest);
}
私有void makeJsonRequest(JsonObjectRequest请求){
RequestQueue=Volley.newRequestQueue(engageTimetime.getInstance().getEMTContext());
添加(请求);
}
如果您有任何帮助/想法,我们将不胜感激。您将使用什么格式来回应?如果您不能使用JSON,您可能会看到此错误。因为postJSONResponse在解析中使用JSONObject。像这样的。您可以编写自定义请求或将响应类型更改为JSON。 自定义请求的示例如下:
public class SignUpRequest extends Request<String> {
private Map<String, String> mParams;
public SignUpRequest(String url, Response.ErrorListener listener) {
super(url, listener);
}
public SignUpRequest(int method, String url,String email,String userName,String password, Response.ErrorListener listener) {
super(method, url, listener);
mParams=new HashMap<>();
mParams.put("username",userName);
mParams.put("email",email);
mParams.put("password",password);
}
@Override
protected void deliverResponse(String response) {
Log.e("Response",response);
}
@Override
protected Map<String, String> getParams() throws AuthFailureError {
return mParams;
}
@Override
protected Response<String> parseNetworkResponse(NetworkResponse response) {
String data=new String(response.data);
return Response.success(data+" "+response.statusCode,
HttpHeaderParser.parseCacheHeaders(response));
}
}
public类SignUpRequest扩展请求{
私人地图;
public SignUpRequest(字符串url,Response.ErrorListener侦听器){
超级(url,侦听器);
}
public SignUpRequest(int方法、字符串url、字符串电子邮件、字符串用户名、字符串密码、Response.ErrorListener){
super(方法、url、侦听器);
mParams=newhashmap();
mParams.put(“用户名”,用户名);
mParams.put(“电子邮件”,email);
mParams.put(“密码”,password);
}
@凌驾
受保护的void deliverResponse(字符串响应){
Log.e(“响应”,响应);
}
@凌驾
受保护的映射getParams()引发AuthFailureError{
返回内存;
}
@凌驾
受保护的响应parseNetworkResponse(NetworkResponse响应){
字符串数据=新字符串(response.data);
返回Response.success(数据+“”+Response.statusCode,
HttpHeaderParser.parseCacheHeaders(响应));
}
}
尝试在服务器端设置默认字符集UTF-8,并在发送之前在UTF-8中编码json数据
在android中使用
URLEncoder.encode(string,"UTF-8");
嘿,Ajil O你能帮我解决这个问题吗@Smith一会儿会看一看环回端的错误消息是什么?我已经做了。我认为谷歌的截击可能有问题。请求通过了Okhttp