数据没有使用凌空发布到mysql数据库中?

时间:2015-12-01 09:49:09

标签: java php android json android-volley

  

我正在使用Google的Volley库将数据发送到mysql数据库。我没有收到错误,但我的onResponse方法返回RESPONSE:{" msg":false,"状态":false}任何人都可以告诉我这意味着什么以及如何解决它。

  private void insertToDb() {
    billType = (invEstSwitch.isChecked() ? textViewEstimate : textViewInvoice)
            .getText().toString();
    try {
        jsonObject.put("custInfo", custSelected.toString());
        jsonObject.put("invoiceNo", textViewInvNo.getText().toString());
        jsonObject.put("barcode", barCode.getText().toString());
        jsonObject.put("desc", itemDesc.getText().toString());
        jsonObject.put("weight", weightLine.getText().toString());
        jsonObject.put("rate", rateAmount.getText().toString());
        jsonObject.put("makingAmt", makingAmount.getText().toString());
        jsonObject.put("net_rate", netRate.getText().toString());
        jsonObject.put("itemTotal", itemtotal.getText().toString());
        jsonObject.put("vat", textViewVat.getText().toString());
        jsonObject.put("sum_total", textViewSum.getText().toString());
        jsonObject.put("bill_type", billType);
        jsonObject.put("date", textViewCurrentDate.getText().toString());


    } catch (JSONException e) {
        e.printStackTrace();
    }
    try {
        itemSelectedJson.put(index, jsonObject);
        index++;
    } catch (JSONException e) {
        e.printStackTrace();
    }
    JSONObject jsonobj = new JSONObject();
    try {
        jsonobj.put("itemarray",itemSelectedJson);
    } catch (JSONException e) {
        e.printStackTrace();
    }

    final JsonObjectRequest objectRequest = new JsonObjectRequest(Request.Method.POST, INVEST_URL, jsonobj, new Response.Listener<JSONObject>() {
        @Override
        public void onResponse(JSONObject response) {
            Log.d("RESPONSE", response.toString());
        }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            Log.d("JSONERROR",error.toString());
        }
    }){
        @Override
        public Map<String, String> getHeaders() throws AuthFailureError {
            Map<String, String> headers = new HashMap<>();
            headers.put("Content-Type", "application/json; charset=utf-8");
            headers.put("Accept", "application/json");
            return headers;
        }
    };


    final RequestQueue queue = Volley.newRequestQueue(this);
    queue.add(objectRequest);

}

//And I am calling this method from onOptionsItemSelected(MenuItem item) like
 case R.id.action_save:
            insertToDb();
            return true;
  

我想做的是   1)将json数组发送到我的php脚本。   2)解析json数组和   3)将数据插入mysql表。

这是我的php脚本

 <?php
          require "init.php";
          $json = file_get_contents('php://input'); 

          $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 = mysqli_query($sql,$con);
        echo $res;
        if(!$res){
            $result = new stdClass();
            $result->status = false;
            $result->msg = mysql_error();
            echo json_encode($result);
            exit;
        }
        }
        ?>

1 个答案:

答案 0 :(得分:2)

根据您的代码,

$result->status = false;// you are only assigning false here
$result->msg = mysql_error();

没有错误,因此msg也是错误的。