如何使用google的Volley lib实现JsonArrayRequest?

时间:2015-11-30 09:20:27

标签: java php android json

我使用过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,增值= RS.3064.38,itemTotal = 51073,sum_total = RS.156283.38,条形码= BQSP78BB,net_rate = 24200,日期= 2015-11-30 ,invoiceNo = 1,bill_type = Invoice}“]

请给我一些关于如何使用volley lib并在android中解析这种类型的json数组的建议。

这是我用来将一个项目详细信息插入数据库的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());
    }
?>

0 个答案:

没有答案