如何使用 Volley In Android 在 POST 请求的正文中传递数组
How to Pass Array in Body of POST Request Using Volley In Android
这里是我用于在 Volley 请求正文中发送数组的代码。但它显示意外响应代码 500。
这是Post人Post请求图片
here i am declaring variable of array
private ArrayList<Integer> medicine_id = new ArrayList<>();
private ArrayList<Integer> code = new ArrayList<>();
private ArrayList<String> expiry_date = new ArrayList<>();
private ArrayList<String> batch_no = new ArrayList<>();
private ArrayList<Integer> qty = new ArrayList<>();
private ArrayList<Double> cost = new ArrayList<>();
private ArrayList<Double> total = new ArrayList<>();
here i am adding value in my declared variables
medicine_id.add(618);
code.add(202012011);
expiry_date.add("2021-01-09");
batch_no.add("1");
qty.add(1);
cost.add((double) 1234);
total.add((double) 2468);
here is the code request i am using for Post Request method
try {
RequestQueue requestQueue = Volley.newRequestQueue(SelectedPurchaseActivity.this);
String URL = "http://khadizamedicinecorner.com/DokanPos/api/purchase/item-save";
JSONObject jsonBody = new JSONObject();
jsonBody.put("purchase_no", "87676767");
jsonBody.put("supplier", 7);
jsonBody.put("date", "2020-12-02");
jsonBody.put("p_type", "Cash");
jsonBody.put("medicine_id",medicine_id);
jsonBody.put("code", code);
jsonBody.put("expiry_date", expiry_date);
jsonBody.put("batch_no", batch_no);
jsonBody.put("qty", qty);
jsonBody.put("cost", cost);
jsonBody.put("total", total);
jsonBody.put("totalQty", 2);
jsonBody.put("subTotal", 2468);
jsonBody.put("discount", 3);
jsonBody.put("d_type", "%");
jsonBody.put("payable", 2394);
jsonBody.put("paid", 3);
jsonBody.put("due", 2391);
final String requestBody = jsonBody.toString();
Here is the String Request to get the string response after posting
data successfully
StringRequest stringRequest = new StringRequest(Request.Method.POST, URL, new
Response.Listener<String>() {
@Override
public void onResponse(String response) {
Log.i("VOLLEY Response", response);
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.e("VOLLEY Error", error.toString());
}
}) {
@Override
public String getBodyContentType() {
return "application/json; charset=utf-8";
}
@Override
public byte[] getBody() throws AuthFailureError {
try {
return requestBody == null ? null : requestBody.getBytes("utf-8");
} catch (UnsupportedEncodingException uee) {
VolleyLog.wtf("Unsupported Encoding while trying to get the bytes of %s using %s", requestBody, "utf-8");
return null;
}
}
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
Map<String, String> params = new HashMap<String, String>();
params.put("Authorization", token);
return params;
}
requestQueue.add(stringRequest);
} catch (JSONException e) {
e.printStackTrace();
}
这就是解决方案
StringRequest stringRequest = new StringRequest(Request.Method.POST, Constant.SAVE_ITEM,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
Toast.makeText(SelectedPurchaseActivity.this,response,Toast.LENGTH_LONG).show();
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(SelectedPurchaseActivity.this,error.toString(),Toast.LENGTH_LONG).show();
}
}){
@Override
protected Map<String,String> getParams(){
Map<String,String> params = new HashMap<String, String>();
params.put("purchase_no", "87676767");
params.put("supplier", "7");
params.put("date", "2020-12-02");
params.put("p_type", "Cash");
params.put("medicine_id[]","618");
params.put("code[]", "202012011");
params.put("expiry_date[]", "2021-01-09");
params.put("batch_no[]", "1");
params.put("qty[]", "1");
params.put("cost[]", "1234");
params.put("total[]", "2468");
params.put("totalQty", "2");
params.put("subTotal", "2468");
params.put("discount", "3");
params.put("d_type", "%");
params.put("payable", "2394");
params.put("paid", "3");
params.put("due", "2391");
return params;
}
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
Map<String, String> params = new HashMap<String, String>();
params.put("Authorization", token);
return params;
}
};
RequestQueue queue = Volley.newRequestQueue(SelectedPurchaseActivity.this);
queue.add(stringRequest);
这里是我用于在 Volley 请求正文中发送数组的代码。但它显示意外响应代码 500。
这是Post人Post请求图片
here i am declaring variable of array
private ArrayList<Integer> medicine_id = new ArrayList<>();
private ArrayList<Integer> code = new ArrayList<>();
private ArrayList<String> expiry_date = new ArrayList<>();
private ArrayList<String> batch_no = new ArrayList<>();
private ArrayList<Integer> qty = new ArrayList<>();
private ArrayList<Double> cost = new ArrayList<>();
private ArrayList<Double> total = new ArrayList<>();
here i am adding value in my declared variables
medicine_id.add(618);
code.add(202012011);
expiry_date.add("2021-01-09");
batch_no.add("1");
qty.add(1);
cost.add((double) 1234);
total.add((double) 2468);
here is the code request i am using for Post Request method
try {
RequestQueue requestQueue = Volley.newRequestQueue(SelectedPurchaseActivity.this);
String URL = "http://khadizamedicinecorner.com/DokanPos/api/purchase/item-save";
JSONObject jsonBody = new JSONObject();
jsonBody.put("purchase_no", "87676767");
jsonBody.put("supplier", 7);
jsonBody.put("date", "2020-12-02");
jsonBody.put("p_type", "Cash");
jsonBody.put("medicine_id",medicine_id);
jsonBody.put("code", code);
jsonBody.put("expiry_date", expiry_date);
jsonBody.put("batch_no", batch_no);
jsonBody.put("qty", qty);
jsonBody.put("cost", cost);
jsonBody.put("total", total);
jsonBody.put("totalQty", 2);
jsonBody.put("subTotal", 2468);
jsonBody.put("discount", 3);
jsonBody.put("d_type", "%");
jsonBody.put("payable", 2394);
jsonBody.put("paid", 3);
jsonBody.put("due", 2391);
final String requestBody = jsonBody.toString();
Here is the String Request to get the string response after posting data successfully
StringRequest stringRequest = new StringRequest(Request.Method.POST, URL, new
Response.Listener<String>() {
@Override
public void onResponse(String response) {
Log.i("VOLLEY Response", response);
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.e("VOLLEY Error", error.toString());
}
}) {
@Override
public String getBodyContentType() {
return "application/json; charset=utf-8";
}
@Override
public byte[] getBody() throws AuthFailureError {
try {
return requestBody == null ? null : requestBody.getBytes("utf-8");
} catch (UnsupportedEncodingException uee) {
VolleyLog.wtf("Unsupported Encoding while trying to get the bytes of %s using %s", requestBody, "utf-8");
return null;
}
}
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
Map<String, String> params = new HashMap<String, String>();
params.put("Authorization", token);
return params;
}
requestQueue.add(stringRequest);
} catch (JSONException e) {
e.printStackTrace();
}
这就是解决方案
StringRequest stringRequest = new StringRequest(Request.Method.POST, Constant.SAVE_ITEM,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
Toast.makeText(SelectedPurchaseActivity.this,response,Toast.LENGTH_LONG).show();
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(SelectedPurchaseActivity.this,error.toString(),Toast.LENGTH_LONG).show();
}
}){
@Override
protected Map<String,String> getParams(){
Map<String,String> params = new HashMap<String, String>();
params.put("purchase_no", "87676767");
params.put("supplier", "7");
params.put("date", "2020-12-02");
params.put("p_type", "Cash");
params.put("medicine_id[]","618");
params.put("code[]", "202012011");
params.put("expiry_date[]", "2021-01-09");
params.put("batch_no[]", "1");
params.put("qty[]", "1");
params.put("cost[]", "1234");
params.put("total[]", "2468");
params.put("totalQty", "2");
params.put("subTotal", "2468");
params.put("discount", "3");
params.put("d_type", "%");
params.put("payable", "2394");
params.put("paid", "3");
params.put("due", "2391");
return params;
}
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
Map<String, String> params = new HashMap<String, String>();
params.put("Authorization", token);
return params;
}
};
RequestQueue queue = Volley.newRequestQueue(SelectedPurchaseActivity.this);
queue.add(stringRequest);