值成功的类型java.lang.String无法转换为JSONArray

时间:2018-01-16 10:05:56

标签: android

final ProgressDialog loading = ProgressDialog.show(Login.this, "Verifying", "Please wait...", false, false);
    StringRequest stringRequest = new StringRequest(Request.Method.POST, Config.LOGIN_URL,
    new Response.Listener<String>() {
    @Override
     public void onResponse(String response) {
     try {
      loading.dismiss();
      JSONArray jsonArray = new JSONArray(response);
      JSONObject jsonObject = jsonArray.getJSONObject(0);
      String code = jsonObject.getString("code");
         if (code.equals("login_failed")) {
             builder.setTitle("Login Error");
             displayAlert(jsonObject.getString("message"));
             } else {
               Intent intent = new Intent(Login.this, Success.class);
               Bundle bundle = new Bundle();
               bundle.putString("name", jsonObject.getString("name"));
               bundle.putString("email", jsonObject.getString("email"));
               intent.putExtras(bundle);
               startActivity(intent);

           }
        }

3 个答案:

答案 0 :(得分:0)

因为您正在尝试将字符串数据解析为JSONArray

这里

JSONArray jsonArray=new JSONArray(response);

将此更改为

JSONObject jsonObject =new JSONObject(response);

答案 1 :(得分:0)

我猜变量response中的值包含除array之外的其他值。

尝试使用json对象并打印/调试该值。

new Response.Listener<String>() {
    @Override
    public void onResponse(String response) {
        try {
        loading.dismiss();
         JSONObject jsonObj = new JSONObject(response);
         }
        }
    }

答案 2 :(得分:0)

Response.Listner<String>更改为JSONArray,如下面的代码

 new Response.Listener<JSONArray>() {
                                @Override
                                public void onResponse(JSONArray response) {
                                    try {
                                        loading.dismiss();
                                        JSONArray jsonArray=new JSONArray(response);
                                        JSONObject jsonObject=jsonArray.getJSONObject(0);
                                         String code=jsonObject.getString("code");
                                        if(code.equals("login_failed"))
                                        {

                                            builder.setTitle("Login Error");
                                            displayAlert(jsonObject.getString("message"));
                                        }
                                        else {

                                            Intent intent=new Intent(Login.this,Success.class);
                                            Bundle bundle=new Bundle();
                                            bundle.putString("name",jsonObject.getString("name"));
                                            bundle.putString("email",jsonObject.getString("email"));
                                            intent.putExtras(bundle);
                                            startActivity(intent);

                                        }