W/System.err: org.json.JSONException: java.lang.String 类型的值无法转换为 JSONArray

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

虽然问题已得到解答,但解决方案对我不起作用。我在这里使用 Volley 库。我也从服务器收到响应,但这仍然是导致问题的原因。你们能告诉我我做错了什么吗?

我的代码:

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

                    }
                }

问题是您的响应在 JSONArray 中并且您正在执行字符串请求。

试试这个吹法

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