【问题标题】:JSON Object RequestJSON对象请求
【发布时间】: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

标签: php android json database


【解决方案1】:

只需将演员表移除到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) {

        }
    }
);

【讨论】:

  • 检查编辑并在问题中添加logcat输出。
  • 如果不进行强制转换,它不再给我错误,但它似乎也没有到达 onResponse 方法。
  • onErrorResponse方法中添加Log语句,看看是否有错误。
  • 我已经更新了我的 onErrorResponse 方法并通过检查 logcat 我得到了这个错误: Volley Error: com.android.volley.ParseError: org.json.JSONException: Value Connection of type java.lang。字符串无法转换为 JSONObject @KNeerajLal
猜你喜欢
  • 2018-12-18
  • 2017-09-18
  • 2017-05-04
  • 2012-01-28
  • 2014-06-26
  • 2014-12-08
  • 2020-05-18
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多