Value {"orders":[{"orders_id":14,"order_price":"220.50","shipping_cost":"10.00","total_tax":"220.50","date_purchased":"2019-01-28 12:16:51","products_id":8},{"orders_id":14,"order_price":"220.50","shipping_cost":"10.00","total_tax":"220.50","date_purchased":"2019-01-28 12:16:51","products_id":1}]} of type java.lang.String cannot be converted to JSONObject
I have tried some solutions provided on Stackoverflow like : Android Value ... of type java.lang.String cannot be converted to JSONArray but no effect. please help me.
php :
public function ResellerOrders($reseller_id)
{
$arr=array();
$result=array();
$stmt = $this->conn->prepare("SELECT o.orders_id,o.order_price,o.shipping_cost,o.total_tax,o.date_purchased,p.products_id FROM orders o, orders_products p WHERE o.orders_id = p.orders_id AND o.reseller_id = ?");
$stmt->bind_param("i", $reseller_id);
if ($stmt->execute())
{
$stmt-> bind_result($token2,$token3,$token4,$token5,$token6,$token7);
for ($i=0; $stmt-> fetch();$i++ )
{
$arr["orders"][$i]["orders_id"] = $token2;
$arr["orders"][$i]["order_price"] = $token3;
$arr["orders"][$i]["shipping_cost"] = $token4;
$arr["orders"][$i]["total_tax"]= $token5;
$arr["orders"][$i]["date_purchased"]= $token6;
$arr["orders"][$i]["products_id"]= $token7;
}
if(!$arr) exit('No rows');
$stmt->close();
return json_encode(($arr));
}
else
{
echo "no result";
}
return json_encode($arr);
}
Android Java code
JsonArrayRequest strReq = new JsonArrayRequest(Request.Method.POST,
URL_FOR_orders, new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
Log.d(TAG, "Reseller Dashboard Response: " + response.toString());
try {
for(int i=0;i<response.length();i++) {
JSONObject jObj=response.getJSONObject(i);
boolean error = jObj.getBoolean("error");
if (!error) {
Order_list orders = new Order_list(jObj);
Toast.makeText(getApplicationContext(), orders.product_id + " " + orders.billing_date, Toast.LENGTH_SHORT).show();
Intent intent = new Intent(
Dashboard.this,
categories.class);
intent.putExtra("reseller_id", reseller_code); startActivity(intent);
finish();
} else {
String errorMsg = jObj.getString("error_msg");
Toast.makeText(getApplicationContext(),
errorMsg, Toast.LENGTH_LONG).show();
}
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.e(TAG, "Error in getting data: " + error.getMessage());
Toast.makeText(getApplicationContext(),
error.getMessage(), Toast.LENGTH_LONG).show();
}
}) {
@Override
protected Map<String,String> getParams() {
// Posting params to login url
Map<String, String> params = new HashMap<String, String>();
params.put("reseller_id", String.valueOf(code));
return params;
}
};
I want to retrieve data into android but it is not allowing me, so where is the issue ? is it in passing values in php or retrieving data into android ?
I want to retrieve result like :
orders: 1.orders_id:14,order_price:220.50,shipping_cost:10.00,total_tax:220.50,date_purchased:2019-01-28 12:16:51,products_id:8 2.orders_id:14,order_price:220.50,shipping_cost:10.00,total_tax:220.50,date_purchased:2019-01-28 12:16:51,products_id:1