Android:如何在Volley中调用put方法?

时间:2018-05-04 11:55:23

标签: java android web-services android-volley postman

当我在邮递员上调用URL时,它会返回预期的结果。我使用Volley使用PUT方法,但它不起作用。

StringRequest reg=new StringRequest(Request.Method.PUT, AppConfig.Registration, new Response.Listener<String>() {
    @Override
    public void onResponse(String response) {
    Log.d("USER_REG",response);
    }
}, new Response.ErrorListener() {
    @Override
    public void onErrorResponse(VolleyError error) {
        Log.d("USER_REG","-------------"+error);
    }
}){
    @Override
    protected Map<String,String> getParams(){
        Map<String,String> params = new HashMap<String, String>();
        params.put(name1,Name);
        params.put(client_id1,CID);
        params.put(email1,Email);
        params.put(mobile1,Mobile);
        params.put(password1,Password);
        params.put(device_id1,"123456");
        return params;
    }

};
RequestQueue requestQueue = Volley.newRequestQueue(this);
requestQueue.add(reg);

这是邮递员输出。

enter image description here

1 个答案:

答案 0 :(得分:0)

目前,您使用Volley作为路径参数从Android发送数据,这些参数应作为JSON发送,如下所示。

RequestQueue queue = Volley.newRequestQueue(this);

private void makeJsonObjReq() {
    Map<String, String> postParam= new HashMap<String, String>();
    postParam.put(name1,Name);
    postParam.put(client_id1,CID);
    postParam.put(email1,Email);
    postParam.put(mobile1,Mobile);
    postParam.put(password1,Password);
    postParam.put(device_id1,"123456");

    JsonObjectRequest jsonObjReq = new JsonObjectRequest(Method.PUT,
        Const.YOUR_URL, new JSONObject(postParam),
        new Response.Listener<JSONObject>() {

            @Override
            public void onResponse(JSONObject response) {
                Log.d(TAG, response.toString());
                msgResponse.setText(response.toString());
            }
        }, new Response.ErrorListener() {

            @Override
            public void onErrorResponse(VolleyError error) {
                VolleyLog.d(TAG, "Error: " + error.getMessage());
            }
        }) {

    @Override
    public Map<String, String> getHeaders() throws AuthFailureError {
        HashMap<String, String> headers = new HashMap<String, String>();
        headers.put("Content-Type", "application/json; charset=utf-8");
        return headers;
    }
};

jsonObjReq.setTag(TAG);
queue.add(jsonObjReq);

您还需要在标头中添加Content-Type application/json