【问题标题】:org.json.JSONException: Value Connection of type java.lang.String cannot be converted to JSONObjectorg.json.JSONException:java.lang.String 类型的值连接无法转换为 JSONObject
【发布时间】:2016-07-09 16:59:04
【问题描述】:

我正在为我的应用程序编写一个登录函数,但我突然开始收到此错误:org.json.JSONException: Value Connection of type java.lang.String cannot be converted to JSONObject 我完全不记得在这里修改过代码,它以前可以工作。当我开始在 android studio 上使用调试模式时,它突然停止工作。

这是我的代码的 sn-p,错误似乎在标有 /*ERROR*/ 的 2 行上:

private String loginUrl = "http://10.0.2.2/user/login.php";

login.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            request = new StringRequest(Request.Method.POST, loginUrl, new Response.Listener<String>() /*ERROR*/ {
                @Override
                public void onResponse(String response) {
                    try {
                        JSONObject jsonObject = new JSONObject(response); /*ERROR*/
                        if (jsonObject.names().get(0).equals("success")) {
                            Toast.makeText(getApplicationContext(), jsonObject.getString("success"), Toast.LENGTH_SHORT).show();
                            email.setText("");
                            password.setText("");
                            Log.d("test", "Going to Main.class");
                            startActivity(new Intent(MainActivity.this, Main.class));
                            //setContentView(R.layout.activity_main);
                        } else {
                            Toast.makeText(getApplicationContext(), jsonObject.getString("error"), Toast.LENGTH_SHORT).show();
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            }, new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                }
            }) {
                @Override
                protected Map<String, String> getParams() throws AuthFailureError {
                    Map<String, String> parameters = new HashMap<String, String>();
                    parameters.put("email", email.getText().toString());
                    parameters.put("password", password.getText().toString());
                    return parameters;
                }
            };
            requestQueue.add(request);
        }
    }); 

【问题讨论】:

  • response的值是多少?

标签: java android mysql json android-studio


【解决方案1】:

org.json.JSONObject(String json) 使用来自 JSON 字符串的名称/值映射创建一个新的 JSONObject

此异常意味着您的 response 参数不包含有效的 JSON

【讨论】:

    【解决方案2】:

    似乎response 对象的格式不正确,无法转换为 JSON 对象。 Example.

    【讨论】:

    • 是的,我一定是在向某人展示 php 文件时做了一些事情。
    猜你喜欢
    • 2016-11-29
    • 1970-01-01
    • 1970-01-01
    • 2012-05-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多