【问题标题】:JSON Object Response ErrorJSON 对象响应错误
【发布时间】:2016-11-06 19:15:40
【问题描述】:
public void searchLoginInfo(View view) {
    JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.POST, showUrl,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").toLowerCase();
                    String password = user.getString("password".toLowerCase());
                    String retypedpassword = user.getString("retypedpassword");
                    String email = user.getString("email");
                    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);
}

我正在使用 JSON 对象请求从我创建的数据库中获取信息。我可以使用我的 android 应用程序将数据插入数据库,但现在我想检查例如用户是否创建了用户帐户。 url 和 php 文件都可以正常工作。我在 onErrorResponse 方法中使用了日志来查看实际错误是什么,我得到了这个:E/Volley Error: com.android.volley.ParseError: org.json.JSONException: Value Connection of type java.lang .String 无法转换为 JSONObject。我不知道这似乎是什么问题。如果我的 Web 服务返回 json 数据,我还使用 postamn 检查过。Printscreen with the json data 有谁能够帮我?提前致谢

【问题讨论】:

  • 听起来 Volley 无法转换为 json 无论您传递什么字符串。我们真的需要查看传递给 Volley 的内容,以确定它有什么问题。
  • @saywhatnow 我该怎么做?
  • 在每一步,Log.debug 数据,这样您就可以看到您实际使用的内容。如果您不明白为什么某事以特定方式表现,请从小块中查看它。 :)

标签: php android json jsonobject


【解决方案1】:

代替JSONRequest 尝试StringRequest 并检查您的响应是否为有效的json。 JSONRequest 尝试将 String 解析为 JSONObject 然后返回,如果响应不是 JSON 类型,这将失败。

【讨论】:

    【解决方案2】:

    您需要测试您的网络服务是否返回值。 例如,您可以使用导航器或 Postman 进行测试。

    【讨论】:

    • 我是使用邮递员完成的,它返回 json 数据。我上传了一张照片。 @SofDroid
    • 你应该用'JsonArrayRequest'修改'JsonObjectRequest'对象
    • 和 JSONObject 和 JSONArray
    • 我这样做了,我得到了完全相同的错误。唯一的区别是现在它说字符串不能像以前那样转换为 JSONObject 而不是 JSONArray。
    • 所以,您的网络服务器 Json 结构有错误。
    猜你喜欢
    • 1970-01-01
    • 2019-02-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-07
    • 1970-01-01
    • 2019-01-26
    • 2022-01-15
    相关资源
    最近更新 更多