我想在截击请求正文中传递{“ data:” [{{id“:” 12“}]}。
private void reqrej(final String currentLat) {
String insertData = "http://www.xxxxxxxx.com/makeinforequest.php";
final StringRequest stringRequest = new StringRequest(Request.Method.POST, insertData, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
Log.i("json", response.toString());
tv.setText(response.toString());
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
}) {
@Override
protected Map<String, String> getParams() throws AuthFailureError {
// String mylong = String.valueOf(mCurrentLocation.getLongitude()).toString();
Map<String, String> params = new HashMap<String, String>();
params.put("data", "");
//where I can type request code?
return checkParams(params);
}
private Map<String, String> checkParams(Map<String, String> map) {
Iterator<Map.Entry<String, String>> it = map.entrySet().iterator();
while (it.hasNext()) {
Map.Entry<String, String> pairs = (Map.Entry<String, String>) it.next();
if (pairs.getValue() == null) {
map.put(pairs.getKey(), "");
}
}
return map;
}
};
RequestQueue requestQueue = Volley.newRequestQueue(this);
requestQueue.add(stringRequest);
}
请指导我如何通过volley在android中传递这些东西,我尝试了很多次但没有成功。
最佳答案
尝试这个
try {
RequestQueue requestQueue = Volley.newRequestQueue(this);
try {
JSONObject jsonObject=new JSONObject();
JSONArray jsonArray=new JSONArray();
JSONObject idJson=new JSONObject();
idJson.put("id","12");
jsonArray.put(jsonObject);
jsonObject.put("data",jsonArray);
} catch (JSONException e) {
e.printStackTrace();
}
final String mRequestBody = jsonObject.toString();
StringRequest stringRequest = new StringRequest(Request.Method.POST, URL, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
Log.i("LOG_RESPONSE", response);
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.e("LOG_RESPONSE", error.toString());
}
}) {
@Override
public String getBodyContentType() {
return "application/json; charset=utf-8";
}
@Override
public byte[] getBody() throws AuthFailureError {
try {
return mRequestBody == null ? null : mRequestBody.getBytes("utf-8");
} catch (UnsupportedEncodingException uee) {
VolleyLog.wtf("Unsupported Encoding while trying to get the bytes of %s using %s", mRequestBody, "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));
}
};
requestQueue.add(stringRequest);
} catch (JSONException e) {
e.printStackTrace();
}