我必须发送
{ "200": {"username": "ppoo" ,
"password": "ppoo" ,
"emails": ["ppoo"] },
"400": [] }
在我的请求的原始主体中,它将在成功响应时给出状态代码200。
如果我使用JSONobjectrequest,那么它将显示VolleyParseError。我不知道要使用哪个请求。
答案 0 :(得分:1)
这里有完整的工作代码..
private void LoginUser() {
try {jsonObject.put("password",hashpassword );jsonObject.put("username",username);} catch (JSONException e){e.printStackTrace();}
jsonStr =jsonObject.toString();
// json string variable保持这是您想要以字符串形式发送数据的原始数据= {“username”:“您的用户名”,“密码”:“yourpassword”} //
RequestQueue requestQueue = Volley.newRequestQueue(this);
StringRequest stringRequest = new StringRequest(Request.Method.POST, SignInUrl,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
Toast.makeText(getBaseContext(),response.toString(), Toast.LENGTH_LONG).show();
hideProgressDialog();
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.e("VOLLEY", error.toString());
Toast.makeText(getBaseContext(),error.toString(), Toast.LENGTH_LONG).show();
}
}){
@Override
public String getBodyContentType() {
return "application/json; charset=utf-8";
}
@Override
public byte[] getBody() {
try {
return jsonStr == null ? null : jsonStr.getBytes("utf-8");
} catch (UnsupportedEncodingException uee) {
VolleyLog.wtf("Unsupported Encoding while trying to get the bytes of %s using %s",
jsonStr, "utf-8");
return null;
}
}
@Override
protected Response<String> parseNetworkResponse(NetworkResponse response) {
String responseString = "";
if (response != null) {
responseString = String.valueOf(response.statusCode);
}
return Response.success(responseString, HttpHeaderParser.parseCacheHeaders(response));
}
@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;
}};
requestQueue.add(stringRequest);
}