JSON对象请求

时间:2016-11-06 16:22:07

标签: php android json database

我正在使用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);
}
?>

1 个答案:

答案 0 :(得分:0)

只需将广告素材移至String即可。试试这个,

JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.POST, showUrl, null,
    new Response.Listener<JSONObject>(){

        @Override
        public void onResponse(JSONObject response) {

        }
    }
);

错误表明服务器正在返回String输出。您可以使用StringRequest来解决错误,

StringRequest stringRequest = new StringRequest(Request.Method.POST, showUrl,
    new Response.Listener<String>() {

        @Override
        public void onResponse(String response) {
            Log.e("response", response);
        }
    },
    new Response.ErrorListener() {

        @Override
        public void onErrorResponse(VolleyError error) {

        }
    }
);