简体   繁体   中英

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. 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. Both the url and the php files work fine. 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 . 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. Printscreen with the json data Can anybody help me? Thanks in advance

Instead of JSONRequest try StringRequest and check if your response is valid json or not. JSONRequest tries to parse String into JSONObject and then returns and this will fail if response is not of type JSON .

You need to test if you web service return a values. You can test this with your navigator or with Postman for example.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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