Java 分析数据org.json.JSONException时出错:在的字符0处输入结束

Java 分析数据org.json.JSONException时出错:在的字符0处输入结束,java,php,android,json,Java,Php,Android,Json,我在解析JSON时出错。分析数据org.json.JSONException时出错:在的字符0处输入结束 错误日志: 04-19 20:51:00.635: E/ViewRootImpl(24857): sendUserActionEvent() mView == null 04-19 20:51:00.635: E/ViewRootImpl(24857): sendUserActionEvent() mView == null 04-19 20:51:03.320: E/ViewRootImp

我在解析JSON时出错。分析数据org.json.JSONException时出错:在的字符0处输入结束

错误日志:

04-19 20:51:00.635: E/ViewRootImpl(24857): sendUserActionEvent() mView == null
04-19 20:51:00.635: E/ViewRootImpl(24857): sendUserActionEvent() mView == null
04-19 20:51:03.320: E/ViewRootImpl(24857): sendUserActionEvent() mView == null
04-19 20:51:10.215: E/JSON Parser(24857): Error parsing data org.json.JSONException: End of input at character 0 of 
04-19 20:51:35.600: E/ViewRootImpl(24857): sendUserActionEvent() mView == null
这是在UserFunction类中调用函数的代码:

    pTotalPrice=new double[cartSize]; // store array of totalprice 

    /** To be sent to DB **/
    sPID = new String[cartSize];
    sProduct = new String[cartSize];
    sQuantity = new String[cartSize];
    sTotalPrice = new String[cartSize];

    if(cartSize >0)
    {
        for(int i=0;i<cartSize;i++)
        {   
            final int counter = i;
            // Get probuct data from product data arraylist
            String pID = aController.getProducts(i).getProductId();
            sPID[i] = pID;
            String pName = aController.getProducts(i).getProductName();
            sProduct[i] = pName;
            double pPrice   = aController.getProducts(i).getProductPrice();
            int pQuantity   = aController.getProducts(i).getProductQuantity();
            sQuantity[i] = Integer.toString(pQuantity);
            pTotalPrice[i] = pPrice * pQuantity;
            sTotalPrice[i] = Double.toString(pTotalPrice[i]); 
            }
    pFinalPrice -= pTotalPrice[counter];
sFinalPrice = Double.toString(pFinalPrice);
    }      
    protected JSONObject doInBackground(String... args) {


            UserFunctions userFunction = new UserFunctions();
            JSONObject json = userFunction.orderDetails(username, sPID, sProduct, sQuantity, sTotalPrice, Double.toString(pFinalPrice));

            Log.d("Button", "Order");
            return json;


        }
这是我的index.php api

else if ($tag == 'order') {
    $username = $_POST['username'];
    $finalprice = $_POST['finalprice'];
    $pid = $_POST['pid'];

    $quantity = $_POST['quantity'];
    $totalprice = $_POST['totalprice'];

            $response["successfullypost"] = 1;
    $response["user"]["username"] = $username;
    $response["user"]["finalprice"] = $finalprice;
    $response["user"]["pid"] = $pid;
    $response["user"]["quantity"] = $quantity;
    $response["user"]["totalprice"] = $totalprice;
    echo json_encode($response);

    $uResult = mysql_query("SELECT * FROM users WHERE username = $username");
    $uid = $uResult['uid'];

    $counter = sizeof($pid); 

    for ( $i=0; $i < $counter; $i++) {
        $db-> orderdetails($uid, $pid[$i], $quantity[$i], $totalprice[$i], $finalprice);
    }

}

else {
    $response["error"] = 3;
    $response["error_msg"] = "JSON ERROR";
    echo json_encode($response);
}
新的JSON响应。虽然我不得不这样做,但它不显示阵列。为什么JSON标记显示“i”而不是pid[i]?数量和总价相同

