【问题标题】:Data not getting posted into mysql database using volley?数据没有使用 volley 发布到 mysql 数据库中?
【发布时间】:2015-12-01 09:49:09
【问题描述】:

我正在使用 google 的 Volley 库将数据发送到 mysql 数据库。我没有收到错误,但我的 onResponse 方法返回 RESPONSE: {"msg":false,"status":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)向我的 php 脚本发送一个 json 数组。 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;
        }
        }
        ?>

【问题讨论】:

  • 您遇到的错误是什么??
  • @NiranjanNRaju 我没有收到任何错误,它只是说 RESPONSE: {"msg":false,"status":false};在 logcat 中。
  • $result-&gt;status = false; $result-&gt;msg = mysql_error();,你只是在代码中加了假,没有错误,所以msg也是假的。
  • @NiranjanNRaju 感谢您指出这一点。您能告诉我如何解决这个问题吗?
  • 你想在什么基础上设置status??

标签: java php android json android-volley


【解决方案1】:

根据您的代码,

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

没有错误,所以msg也是假的。

【讨论】:

  • 你能告诉我如何检查 json 数组或 json 对象是否到达 php 代码
  • 在什么基础上,msg 应该包含值??
  • 成功插入时应该说“数据插入成功”就是这样
  • echo $res; 给了什么??
  • PHP 警告:在第 10 行的 /var/www/webapp/demoinvest.php 中为 foreach() 提供的参数无效这是我得到的错误。实际上我的 php 代码在 google cloud vm 上.所以当我运行我的 php 代码时,我得到了这个错误。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-03-08
  • 2013-07-29
  • 2017-02-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多