5

嗨,我正在使用Volley我的登录页面。我需要以这种方式传递数据

{
userID : '-988682425884628921',
email :'aditya@vyas.com',
passwd : '123ss'
}

我正在使用 POST 方法发送数据,我已经检查了 DHC,Api 在 DHC 中工作正常,我得到JSON 响应,但是当我尝试使用 Volley 时,我无法得到响应。我的logcat中甚至没有任何错误。

JAVA代码

 RequestQueue mVolleyQueue = Volley.newRequestQueue(this);

    CustomRequest req = new CustomRequest(Request.Method.POST,url,null,
            new Response.Listener<JSONObject>() {
                @Override
                public void onResponse(JSONObject response) {
                    Log.v("tag","login response " + response);
                }
            }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            Log.v("tag","login error response " + error.getMessage());
        }
    }){
        @Override
        public Map<String, String> getParams() throws AuthFailureError {
            Map<String, String>  params = new HashMap<String, String>();

            params.put("userID", "-988682425884628921");
            params.put("email", "aditya@vyas.com");
            params.put("passwd", "123ss");
            return params;
        }


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

错误

05-28 09:20:14.696 2450-2468/com.android.msahakyan.expandablenavigationdrawer E/tag﹕ parseNetworkError is ! null
05-28 09:20:14.697 2450-2468/com.android.msahakyan.expandablenavigationdrawer E/tag﹕ parseNetworkError status code : 400
05-28 09:20:14.697 2450-2468/com.android.msahakyan.expandablenavigationdrawer E/tag﹕ parseNetworkError message : <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN""http://www.w3.org/TR/html4/strict.dtd">
<HTML><HEAD><TITLE>Bad Request</TITLE>
<META HTTP-EQUIV="Content-Type" Content="text/html; charset=us-ascii"></HEAD>
<BODY><h2>Bad Request - Invalid Header</h2>
<hr><p>HTTP Error 400. The request has an invalid header name.</p>
</BODY></HTML>


    }
4

5 回答 5

6

解决了你的问题。刚刚使用JsonArrayRequest并以形式传递参数JsonObject

    Map<String, String> params = new HashMap<String, String>();
    params.put("userID", "userid");
    params.put("email","email");
    params.put("passwd", "password");
    JsonArrayRequest request = new JsonArrayRequest(Request.Method.POST, "url", new JSONObject(params),
            new Response.Listener<JSONArray>() {
                @Override
                public void onResponse(JSONArray response) {
                    System.out.println("response -->> " + response.toString());
                }
            },
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    System.out.println("change Pass response -->> " + error.toString());
                }
            });

    request.setRetryPolicy(new

            DefaultRetryPolicy(60000,
            DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
            DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));


    Volley.newRequestQueue(activity).add(request);

无需覆盖getParams()getHeaders().

问题: 1 您收到响应代码 500,因为服务器正在接受paramsasJsonObject并且我们正在尝试馈送String

问题:2 您正在使用JsonObjectRequet但来自服务器的响应在,JsonArray所以您需要使用JsonArrayRequest来接受响应JsonArray

试着让我知道这有没有帮助:)

于 2016-05-28T04:56:54.280 回答
4

我有一个类似的问题。我在 getHeaders 方法中覆盖了 Content-Type:

headers.put("Content-Type", "application/json; charset=UTF-8");

但是 Volley 本身添加了一个 Content-Type 参数,所以有两个这样的参数。删除行解决了我的问题。

于 2016-12-12T17:30:07.983 回答
1

您需要覆盖 protected Map<String, String> getParams()以在 POST 中传递参数。

 JsonObjectRequest jsonObjReq = new JsonObjectRequest(Request.Method.POST,
            url, params,
            new Response.Listener<JSONObject>() {

                @Override
                public void onResponse(JSONObject response) {
                    Log.d("Login Response", response.toString());


                   hidepDialog();
                }
            }, new Response.ErrorListener() {

        @Override
        public void onErrorResponse(VolleyError error) {

            System.out.println(error.getStackTrace());
            VolleyLog.d("ErrorVolley", "Error: " + error.getStackTrace());
            hidepDialog();
        }
    }) {

            @Override
            protected Map<String, String> getParams() {
                Map<String, String> params = new HashMap<String, String>();
                params.put("userID", "2"));
                params.put("email","aa@a.kl");
                params.put("passwd", "ddddd");

                return params;
            }
        };

于 2016-05-27T10:46:19.330 回答
0

当心内容类型标头。Volley 处理它与其他标题不同,它没有显示在 Map<> 标题中。而不是覆盖 getHeaders() 方法来设置内容类型,你应该覆盖 getBodyContentType() 像这样:

@Override
public String getBodyContentType()
{
    return "application/json; charset=utf-8";
}
于 2017-08-10T14:44:10.287 回答
0

我有同样的问题使用这个:

        StringRequest stringRequest = new StringRequest(Request.Method.POST, URL,
            new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {
                    Toast.makeText(LoginActivity.this, response, Toast.LENGTH_LONG).show();

                }
            },
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    Toast.makeText(LoginActivity.this, error.toString(), Toast.LENGTH_LONG).show();

                }
            }) {

        @Override
        public Map<String, String> getParams()  {
            HashMap<String, String> headers = new HashMap<String, String>();
            headers.put("Content-Type", "application/x-www-form-urlencoded; charset=utf-8");
            headers.put("UN", UserName);
            headers.put("PW", PassWord);
            return headers;


        }

    };

    RequestQueue requestQueue = Volley.newRequestQueue(this);
     requestQueue.add(stringRequest);

只需要添加headers.put("Content-Type", "application/x-www-form-urlencoded; charset=utf-8");到parms

祝你好运。

于 2018-03-09T00:18:10.433 回答