-1
我在這裏掙扎了一點,我在我的應用程序中有一個研究按鈕,它將調用一個PHP文件來執行SELECT(並獲取幾行代碼) 。這裏是我的PHP文件:java.lang.String類型的值無法轉換爲JSONObject
$con = mysqli_connect("***", "***", "***", "***");
$city = $_POST["city"];
$statement = mysqli_prepare($con, "SELECT name FROM Restaurants WHERE city = ? ");
mysqli_stmt_bind_param($statement, "s", $city);
mysqli_stmt_execute($statement);
mysqli_stmt_store_result($statement);
mysqli_stmt_bind_result($statement, $city);
$response = array();
$response["success"] = false;
$i=-1
while(mysqli_stmt_fetch($statement)){
$response["success"] = true;
while($row = mysqli_fetch_array($statement)){
$i++;
$response["name"][$i]=$row[$i];
$response["name"][$i]=$row[$i];
}
}
echo json_encode($response);
我從.java文件中收到錯誤。這裏是按鈕聽衆在我的.java文件:
public void bSearchRestaurantClicked(View v) {
final EditText etCity = (EditText) findViewById(R.id.etSearchRestaurantsCity);
final String city = etCity.getText().toString();
Response.Listener<String> responseListener = new Response.Listener<String>() {
public void onResponse(String response) {
try {
JSONObject jsonResponse = new JSONObject(response);
boolean success = jsonResponse.getBoolean("success");
if (success) {
String nameCities = jsonResponse.getString("name");
System.out.println("=======> "+nameCities);
Intent intent = new Intent(AreaActivityClient.this, ResultSearchActivity.class);
intent.putExtra("city", city);
intent.putExtra("cities", nameCities);
AreaActivityClient.this.startActivity(intent);
} else {
AlertDialog.Builder builder = new AlertDialog.Builder(AreaActivityClient.this);
builder.setMessage("Login Failed !").setNegativeButton("Retry", null).create().show();
}
} catch (JSONException e) {
e.printStackTrace();
}
}
};
SearchRequest searchRequest = new SearchRequest(city, responseListener);
RequestQueue queue = Volley.newRequestQueue(AreaActivityClient.this);
queue.add(searchRequest);
的錯誤是在這一行:
JSONObject jsonResponse = new JSONObject(response);
這裏是我的SearchRequest.java文件:
private static final String SEARCH_REQUEST_URL="file.php";
private Map<String,String> params;
public SearchRequest(String city, Response.Listener<String> listener){
super(Request.Method.POST,SEARCH_REQUEST_URL,listener,null);
params = new HashMap<>();
params.put("city",city);
}
我很確定錯誤來自.php文件,但我找不到它... 預先感謝您的答案。