0

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

                    }
                }
  • 1
    Check your response , I think its not json array – Manohar Mar 13 '19 at 11:02
  • This is the response m getting: [{"code":"mail_send","message":"mail send to the register email id","Password":"12345"}] – Rajveer Bedi Mar 13 '19 at 11:03
  • post complete exception – Manohar Mar 13 '19 at 11:04
  • 03-13 16:25:19.222 10109-10109/com.Dignc.texrecruit D/response: Could not execute: /usr/sbin/sendmail [{"code":"mail_send","message":"mail send to the register email id","Password":"12345"}] 03-13 16:25:19.222 10109-10109/com.Dignc.texrecruit W/System.err: org.json.JSONException: Value Could of type java.lang.String cannot be converted to JSONArray – Rajveer Bedi Mar 13 '19 at 11:07

1 Answers1

0

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