简体   繁体   English

使用Android中的凌空,使用JsonArrayRequest将JSON数组POST到Mysql数据库?

[英]POST json array to Mysql database using JsonArrayRequest using volley in android?

This is my code for JsonArrayRequest I think either it is incomplete or incorrect. 这是我的JsonArrayRequest代码,我认为它不完整或不正确。 The problem is nothing getting added into the database and I am not getting any errors. 问题是什么都没有添加到数据库中,并且我没有收到任何错误。 I am new to programming so I have no idea as to where I might be going wrong. 我是编程新手,所以我不知道哪里可能出错。

private void insertToDb() {
    JsonArrayRequest jsonArrayRequest = new JsonArrayRequest(Request.Method.POST, INVEST_URL,
            itemSelectedJson, new Response.Listener<JSONArray>() {
        @Override
        public void onResponse(JSONArray response) {
            Toast.makeText(AddInvEst.this, "The echoed response is "+response, Toast.LENGTH_SHORT).show();
        }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {

        }
    }){
        @Override
        public Map<String, String> getHeaders() throws AuthFailureError {
            Map<String,String> headers = new HashMap<String, String>();
            headers.put("Content-Type","application/json");
            headers.put("Accept", "application/json");
            return headers;
        }
    };
    RequestQueue requestQueue = Volley.newRequestQueue(this);
    requestQueue.add(jsonArrayRequest);
}

This is code for creating a jsonArray 这是用于创建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++;
}

And this is my php code. 这是我的php代码。

<?php
require "init.php";
$json = file_get_contents('php://input'); 
//$data = json_decode($json,true);
// remove the ,true so the data is not all converted to arrays
$data = json_decode($json);
// Now process the array of objects
foreach ( $data as $inv ) {
    $custInfo = $inv->custInfo;
    $rate =     $inv->rate;
    $weight=    $inv->weight;
    $desc=      $inv->desc;
    $makingAmt= $inv->makingAmt;
    $vat=       $inv->vat;
    $itemTotal= $inv->itemTotal;
    $sum_total= $inv->sum_total;
    $barcode=   $inv->barcode;
    $net_rate=  $inv->net_rate;
    $date=      $inv->date;
    $invoiceNo= $inv->invoiceNo;
    $bill_type= $inv->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')";
    $res = mysql_query($sql,$con);
    if(!$res){
        $result = new stdClass();
        $result->status = false;
        $result->msg = mysql_error();
        echo json_encode($result);
        exit;
    }
}
?>

You missed to add a method getParams() inside the post request, it should have this structure 您错过了在发布请求内添加方法getParams()的方法,该方法应具有以下结构

StringRequest stringRequest = new StringRequest(Request.Method.POST, "url",
            new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {

                    }
                }
            },
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    errorHandler(error);
                }
            }){
        @Override
        protected Map<String, String> getParams() throws AuthFailureError {
            Map<String,String> params = new HashMap<>();
            //Adding parameters to request
            params.put("KEY1", key1);
            params.put("KEY2", key2);

            //returning parameter
            return params;
        }
    };

    //Adding the string request to the queue
    RequestQueue requestQueue = Volley.newRequestQueue(this);
    requestQueue.add(stringRequest);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Volley - 使用JSONArrayRequest发送POST请求 - Volley - Sending a POST request using JSONArrayRequest 将一个参数传递给服务器并使用带有凌空android studio的JsonArrayRequest获取数组 - passing one param to server and get array using JsonArrayRequest with volley android studio 排球JsonArrayRequest加入StringRequest帖子 - android - volley JsonArrayRequest join StringRequest post 与POST方法齐射_ jsonArrayRequest - volley _ jsonArrayRequest with POST method 我如何发布JSON数组,并最好使用android Volley获得JSON对象作为响应? - How do I POST a JSON array and get a JSON object in response using preferably android Volley? 使用Android中的Volley发送JSON数组以将MySQL中的多个记录添加到PHP脚本中 - Send json array to add multiple records in mysql to php script using volley in android Android-使用凌空库将json对象中的图像发布到服务器 - Android - Post image within json object to server using volley library 使用PHP对片段Volley上的RecyclerView解析错误检索的JsonArrayRequest - Parse Error Retrieved JsonArrayRequest Using PHP to RecyclerView On Fragments Volley 使用齐射显示BLOB图像从mysql到android(json编码) - Display BLOB image from mysql to android using volley (json encoded) 使用Volley从在线数据库中获取Android应用程序的数组 - Fetching array from online database for android app using volley
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM