我正在使用Volley登录功能,但我为https获得了意外的响应代码400。
它适用于WiFi但不适用于蜂窝网络,问题只发生在选定的手机型号上。以下是我的代码。
public void LoginRequest(final String username,final String password,final ActionResponse success,final Action err ){
RequestQueue queue = MVolleyRequests.getInstance(mContext).getRequestQueue();
StringRequest sr = new StringRequest(com.android.volley.Request.Method.POST,LOGIN_URL,
new Listener<String>() {
@Override
public void onResponse(String response) {
success.action(MError.getError(Integer.parseInt(response)));
}
}, new ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
err.action();
}
}){
@Override
protected Map<String,String> getParams(){
Map<String,String> params = new HashMap<String, String>();
params.put("id", username);
params.put("pwd", password);
params.put("version", "2.5" );
return params;
}
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
Map<String,String> params = new HashMap<String, String>();
params.put("Content-Type","application/x-www-form-urlencoded; charset=utf-8");
return params;
}
};
sr.setRetryPolicy(new DefaultRetryPolicy(
60000,
DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
MVolleyRequests.getInstance(mContext).addToRequestQueue(sr);
}
答案 0 :(得分:3)
试试这个:
JSONObject params = new JSONObject();
try {
params.put("id", username);
params.put("pwd", password);
params.put("version", "2.5" );
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//
JsonObjectRequest jsonObjReq = new JsonObjectRequest(Method.POST,
url, params,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
Log.d(TAG, response.toString());
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
VolleyLog.d(TAG, "Error: " + error.getMessage());
}
}) {
/**
* Passing some request headers
* */
@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.setRetryPolicy(new DefaultRetryPolicy(60000,
DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
// Adding request to request queue
getRequestQueue(jsonObjReq);