org.json.JSONException: 没有名称值

Eva*_*der 4 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);
            }
        });
Run Code Online (Sandbox Code Playgroud)

ccp*_*zza 8

首先检查您是否拥有密钥:

if (jsonObject.has("name")) {
    String name = jsonObject.getString("name");
}
Run Code Online (Sandbox Code Playgroud)


Dan*_*n R 0

在不知道上下文(或异常的行号)的情况下不能肯定地说,但我的钱将随时待命:

jsonResponse.getString("name")
Run Code Online (Sandbox Code Playgroud)

最有可能的是,从服务器接收到的 JSON 不包含任何带有 name 的名称/值对name