JSON对象响应错误

时间:2016-11-06 19:15:41

标签: php android json jsonobject

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错误:com.android.volley.ParseError:org.json.JSONException:类型为java.lang的值的连接.String无法转换为JSONObject 。我不知道这个问题到底是什么。如果我的网络服务正在返回json数据,我还使用postamn进行了检查。Printscreen with the json data  有谁能够帮我?提前致谢

2 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:0)

您需要测试Web服务是否返回值。 您可以使用导航器或Postman进行测试。