I got the error when to parse the JSON. Error parsing data org.json.JSONException: End of input at character 0 of.
我在解析JSON时遇到了错误。org.json误差解析数据。JSONException:在字符0处结束输入。
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:
这是在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;
}
The function in UserFunction class
UserFunction类中的函数。
/**
* 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:
java解析器类:
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
这是我的指数。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
索引。php调用这个orderdetails函数。
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
新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}
1 个解决方案
#1
0
DONE!!! After referring to Passing String array to PHP as POST I got the answer.
完成了! ! !在引用将字符串数组传递给PHP后,我得到了答案。
/**
* 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;
}
#1
0
DONE!!! After referring to Passing String array to PHP as POST I got the answer.
完成了! ! !在引用将字符串数组传递给PHP后,我得到了答案。
/**
* 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;
}