AndroidVolley:JSONArray jsonArray = new JSONArray(response);导致例外

时间:2017-12-21 02:05:11

标签: php android json android-studio android-volley

我需要一些帮助。每当我实施:

JSONArray jsonArray = new JSONArray(response); 

它导致我的代码跳转到异常块。

我正在使用php方法:

echo json_encode(array("code"=>$code));

将JSON字符串传递给我的应用程序。我得到的原始响应字符串是:

{"code":"UserIsValid"}

我查看了文档,它说JSONArray(String json)是初始化JSON数组的有效方法。我正在使用Volley与我的数据库进行通信,这似乎运行良好。我的相关应用程序代码如下所示:

StringRequest postRequest = new StringRequest(Request.Method.POST, server_url,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        // response
                        try {
                            JSONArray jsonArray = new JSONArray(response);
                            Toast.makeText(RegisterActivity.this, response, Toast.LENGTH_SHORT).show();

                        } catch (JSONException e) {
                            e.printStackTrace();
                            Toast.makeText(RegisterActivity.this, "failed", Toast.LENGTH_SHORT).show();
                        }
                    }
                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        // error response
                        Toast.makeText(RegisterActivity.this, "Server file not found", Toast.LENGTH_SHORT).show();
                    }
                }
        ) {
            @Override
            protected Map<String, String> getParams() {
                Map<String, String> params = new HashMap<String, String>();
                params.put("name", user_name);
                return params;
            }
        };
        //Adds post request to request queue using MySingleton class.
        MySingleton.getInstance(RegisterActivity.this).addToRequestQueue(postRequest);

我很高兴有任何帮助!

2 个答案:

答案 0 :(得分:0)

不,这是Unexpected error running Liquibase: Unknown parameter: 'masterUsername' SEVERE 12/21/17 12:59 PM:liquibase: Unexpected error running Liquibase: Unknown parameter: 'masterUsername' liquibase.exception.CommandLineParsingException: Unknown parameter: 'masterUsername' at liquibase.integration.commandline.Main.parsePropertiesFile(Main.java:387) at liquibase.integration.commandline.Main.main(Main.java:122) 。您应该尝试使用Json Object代替:

JSONObject

答案 1 :(得分:0)

由于您要从PHP发送JSONObject作为响应,因此必须使用JSONObject类将字符串解析为JSON。请尝试以下

StringRequest postRequest = new StringRequest(Request.Method.POST, server_url,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        // response
                        try {
                            JSONObject jsonObject = new JSONObject(response);
                            Toast.makeText(RegisterActivity.this, jsonObject.getString("code"), Toast.LENGTH_SHORT).show();

                        } catch (JSONException e) {
                            e.printStackTrace();
                            Toast.makeText(RegisterActivity.this, "failed", Toast.LENGTH_SHORT).show();
                        }
                    }
                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        // error response
                        Toast.makeText(RegisterActivity.this, "Server file not found", Toast.LENGTH_SHORT).show();
                    }
                }
        ) {
            @Override
            protected Map<String, String> getParams() {
                Map<String, String> params = new HashMap<String, String>();
                params.put("name", user_name);
                return params;
            }
        };
        //Adds post request to request queue using MySingleton class.
        MySingleton.getInstance(RegisterActivity.this).addToRequestQueue(postRequest);

在PHP中编码多维数组时,结果将采用JSONArray格式,否则将编码为JSONObject。