![](/img/trans.png)
[英]error. org.json.JSONException: Value <br of type java.lang.String cannot be converted to JSONObject
[英]encountered “org.json.JSONException: Value <br of type java.lang.String cannot be converted to JSONObject”
我仍然很陌生,需要幫助。 也許我錯過了什么...
這是我的PHP代碼
<?php
$con = mysqli_connect("localhost", "root", "", "customer");
$order_name = $_POST["order_name"];
$order_cust = $_POST["order_cust"];
$quantity = $_POST["quantity"];
$statement = mysqli_prepare($con, "INSERT INTO order (order_name, order_cust, quantity) VALUES (?, ?, ?)");
mysqli_stmt_bind_param($statement, "sss", $order_name, $order_cust, $quantity);
mysqli_stmt_execute($statement);
$response = array();
$response["success"] = true;
echo json_encode($response);
?>
然后這是我的回復代碼。
import com.android.volley.Response;
import com.android.volley.toolbox.StringRequest;
import java.util.HashMap;
import java.util.Map;
public class OrderRequest extends StringRequest {
private static final String ORDER_REQUEST_URL = "http://10.0.2.2/customer/Insert.php";
private Map<String, String> params;
public OrderRequest(String order_name, String order_cust, String quantity, Response.Listener<String> listener){
super(Method.POST, ORDER_REQUEST_URL, listener, null);
params = new HashMap<>();
params.put ("order_name", order_name);
params.put ("order_cust", order_cust);
params.put ("quantity", quantity);
}
@Override
public Map<String, String> getParams() {
return params;
}
}
最后,這是我的按鈕活動代碼...
bConfirm.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
final String user_id = etUsername.getText().toString();
final String date = etDate.getText().toString();
final String time = etTime.getText().toString();
final String pax = etPax.getText().toString();
Response.Listener<String> responseListener = new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try{
JSONObject jsonResponse = new JSONObject(response);
boolean success = jsonResponse.getBoolean("success");
if(success){
AlertDialog.Builder builder = new AlertDialog.Builder(ReservationActivity.this);
builder.setMessage("Success! Your reservation has been added to our Queue!").setNegativeButton("Confirm", null).create().show();
}else{
AlertDialog.Builder builder = new AlertDialog.Builder(ReservationActivity.this);
builder.setMessage("Reservation failed or missing credentials...").setNegativeButton("Retry", null).create().show();
}
} catch (JSONException e) {
e.printStackTrace();
}
}
};
ReservationRequest reservationRequest = new ReservationRequest(user_id, date, time, pax, responseListener);
RequestQueue queue = Volley.newRequestQueue(ReservationActivity.this);
queue.add(reservationRequest);
}
});
這是LogCat
W/System.err: org.json.JSONException: Value <br of type java.lang.String cannot be converted to JSONObject
我需要幫助,請...
您請求的網址不是發送JSON,而是發送html。 因此,您不能將其解析為JSON。 端點錯誤,根本不發送JSON或錯誤發送HTML。 或者您有服務器錯誤。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.