我嘗試將數據放入我的數據庫,但我有例外,如主題。將String轉換爲JSONObject可能是問題,但我不知道如何解決這個問題。Android排除異常:org.json.JSONException:值<br><java.lang.String類型的表不能轉換爲JSONObject
這裏是我的OrderRequest
:
public OrderRequest(String orderNumber, String date, String adress, int phone, String email, String orderCode, int price, String time, int state, Response.Listener<String> listener){
super(Method.POST, ORDER_REQUEST_URL, listener, null);
params = new HashMap<>();
params.put("ordernumber", orderNumber);
params.put("data", date);
params.put("adress", adress);
params.put("phone", phone + "");
params.put("email", email);
params.put("ordercode", orderCode);
params.put("price", price + "");
params.put("time", time);
params.put("state", state + "");
}
@Override
public Map<String, String> getParams(){
return params;
}
這是我的PHP代碼:
<?php
$con = <-here is password, etc...
$ordernumber = $_POST["ordernumber"];
$date = $_POST["date"];
$adress = $_POST["adress"];
$phone = $_POST["phone"];
$email = $_POST["email"];
$ordercode = $_POST["ordercode"];
$price = $_POST["price"];
$time = $_POST["time"];
$state = $_POST["state"];
$statement = mysqli_prepare($con, "INSERT INTO order (ordernumber, date, adress, phone, email, ordercode, price, time, state) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)");
mysqli_stmt_bind_param($statement, "sssissisi", $ordernumber, $date, $adress, $phone, $email, $ordercode, $price, $time, $state);
mysqli_stmt_execute($statement);
$response = array();
$response["success"] = true;
echo json_encode($response);
>
這裏是我的Java代碼:
Response.Listener<String> responsListener = new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try {
JSONObject jsonResponse = new JSONObject(response);
boolean success = jsonResponse.getBoolean("success");
if (success) {
Intent intent = new Intent(FinalizeActivity.this, LoggedActivity.class);
FinalizeActivity.this.startActivity(intent);
} else {
}
} catch (JSONException e) {
e.printStackTrace();
}
}
};
OrderRequest orderRequest = new OrderRequest(orderNumberO, dateO, adressO, phoneO, emailO, orderCodeO, priceO, timeO, stateO, responsListener);
RequestQueue queue = Volley.newRequestQueue(FinalizeActivity.this);
queue.add(orderRequest);
}
}
});
在其他放在我的ap我有一個類似的代碼,然後它的工作。
你在某處迴應HTML,這是你的例外告訴你的。因爲HTML不能被解析爲JSON,所以你會得到一個異常 – 0xDEADC0DE
它的'data' not'date'在php代碼 –
中尋找拼寫錯誤,你得到一個HTML內容的響應,並且在這一行中'new JSONObject(response);'它不能轉換它。首先找出你的迴應,然後如果它是json格式,請將它轉換爲Notepad ++中的 –