【发布时间】: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->status = false; $result->msg = mysql_error();,你只是在代码中加了假,没有错误,所以msg也是假的。 -
@NiranjanNRaju 感谢您指出这一点。您能告诉我如何解决这个问题吗?
-
你想在什么基础上设置
status??
标签: java php android json android-volley