json - org.json.JSONException : No value for name

标签 json android-studio

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

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);
            }
        });

最佳答案

首先检查您是否拥有 key :

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

关于json - org.json.JSONException : No value for name,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38113727/

相关文章:

java - onclick 按钮出现错误(cardview、recyclerview)

android - 如何使用 Flutter 从 Firebase 获取当前活跃用户?

java - 如何将数组的值传递给另一个类?

json - 将JSON Tweet导入到Hive中的问题

jquery - 检查是否允许使用jQuery嵌入YouTube视频

python - 如何在 Python 中打印和计算 MongoDB 中 Json 内的值

android - 如何将多张图片上传到 Firebase

android - 在启用混淆器的情况下以 Release模式编译 Android 应用程序时出现问题

java - 无法解析 google place JSON 响应 - org.json.JSONException

javascript - Unicode 编码字符串