我正在使用ArrayList<Hashmap<String,String>>
存储我的购物车。然后我将ArrayList
转换为JSONArray
以将其传递给服务器。但是当我在JSONArray
中解码PHP
时,它会null
。和android方面它给出了
03-02 20:48:38.546: E/JSON Parser(16949): Error parsing data org.json.JSONException: Value <br of type java.lang.String cannot be converted to JSONObject
参数如下所示
03-02 20:48:38.516: D/params(16949):
{address=address, email=email@yahoo.com,
cartitems=[{"name":"Chair","price":"1001","prodId":"2","category":"Chairs","image":"2130837519","Quantity":"1"},{"name":"Baxton Studio Club Chair","price":"4545","prodId":"5","category":"Chairs","image":"2130837521","Quantity":"1"}],
contact=124536987, username=name, payment=Cash, city=Islamabad}
Checkout
页面将数据发送到服务器
class SaveOrder extends AsyncTask<String, String, String> {
@Override
protected String doInBackground(String... args) {
// TODO Auto-generated method stub
AddtoCart obj = (AddtoCart) getApplicationContext();
obj.getCart().toString()));
JSONArray cart = new JSONArray(obj.getCart());
HashMap<String, String> params = new HashMap<String, String>();
params.put("username", username);
params.put("email", email);
params.put("payment", payment);
params.put("address", useraddress);
params.put("contact", contact);
params.put("city", usercity);
params.put("cartitems", cart.toString());
Log.d("params", params.toString());
JSONObject json = jParser.makeHttpRequest(url_all_products, "POST", params);
try {
int success = json.getInt("success");
if (success == 1) {
Toast.makeText(Checkout.this, json.getString("message"), Toast.LENGTH_LONG).show();
} else {
Toast.makeText(Checkout.this, json.getString("message"), Toast.LENGTH_LONG).show();
}
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
}
PHP
文件在数据库中存储数据
require_once __DIR__ . '/db_connect.php';
$db = new DB_CONNECT();
if($_SERVER['REQUEST_METHOD']=="POST"){
$userName=$_POST['username'];
$userEmailId=$_POST['email'];
$userPayment=$_POST['payment'];
$userAddress=$_POST['address'];
$userContact=$_POST['contact'];
$userCity=$_POST['city'];
$status='new';
$date=strftime('%y-%m-%d');
$array = array();
$array = $_POST['cartitems']; //jsonArray
$resp2 = array(stripslashes($array));
$array2 = array(json_decode($array));
switch (json_last_error()) {
case JSON_ERROR_NONE:
$error = ' - No errors';
break;
case JSON_ERROR_DEPTH:
$error =' - Maximum stack depth exceeded';
break;
case JSON_ERROR_STATE_MISMATCH:
$error =' - Underflow or the modes mismatch';
break;
case JSON_ERROR_CTRL_CHAR:
$error = ' - Unexpected control character found';
break;
case JSON_ERROR_SYNTAX:
$error =' - Syntax error, malformed JSON';
break;
case JSON_ERROR_UTF8:
$error = ' - Malformed UTF-8 characters, possibly incorrectly encoded';
break;
default:
$error = ' - Unknown error';
break;
}
$count=count($_POST['cartitems']);
$sql="INSERT INTO orders (customer_name, customer_emailId, customer_city, shipping_address, payment_method, contact_no, ordered_on,status) VALUES('$userName','$userEmailId','$userCity' ,'$userAddress','$userPayment','$userContact','$date','$status')";
mysql_query($sql);
$order_id = mysql_insert_id();
$sql2="insert into `orders_details` (`order_id`,`product_id`,`prodName`,`prodImg`,`catName`,`requied_qty`) values ";
for ($i=0; $i<$count; $i++) {
$sql2.="('$order_id', '{$array2[$i]['prodId']}', '{$array2[$i]['name']}', '{$array2[$i]['image']}', '{$array2[$i]['category']}', {$array2[$i]['Quantity']})";
if ($i < ($count - 1)) {
$sql2 .= ", ";
}
}
echo $sql2;
if (mysql_query($sql2)) {
$response['message']="success full inserted";
$response['success']=1;
echo json_encode($response);
}
else
{
$response['message']="failed to insert";
$response['success']=0;
echo json_encode($response);
}
}
?>