Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

org.json.JSONException: No value for name

What could be the reason of this error in the code below?

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);
            }
        });
like image 691
Evan Alexander Avatar asked Jun 30 '16 03:06

Evan Alexander


2 Answers

Check if you have the key first:

if (jsonObject.has("name")) {
    String name = jsonObject.getString("name");
}
like image 137
ccpizza Avatar answered Sep 27 '22 20:09

ccpizza


For others users which have the org.json.JSONException: No value for //your parameter.

In this case you should check if the name is empty.
For example using method jsonResponse.optString("name").

Live example:

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();
}
like image 40
V.March Avatar answered Sep 27 '22 20:09

V.March