【发布时间】:2016-11-06 16:22:06
【问题描述】:
我正在使用 json 对象请求从我创建的数据库中获取信息。我可以使用我的 android 应用程序将数据插入数据库,但现在我想检查例如用户是否创建了用户帐户。 尝试创建 json 对象请求时出现语法错误。这在某种程度上是争论的问题,但我不明白似乎是什么问题。 url 和 php 文件都可以正常工作。
String showUrl = "http://192.168.0.16/webapps/showUser.php";
public void searchLoginInfo(View view) {
JsonObjectRequest jsonObjectRequest = new /*HERE IS WHERE I GET THE ERROR*/ JsonObjectRequest(Request.Method.POST,showUrl,(String)null, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try {
JSONArray users = response.getJSONArray("users");
for (int i = 0; i < users.length(); i++){
JSONObject user = users.getJSONObject(i);
String username = user.getString("username");
String password = user.getString("password");
if (username.equals(myLoginList.get(0)) && password.equals(myLoginList.get(1))) {
Toast.makeText(LoginActivity.this, "Login Succesfull", Toast.LENGTH_LONG).show();
Intent send = new Intent(LoginActivity.this, WelcomeActivity.class);
startActivity(send);
break;
}else{
Toast.makeText(LoginActivity.this, "Login Failed!", Toast.LENGTH_LONG).show();
Intent send = new Intent(LoginActivity.this, LoginActivity.class);
startActivity(send);
}
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.e("Volley Error", error.toString());
NetworkResponse networkResponse = error.networkResponse;
if (networkResponse != null) {
Log.e("Status code", String.valueOf(networkResponse.statusCode));
}
}
});
requestQueue.add(jsonObjectRequest);
}
这是我得到的错误:错误:(60、98)错误:不兼容的类型:字符串无法转换为 JSONObject。
这是我的 showUser.php
<?php
if($_SERVER["REQUEST_METHOD"]=="POST"){
include 'connection.php';
showUser();
}
function showUser(){
global $connect;
$query = "SELECT* FROM user;";
$result = mysqli_query($connect,$query);
$number_of_rows = mysqli_num_rows($result);
$temp_array = array();
if($number_of_rows>0){
while($row = mysqli_fetch_assoc($result)){
$temp_array[] = $row;
}
}
header('Content-Type:application/json');
json_encode(array("users"=>$temp_array));
mysqli_close($connect);
}
?>
【问题讨论】:
-
把错误贴出来就更好了
-
@SarmadAijaz 错误:(60, 98) 错误:不兼容的类型:字符串无法转换为 JSONObject
-
response变量中的值是多少? -
它甚至没有到达那条线。错误在线: JsonObjectRequest jsonObjectRequest = new /*HERE IS WHERE I GET THE ERROR*/ JsonObjectRequest(Request.Method.POST,showUrl,(String)null, new Response.Listener
() { @KNeerajLal跨度> -
@KNeerajLal 错误:(60, 98) 错误:不兼容的类型:字符串无法转换为 JSONObject