运行时Android中出现错误:无法将字符串转换为JSONObject

时间:2019-01-28 16:25:58

标签: php android

  

值   {“ 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   类型为java.lang.String的12:16:51“,” products_id“:1}]}}不能为   转换为JSONObject

我尝试了一些Stackoverflow提供的解决方案,例如:Android Value ... of type java.lang.String cannot be converted to JSONArray,但没有任何效果。请帮助我。


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代码

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;
            }
        };

我想将数据检索到android中,但不允许我这样做,所以问题出在哪里?是在php中传递值还是将数据检索到android中?

我想检索结果:

  

订单:   1.orders_id:14,order_price:220.50,shipping_cost:10.00,total_tax:220.50,date_purchaeded: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_purchaeded:2019-01-28   12:16:51,products_id:1

1 个答案:

答案 0 :(得分:0)

@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);
                String ordersId = jObj.getString("orders_id");
                String orderPrice = jObj.getString("order_price");
                String shippingCost = jObj.getString("shipping_cost");
                String totalTax =jObj.getString("total_tax");
                String datePurchased = jObj.getString("date_purchased");
                String productsId = jObj.getString("products_id);            
            }
        } 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;
    }
};