Я использовал метод getParams (), но я не знаю, как использовать метод JsonArrayRequest. Я думаю, что так оно и должно выглядеть.
JsonArrayRequest jsonArrayRequest = new JsonArrayRequest(Request.Method.POST, INVEST_URL, itemSelectedJson, new Response.Listener<JSONArray>() { @Override public void onResponse(JSONArray response) { } }, new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError error) { } }); RequestQueue requestQueue = Volley.newRequestQueue(this); requestQueue.add(jsonArrayRequest);
В приведенном ниже коде создается JsonArray.
private void selectedItems() { billType = (invEstSwitch.isChecked() ? textViewEstimate : textViewInvoice) .getText().toString(); itemselected.put("custInfo", custSelected.toString()); itemselected.put("invoiceNo", textViewInvNo.getText().toString()); itemselected.put("barcode", barCode.getText().toString()); itemselected.put("desc", itemDesc.getText().toString()); itemselected.put("weight", weightLine.getText().toString()); itemselected.put("rate", rateAmount.getText().toString()); itemselected.put("makingAmt", makingAmount.getText().toString()); itemselected.put("net_rate", netRate.getText().toString()); itemselected.put("itemTotal", itemtotal.getText().toString()); itemselected.put("vat", textViewVat.getText().toString()); itemselected.put("sum_total", textViewSum.getText().toString()); itemselected.put("bill_type", billType); itemselected.put("date", textViewCurrentDate.getText().toString()); //Add the map to the Array itemSelectedJson.put(itemselected); index++; }
Это массив json, который генерируется.
["{custInfo = Ujwal 9975022560, rate = 24000, weight = 21.00000, desc = GENTS ANGTHI 22k NO STONE, makingAmt = 200, vat = RS.3064.38, itemTotal = 51073, sum_total = RS.156283.38, штрих-код = BQSP78BB, net_rate = 24200, date = 2015-11-30, invoiceNo = 1, bill_type = Invoice} "," {custInfo = Ujwal 9975022560, rate = 24000, weight = 21.00000, desc = GENTS ANGTHI 22k NO STONE, makingAmt = 200, vat = RS .3064.38, itemTotal = 51073, sum_total = RS.156283.38, barcode = BQSP78BB, net_rate = 24200, date = 2015-11-30, invoiceNo = 1, bill_type = Invoice} "," {custInfo = Ujwal 9975022560, rate = 24000, вес = 21,00000, desc = GENTS ANGTHI 22k NO STONE, makingAmt = 200, vat = RS.3064.38, itemTotal = 51073, sum_total = RS.156283.38, штрих-код = BQSP78BB, net_rate = 24200, date = 2015-11-30, invoiceNo = 1, bill_type = Invoice} "]
Пожалуйста, дайте мне несколько советов о том, как использовать volley lib и разбирать этот тип json-массива в android.
Это код getParams, который я использовал для вставки одной детали детали в базу данных.
@Override protected Map<String, String> getParams() throws AuthFailureError { Map<String, String> params = new HashMap<>(); params.put(KEY_CUSTINFO, custInfo); params.put(KEY_INVOICENO, invoiceNo); params.put(KEY_SBARCODE, barcode); params.put(KEY_DESC, desc); params.put(KEY_WEIGHT, weight); params.put(KEY_RATE, rate); params.put(KEY_MAKINGAMT, makingAmt); params.put(KEY_NETRATE, net_rate); params.put(KEY_ITEMTOTAL, itemTotal); params.put(KEY_VAT, vat); params.put(KEY_SUMTOTAL, sum_total); params.put(KEY_BILLTYPE, bill_type); params.put(KEY_DATE, date); return params; } @Override protected String getParamsEncoding() { return super.getParamsEncoding(); } }; RequestQueue requestQueue = Volley.newRequestQueue(this); requestQueue.add(stringRequest);
Это мой php-код
<?php require "init.php"; $json = file_get_contents('php://input'); $data = json_decode($json,true); //echo $obj+""; $custInfo = $data['custInfo']; $rate = $data['rate']; $weight= $data['weight']; $desc= $data['desc']; $makingAmt= $data['makingAmt']; $vat= $data['vat']; $itemTotal= $data['itemTotal']; $sum_total= $data['sum_total']; $barcode= $data['barcode']; $net_rate= $data[net_rate'']; $date= $data['date']; $invoiceNo= $data['invoiceNo']; $bill_type= $data['bill_type']; $sql = "INSERT INTO selected_items (custInfo, invoiceNo, barcode, desc, weight, rate, makingAmt,net_rate,itemTotal,vat,sum_total,bill_type,date) VALUES ('$custInfo','$invoiceNo','$barcode','$desc','$weight','$rate','$makingAmt','$net_rate','$itemTotal','$vat','$sum_total','$bill_type','$date')"; if(!mysqli_query($sql,$con)) { die('Error : ' . mysql_error()); } ?>
-<?php require "init.php"; $json = file_get_contents('php://input'); $data = json_decode($json,true); //echo $obj+""; $custInfo = $data['custInfo']; $rate = $data['rate']; $weight= $data['weight']; $desc= $data['desc']; $makingAmt= $data['makingAmt']; $vat= $data['vat']; $itemTotal= $data['itemTotal']; $sum_total= $data['sum_total']; $barcode= $data['barcode']; $net_rate= $data[net_rate'']; $date= $data['date']; $invoiceNo= $data['invoiceNo']; $bill_type= $data['bill_type']; $sql = "INSERT INTO selected_items (custInfo, invoiceNo, barcode, desc, weight, rate, makingAmt,net_rate,itemTotal,vat,sum_total,bill_type,date) VALUES ('$custInfo','$invoiceNo','$barcode','$desc','$weight','$rate','$makingAmt','$net_rate','$itemTotal','$vat','$sum_total','$bill_type','$date')"; if(!mysqli_query($sql,$con)) { die('Error : ' . mysql_error()); } ?>