【问题标题】:org.json.JSONException: No value for nameorg.json.JSONException:名称没有值
【发布时间】:2016-11-01 23:53:36
【问题描述】:

以下代码中出现此错误的原因可能是什么?

loginButton.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick (View v){
                final String e_mail = e_mailEditText.getText().toString();
                final String password = passwordEditText.getText().toString();

                // Response received from the server
                Response.Listener<String> responseListener = new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        try {
                            JSONObject jsonResponse = new JSONObject(response);
                            boolean success = jsonResponse.getBoolean("success");

                            if (success) {
                                String name = jsonResponse.getString("name");
                                //  int age = jsonResponse.getInt("age");

                                Intent intent = new Intent(login.this, Welcome.class);
                                intent.putExtra("name", name);
                                // intent.putExtra("age", age);
                                intent.putExtra("e_mail", e_mail);
                                login.this.startActivity(intent);
                            } else {
                                AlertDialog.Builder builder = new AlertDialog.Builder(login.this);
                                builder.setMessage("Login Failed")
                                        .setNegativeButton("Retry", null)
                                        .create()
                                        .show();
                            }

                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                };

                LoginRequest loginRequest = new LoginRequest(e_mail, password, responseListener);
                RequestQueue queue = Volley.newRequestQueue(login.this);
                queue.add(loginRequest);
            }
        });

【问题讨论】:

  • 感谢 Dan Roche,感谢上帝
  • 我该如何修复它我已将其注释掉

标签: json android-studio


【解决方案1】:

先检查是否有钥匙:

if (jsonObject.has("name")) {
    String name = jsonObject.getString("name");
}

【讨论】:

  • 很好的建议。首先检查密钥是否存在将防止抛出“org.json.JSONException: No value for yourKey”。
【解决方案2】:

对于其他拥有org.json.JSONException: No value for //your parameter的用户。

在这种情况下,您应该检查名称是否为空。
例如使用方法jsonResponse.optString("name")

活生生的例子:

if (success) {
    String name = jsonResponse.optString("name"); //will get name value or return empty String

    if (!name.equals("")) {

        //Your code if name is exist
        Intent intent = new Intent(login.this, Welcome.class);
        intent.putExtra("name", name);
        intent.putExtra("e_mail", e_mail);
        login.this.startActivity(intent);

    } else {
        //Your code if the name is empty
    }

} else {
    AlertDialog.Builder builder = new AlertDialog.Builder(login.this);
    builder.setMessage("Login Failed")
            .setNegativeButton("Retry", null)
            .create()
            .show();
}

【讨论】:

    【解决方案3】:

    在不知道上下文(或异常的行号)的情况下无法确定,但我的钱会在电话上:

    jsonResponse.getString("name")
    

    很可能,从服务器收到的 JSON 不包含任何名称为 name 的名称/值对。

    【讨论】:

      猜你喜欢
      • 2016-09-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多