解析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;
}
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;
}
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;
}
}
这是我的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调用此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')");
}
新的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}
完成!!!在将Passing String数组作为POST引用到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;
}