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

时间:2019-03-13 11:00:49

标签: android json android-volley jsonexception

尽管问题已经回答,但解决方案对我不起作用。我在这里使用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();
                        }

                    }
                }

1 个答案:

答案 0 :(得分:0)

问题是您的响应位于JSONArray中,并且您正在执行String Request。

尝试这种打击方法。

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