androidjsonandroid-volleyjsonexception

W/System.err: org.json.JSONException: Value Could of type java.lang.String cannot be converted to JSONArray


Although the question has been answered but the solution is not working for me. I'm using Volley library here. I'm getting response from the server as well but this is still causing the problem. Can u guys please tell me what I'm doing wrong?

My code:

StringRequest stringRequest = new StringRequest(Request.Method.POST, forgetPassword_url,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        Log.d("response", response);
                        try {

                            JSONArray jsonArray = new JSONArray(response);
                            JSONObject jsonObject = jsonArray.getJSONObject(0);
                            String code = jsonObject.getString("code");
                            if (code.equals("mail_send")) {
                                AlertDialog.Builder builder = new AlertDialog.Builder(Signin.this);
                                builder.setCancelable(false);
                                builder.setMessage("Password will be sent to your registered email id.");
                                builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
                                    @Override
                                    public void onClick(DialogInterface dialog, int which) {
                                        //if user pressed "yes", then he is allowed to exit from application
                                        dialog.cancel();
                                        EmailText.setVisibility(View.VISIBLE);
                                    }
                                });
                                AlertDialog alert = builder.create();
                                alert.show();

                            } else {
                                AlertDialog.Builder builder = new AlertDialog.Builder(Signin.this);
                                builder.setCancelable(false);
                                builder.setMessage("Email id is not registered.");
                                builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
                                    @Override
                                    public void onClick(DialogInterface dialog, int which) {
                                        //if user pressed "yes", then he is allowed to exit from application
                                        dialog.cancel();

                                    }
                                });
                                AlertDialog alert = builder.create();
                                alert.show();
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }

                    }
                }

Solution

  • Problem is your response is in JSONArray and you are doing String Request.

    Try this blow method.

    JsonArrayRequest jsonArrayRequest = new JsonArrayRequest(
                    Request.Method.GET,
                    mJSONURLString,
                    null,
                    new Response.Listener<JSONArray>() {
                        @Override
                        public void onResponse(JSONArray jsonArray) {
                            Log.d("response", response);
                            try {
    
                                JSONObject jsonObject = jsonArray.getJSONObject(0);
                                String code = jsonObject.getString("code");
                                if (code.equals("mail_send")) {
                                    AlertDialog.Builder builder = new AlertDialog.Builder(Signin.this);
                                    builder.setCancelable(false);
                                    builder.setMessage("Password will be sent to your registered email id.");
                                    builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
                                        @Override
                                        public void onClick(DialogInterface dialog, int which) {
                                            //if user pressed "yes", then he is allowed to exit from application
                                            dialog.cancel();
                                            EmailText.setVisibility(View.VISIBLE);
                                        }
                                    });
                                    AlertDialog alert = builder.create();
                                    alert.show();
    
                                } else {
                                    AlertDialog.Builder builder = new AlertDialog.Builder(Signin.this);
                                    builder.setCancelable(false);
                                    builder.setMessage("Email id is not registered.");
                                    builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
                                        @Override
                                        public void onClick(DialogInterface dialog, int which) {
                                            //if user pressed "yes", then he is allowed to exit from application
                                            dialog.cancel();
    
                                        }
                                    });
                                    AlertDialog alert = builder.create();
                                    alert.show();
                                }
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                        }
                    },
                    new Response.ErrorListener(){
                        @Override
                        public void onErrorResponse(VolleyError error){
                            // Do something when error occurred
                            Snackbar.make(
                                    mCLayout,
                                    "Error...",
                                    Snackbar.LENGTH_LONG
                            ).show();
                        }
                    }
            );