简体   繁体   English

JSON对象响应错误

[英]JSON Object Response Error

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);
}

I am using a JSON object request to get information from a database that I have created. 我正在使用JSON对象请求从我创建的数据库中获取信息。 I am able with my android application to insert data into the database but now I want to check for example if the user created an user account. 我可以使用我的android应用程序将数据插入数据库,但是现在我想检查例如用户是否创建了用户帐户。 Both the url and the php files work fine. url和php文件都可以正常工作。 I have used a log in the onErrorResponse method to see what is the actual error and I got this: E/Volley Error: com.android.volley.ParseError: org.json.JSONException: Value Connection of type java.lang.String cannot be converted to JSONObject . 我在onErrorResponse方法中使用了一个日志来查看实际的错误是什么,并且得到了以下信息: E / Volley错误:com.android.volley.ParseError:org.json.JSONException:类型为java.lang.String的值连接无法被转换为JSONObject I do not know what seems to be the problem with this. 我不知道这有什么问题。 I have also checked using postamn if my web service is returning json data. 我还使用postamn检查了我的Web服务是否返回json数据。 Printscreen with the json data Can anybody help me? 带有json数据的Printscreen有人可以帮助我吗? Thanks in advance 提前致谢

Instead of JSONRequest try StringRequest and check if your response is valid json or not. 尝试使用StringRequest而不是JSONRequest并检查您的响应是否为有效的json。 JSONRequest tries to parse String into JSONObject and then returns and this will fail if response is not of type JSON . JSONRequest尝试将String解析为JSONObject ,然后返回,如果响应的类型不是JSON ,这将失败。

You need to test if you web service return a values. 您需要测试Web服务是否返回值。 You can test this with your navigator or with Postman for example. 您可以使用导航器或Postman进行测试。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM