我想通过Volley库将以下格式的JsonObject发送到服务器。
{
"Email":"a@a.a",
"Password":"123456",
"ProveedorAcceso":"web",
"NivelAcceso":{
"Id": 1
},
"Estatus": {
"Id":1
} }
我的代码:
public void getData() {
StringRequest postRequest = new StringRequest(Request.Method.POST, urlLogin,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try {
JSONObject jsonResponse = new JSONObject(response);
Log.d(TAG, String.valueOf(jsonResponse));
} catch (JSONException e) {
e.printStackTrace();
Log.d(TAG, String.valueOf(e));
}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError volleyError) {
try {
String responseBody = new String(volleyError.networkResponse.data, "utf-8");
JSONObject jsonObject = new JSONObject(responseBody);
/**
dialogLoading.dismiss();
if (jsonObject.getInt("Codigo") == 400) {
onDialogErrorResponse();
}**/
} catch (JSONException e) {
//Handle a malformed json response
Log.d("Response", String.valueOf(e));
} catch (UnsupportedEncodingException error) {
Log.d("Response", String.valueOf(error));
}
}
}
) {
// here is params will add to your url using post method
@Override
protected Map<String, String> getParams() {
Map<String, String> params = new HashMap<String, String>();
params.put("Email", edtEmail.getText().toString());
params.put("Password", edtPassword.getText().toString());
params.put("ProveedorAcceso", "web");
params.put("NivelAcceso", 1+"");
params.put("Estatus", 1+"");
return params;
}
};
postRequest.setRetryPolicy(new DefaultRetryPolicy(
10000,
DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
requestQueue = Volley.newRequestQueue(this);
requestQueue.add(postRequest);
DiskBasedCache cache = new DiskBasedCache(this.getCacheDir(), 500 * 1024 * 1024);
requestQueue = new RequestQueue(cache, new BasicNetwork(new HurlStack()));
requestQueue.start();
}
由于服务器未正确接收数据,因此向我抛出有关数据的错误。
“ Codigo”:500,“ Mensaje”:“对象引用未设置为对象的实例。”
我希望您能在这个疑问中对我有所帮助,因为我对此感到非常困惑,非常感谢。