您好我正在使用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>
}
答案 0 :(得分:5)
解决了你的问题。刚刚使用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 ,因为服务器正在接受params
JsonObject
,我们正在尝试提供String
。
问题:2
您使用的是JsonObjectRequet
,但服务器的响应位于JsonArray
,因此您需要使用JsonArrayRequest
接受JsonArray
尝试让我知道这有用与否:)
答案 1 :(得分:3)
我有类似的问题。我在getHeaders方法中覆盖了Content-Type:
headers.put("Content-Type", "application/json; charset=UTF-8");
但是Volley本身添加了一个Content-Type参数,因此有两个参数。删除该行已解决了我的问题。
答案 2 :(得分:1)
您需要覆盖NSURL *scriptURL = [NSURL URLWithString:@"path/to/fancyLibrary.js"];
NSError *error = nil;
NSString *script = [NSString stringWithContentsOfURL:scriptURL encoding:NSUTF8StringEncoding error:&error];
[context evaluateScript:script];
以在POST中传递参数。
protected Map<String, String> getParams()
答案 3 :(得分:0)
注意内容类型标题。 Volley处理它与其他标题不同,它没有在Map&lt;&gt;中显示头。您应该覆盖getBodyContentType(),而不是覆盖getHeaders()方法来设置内容类型:
Foo.equals(String)
答案 4 :(得分: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