org.json.JSONException:名称没有值

时间:2016-06-30 03:56:53

标签: 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);
            }
        });

2 个答案:

答案 0 :(得分:4)

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

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

答案 1 :(得分:0)

在不知道上下文(或例外的行号)的情况下无法肯定地说,但我的钱将在通话中:

jsonResponse.getString("name")

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