简体   繁体   English

带有JSON的Volley Post-获取422状态代码

[英]Volley Post with JSON - getting 422 Status Code

I am trying to use Volley to send a JSON object to a Loopback application API which is using MongoDB. 我正在尝试使用Volley将JSON对象发送到正在使用MongoDB的回送应用程序API。 I can see that the data is being saved in the database successfully. 我可以看到数据已成功保存在数据库中。 But the Volley response always shows that the status code is 422 . 但是Volley响应始终显示状态码为422 Can someone point out what I am doing wrong? 有人可以指出我做错了吗?

This is the error message: 这是错误消息:

BasicNetwork.performRequest: Unexpected response code 422 for http://localhost:3001/api/Persons

This is the JSON I am trying to send: 这是我要发送的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"
}

I do not think that there is a problem with the JSON because, if I POST the same object from the Loopback explorer then the request goes through successfully and returns status code 200. 我认为JSON没有问题,因为如果我从回送资源管理器中发布相同的对象,则请求将成功通过并返回状态代码200。

This is the code I am using to send the JSON request. 这是我用来发送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);
}

Any help/idea would be appreciated. 任何帮助/想法,将不胜感激。

Try to set the default charset UTF-8 on server side and encode in UTF-8 the json data before send 尝试在服务器端设置默认字符集UTF-8,并在发送前以UTF-8编码json数据

In android with 在Android中

URLEncoder.encode(string,"UTF-8");

What format are you used in response? 您使用哪种格式进行回应? İf you can't use JSON,You may see this error. 如果您无法使用JSON,则可能会看到此错误。 Because postJSONResponse use JSONObject in parsing. 因为postJSONResponse在解析中使用JSONObject。 Something like this. 这样的事情。 You can write custom request or change your response type to JSON. 您可以编写自定义请求,也可以将响应类型更改为JSON。 Example of custom request is: 定制请求的示例是:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM