【问题标题】:W/System.err: org.json.JSONException: Value Could of type java.lang.String cannot be converted to JSONArrayW/System.err:org.json.JSONException:Java.lang.String 类型的值无法转换为 JSONArray
【发布时间】:2019-03-13 11:00:49
【问题描述】:

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

                    }
                }

【问题讨论】:

  • 检查您的回复,我认为它不是 json 数组
  • 这是我得到的响应:[{"code":"mail_send","message":"mail send to the register email id","Password":"12345"}]跨度>
  • 发布完成异常
  • 03-13 16:25:19.222 10109-10109/com.Dignc.texrecruit D/响应:无法执行:/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 : java.lang.String 类型的值不能转换为 JSONArray

标签: android json android-volley jsonexception


【解决方案1】:

问题是您的响应在 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();
                    }
                }
        );

【讨论】:

  • 让我试试这个兄弟
  • 替换成forgetPassword_url。
  • 是的,我想,错误已经消失了兄弟。谢了哥们。你能更好地解释一下我在做什么吗??
  • 问题是你在做 StringRequest 当你得到 JSONArray 在这种情况下你需要做 JSONArray Request 而不是 StringRequest。尝试理解 JSONArray、JsonObject 和 String 的区别。
  • 现在知道了,我会记住这一点的,谢谢 G'Day。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-07-12
  • 2013-08-24
  • 2016-02-21
  • 1970-01-01
相关资源
最近更新 更多