错误分析数据org.json.JSONException:输入的字符0结束字符、错误、结束、数据

2023-09-03 23:03:51 作者:一阵可爱风

我得到的错误时解析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解析器(24857):错误分析数据org.json.JSONException:输入的字符0结束
04-19 20:51:35.600:E / ViewRootImpl(24857):sendUserActionEvent()MVIEW == NULL
 

这是在$ C $下调用UserFunction类的函数:

  pTotalPrice =新的双[cartSize] //商店totalprice阵列

    / **发送到DB ** /
    SPID =新的String [cartSize]
    sProduct =新的String [cartSize]
    sQuantity =新的String [cartSize]
    sTotalPrice =新的String [cartSize]

    如果(cartSize大于0)
    {
        的for(int i = 0; I< cartSize;我++)
        {
            最终诠释柜台=我;
            //获取从产品数据的ArrayList probuct数据
            字符串PID = aController.getProducts(我).getProductId();
            SPID [我] = PID;
            字符串PNAME = aController.getProducts(我).getProductName();
            sProduct [我] = PNAME;
            双pPrice = aController.getProducts(ⅰ).getProductPrice();
            INT pQuantity = aController.getProducts(我).getProductQuantity();
            sQuantity [I] = Integer.toString(pQuantity);
            pTotalPrice [我] = pPrice * pQuantity;
            sTotalPrice [I] = Double.toString(pTotalPrice [I]);
            }
    pFinalPrice  -  = pTotalPrice [窗口];
sFinalPrice = Double.toString(pFinalPrice);
    }
    受保护的JSONObject doInBackground(字符串参数... args){


            UserFunctions userFunction =新UserFunctions();
            JSONObject的JSON = userFunction.orderDetails(用户名,SPID,sProduct,sQuantity,sTotalPrice,Double.toString(pFinalPrice));

            Log.d(按钮,订单);
            返回JSON;


        }
 

在UserFunction类的功能

  / **
 *功能店的订单详细信息
 ** /
公众的JSONObject订单明细(字符串username,字符串[] PID,的String []产品的String []量的String [] totalprice,字符串finalprice){
    //大厦参数
    名单PARAMS =新的ArrayList();
    params.add(新BasicNameValuePair(标签,order_tag));
    params.add(新BasicNameValuePair(用户名,用户名));
    params.add(新BasicNameValuePair(finalpice,finalprice));
    的for(int i = 0; I< pid.length;我++){
        params.add(新BasicNameValuePair(PID [I],PID [I]));
    }
    对于(INT J = 0; J< products.length; J ++){
        params.add(新BasicNameValuePair(产品的研究[J],产品的研究[J]));
    }
    对于(INT K = 0; K< quantity.length; k ++){
        params.add(新BasicNameValuePair(量[K]量[K]));
    }
    对于(INT L = 0; L< totalprice.length,L ++){
        params.add(新BasicNameValuePair(totalprice [升],totalprice [升]));
    }

    JSONObject的JSON = jsonParser.getJSONFromUrl(orderURL,则params);
    返回JSON;
}
 
java出现大量Exception org.json.JSONException,无报错,换了同学的电脑也可以运行,,请问是什么原因

Java的语法分析器类:

 公共类JSONParser {
静态的InputStream是= NULL;
静态的JSONObject jObj = NULL;
静态JSON字符串=;
//构造
公共JSONParser(){
}
公众的JSONObject getJSONFromUrl(字符串URL,列表PARAMS){
    //使HTTP请求
    尝试 {
        // defaultHttpClient
        DefaultHttpClient的HttpClient =新DefaultHttpClient();
        HttpPost httpPost =新HttpPost(URL);
        httpPost.setEntity(新UrlEn codedFormEntity(PARAMS));
        HTT presponse HTT presponse = httpClient.execute(httpPost);
        HttpEntity httpEntity = HTT presponse.getEntity();
        是= httpEntity.getContent();
    }赶上(UnsupportedEncodingException E){
        e.printStackTrace();
    }赶上(ClientProtocolException E){
        e.printStackTrace();
    }赶上(IOException异常E){
        e.printStackTrace();
    }
    尝试 {
        的BufferedReader读卡器=新的BufferedReader(新的InputStreamReader(
                是,ISO-8859-1),8);
        StringBuilder的SB =新的StringBuilder();
        串线= NULL;
        而((行= reader.readLine())!= NULL){
            sb.append(行+\ N);
        }
        is.close();
        JSON = sb.toString();
        Log.e(JSON,JSON);
    }赶上(例外五){
        Log.e(缓冲区错误,转换的结果错误+ e.toString());
    }
    //尝试解析字符串到一个JSON对象
    尝试 {
        jObj =新的JSONObject(JSON);
    }赶上(JSONException E){
        Log.e(JSON解析器,错误分析数据+ e.toString());
    }
    //返回JSON字符串
    返回jObj;
}
}
 