04-20 10:19:31.615: E/ViewRootImpl(20740): sendUserActionEvent() mView == null
04-20 10:19:44.505: E/JSON(20740): {"tag":"order","success":0,"error":0,"successfullypost":1,"user":{"username":"zulanawi","finalprice":null,"pid":{"i":"0002"},"quantity":{"k":"3"},"totlaprice":{"l":"32.400000000000006"}}}{"success":0}

完成!!!在参考之后,我得到了答案

/**
* Function store order details
**/
public JSONObject orderDetails(String username, String[] pid, String[] products,    String[] quantity, String[] totalprice, String finalprice) {
// Building Parameters
List params = new ArrayList();
params.add(new BasicNameValuePair("tag", order_tag));
params.add(new BasicNameValuePair("username", username));
params.add(new BasicNameValuePair("finalpice", finalprice));
for (int i = 0; i < pid.length; i++) {
    params.add(new BasicNameValuePair("pid[]", pid[i]));
}   
for (int j = 0; j < products.length; j++) {
    params.add(new BasicNameValuePair("products[]", products[j]));
}
for (int k = 0; k < quantity.length; k++) {
    params.add(new BasicNameValuePair("quantity[]", quantity[k]));
}
for (int l = 0; l < totalprice.length; l++) {
    params.add(new BasicNameValuePair("totalprice[]", totalprice[l]));
}

JSONObject json = jsonParser.getJSONFromUrl(orderURL,params);
return json;
} 
/**
*函数存储订单详细信息
**/
公共JSONObject orderDetails(字符串用户名、字符串[]pid、字符串[]产品、字符串[]数量、字符串[]总价、字符串最终价格){
//建筑参数
List params=new ArrayList();
参数添加(新的BasicNameValuePair(“标签”,订单标签));
添加(新的BasicNameValuePair(“用户名”,用户名));
参数添加(新的BasicNameValuePair(“最终价格”,最终价格));
对于(int i=0;i
您是否检查过php代码是否正常工作?您似乎返回了一个错误的json,但是在成功场景中返回了什么?@MTahir我已经更新了它。请在您的回答中添加一些详细信息,解释上面代码中的问题以及您是如何解决的。这将有助于其他人的理解
public function orderdetails ($uid, $pid, $quantity, $totalprice, $finalprice) {

    $pResult = mysql_query("SELECT * FROM products WHERE pid = $pid");
    $Product_ID = $pResult['ProductID'];

    $final = mysql_query("INSERT INTO vieworders (uid, ProductID, quantity, $totalprice, finalprice) 
        VALUES ('$uid', '$ProductID', '$quantity', '$totalprice', '$finalprice')");

}
04-20 10:19:31.615: E/ViewRootImpl(20740): sendUserActionEvent() mView == null
04-20 10:19:44.505: E/JSON(20740): {"tag":"order","success":0,"error":0,"successfullypost":1,"user":{"username":"zulanawi","finalprice":null,"pid":{"i":"0002"},"quantity":{"k":"3"},"totlaprice":{"l":"32.400000000000006"}}}{"success":0}
/**
* Function store order details
**/
public JSONObject orderDetails(String username, String[] pid, String[] products,    String[] quantity, String[] totalprice, String finalprice) {
// Building Parameters
List params = new ArrayList();
params.add(new BasicNameValuePair("tag", order_tag));
params.add(new BasicNameValuePair("username", username));
params.add(new BasicNameValuePair("finalpice", finalprice));
for (int i = 0; i < pid.length; i++) {
    params.add(new BasicNameValuePair("pid[]", pid[i]));
}   
for (int j = 0; j < products.length; j++) {
    params.add(new BasicNameValuePair("products[]", products[j]));
}
for (int k = 0; k < quantity.length; k++) {
    params.add(new BasicNameValuePair("quantity[]", quantity[k]));
}
for (int l = 0; l < totalprice.length; l++) {
    params.add(new BasicNameValuePair("totalprice[]", totalprice[l]));
}

JSONObject json = jsonParser.getJSONFromUrl(orderURL,params);
return json;
}