嗨,我正在使用Volley我的登录页面。我需要像这样传递数据
Volley
{ userID : '-988682425884628921', email :'aditya@vyas.com', passwd : '123ss' }
我正在使用POST方法发送数据,我已经在DHC中进行了检查,Api在DHC中工作正常,并且我正在获取JSON Response,但是当我尝试使用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> }
解决了您的问题。刚刚使用JsonArrayRequest和传递的参数JsonObject形式:
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()。
getParams()
getHeaders()
问题:1 因为服务器接受了as 并且我们正在尝试提供数据,所以 您获得 响应代码500 。params``JsonObject``String
params``JsonObject``String
问题:2 您正在使用,JsonObjectRequet但是来自服务器的响应已进入,JsonArray因此您需要使用JsonArrayRequest来接受JsonArray
JsonObjectRequet
JsonArray
尝试让我知道这是否有帮助:)