这是我的index.php API

 否则,如果($标记=='秩序'){
    $用户名= $ _ POST ['用户名'];
    $ finalprice = $ _ POST ['finalprice'];
    $ PID = $ _ POST ['PID'];

    $数量= $ _ POST ['量'];
    $ totalprice = $ _ POST ['totalprice'];

            $响应[successfullypost] = 1;
    $响应[用户] [用户名] = $的用户名;
    $响应[用户] [finalprice] = $ finalprice;
    $响应[用户] [PID] = $ PID;
    $响应[用户] [数量] = $数量;
    $响应[用户] [totalprice] = $ totalprice;
    回声json_en code($响应);

    $ uResult =请求mysql_query(SELECT * FROM用户其中username = $用户名);
    $ UID = $ uResult ['UID'];

    $计数器= sizeof的($ PID);

    为($ i = 0; $ I< $计数器; $ I ++){
        $ DB->订单明细($ UID,$ PID [$ i],$数量[$ i],$ totalprice [$ i],$ finalprice);
    }

}

其他 {
    $响应[错误] = 3;
    $响应[ERROR_MSG] =​​JSON错误;
    回声json_en code($响应);
}
 

index.php文件调用这个函数订单明细

 公共职能订单明细($ UID,$ PID,$数量,$ totalprice,$ finalprice){

    $presult =请求mysql_query(SELECT * FROM产品其中pid = $ PID);
    $ PRODUCT_ID = $presult ['产品id'];

    $最后=请求mysql_query(INSERT INTO vieworders(UID,产品ID,数量,$ totalprice,finalprice)
        VALUES('$ UID','$产品id','$量,$ totalprice','$ finalprice'));

}
 

新的JSON响应。虽然我的产品也不会显示阵列。为什么JSON标签显示'我',而不是PID [I]?相同的数量和totalprice

  10月4日至20日:19:31.615:E / ViewRootImpl(20740):sendUserActionEvent()MVIEW == NULL
10月4日至二十日: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}
 

解决方案

完成!谈到以后传递字符串数组PHP作为POST 我得到了答案。

  / **
*功能店的订单详细信息
** /
公众的JSONObject订单明细(字符串username,字符串[] PID,的String []产品的String []量的String [] totalprice,字符串finalprice){
//大厦参数
名单PARAMS =新的ArrayList();
params.add(新BasicNameValuePair(标签,order_tag));
params.add(新BasicNameValuePair(用户名,用户名));
params.add(新BasicNameValuePair(finalpice,finalprice));
的for(int i = 0; I< pid.length;我++){
    params.add(新BasicNameValuePair(PID [],PID [I]));
}
对于(INT J = 0; J< products.length; J ++){
    params.add(新BasicNameValuePair(产品[],产品的研究[J]));
}
对于(INT K = 0; K< quantity.length; k ++){
    params.add(新BasicNameValuePair(量[],数量[K]));
}
对于(INT L = 0; L< totalprice.length,L ++){
    params.add(新BasicNameValuePair(totalprice [],totalprice [升]));
}

JSONObject的JSON = jsonParser.getJSONFromUrl(orderURL,则params);
返回JSON;
}
 

I got the error when to parse the JSON. Error parsing data org.json.JSONException: End of input at character 0 of.

error log:

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

This is the code for calling a function in UserFunction class:

    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;


        }

The function in UserFunction class

/**
 * 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[i]", pid[i]));
    }   
    for (int j = 0; j < products.length; j++) {
        params.add(new BasicNameValuePair("products[j]", products[j]));
    }
    for (int k = 0; k < quantity.length; k++) {
        params.add(new BasicNameValuePair("quantity[k]", quantity[k]));
    }
    for (int l = 0; l < totalprice.length; l++) {
        params.add(new BasicNameValuePair("totalprice[l]", totalprice[l]));
    }

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

The java parser class:

public class JSONParser {
static InputStream is = null;
static JSONObject jObj = null;
static String json = "";
// constructor
public JSONParser() {
}
public JSONObject getJSONFromUrl(String url, List params) {
    // Making HTTP request
    try {
        // defaultHttpClient
        DefaultHttpClient httpClient = new DefaultHttpClient();
        HttpPost httpPost = new HttpPost(url);
        httpPost.setEntity(new UrlEncodedFormEntity(params));
        HttpResponse httpResponse = httpClient.execute(httpPost);
        HttpEntity httpEntity = httpResponse.getEntity();
        is = httpEntity.getContent();
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    } catch (ClientProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    try {
        BufferedReader reader = new BufferedReader(new InputStreamReader(
                is, "iso-8859-1"), 8);
        StringBuilder sb = new StringBuilder();
        String line = null;
        while ((line = reader.readLine()) != null) {
            sb.append(line + "\n");
        }
        is.close();
        json = sb.toString();
        Log.e("JSON", json);
    } catch (Exception e) {
        Log.e("Buffer Error", "Error converting result " + e.toString());
    }
    // try parse the string to a JSON object
    try {
        jObj = new JSONObject(json);
    } catch (JSONException e) {
        Log.e("JSON Parser", "Error parsing data " + e.toString());
    }
    // return JSON String
    return jObj;
}
}

This is my 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);
}

index.php call this orderdetails function

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')");

}

The new JSON Response. although I have to products it don't show the array. why the JSON tag show 'i' instead of pid[i]? same for quantity and totalprice

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}

解决方案

DONE!!! After referring to Passing String array to PHP as POST I got the answer.

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

 
精彩推荐
图片推荐