Christophe Harris Christophe Harris - 3 months ago 55
JSON Question

How can I solve JSONException: No value in my Android project?

Does anybody know how to solve my problem or where to even start looking? I really have no idea.

It's a little Android tutorial I've modified slightly, making use of MySql and Php. Nothing happens at all when I click my 'Register' button on my app - it should move on to a new activity.

Here's my Android code:

package com.example.chris.loginregister;

import android.content.Intent;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.toolbox.Volley;

import org.json.JSONException;
import org.json.JSONObject;

public class RegisterActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);

final EditText etUsername = (EditText) findViewById(R.id.etUsername);
final Button bRegister = (Button) findViewById(R.id.bRegister);

bRegister.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View v) {
final String username = etUsername.getText().toString();
System.out.println("first step of the test, at least my button is recognised");
Response.Listener<String> responseListener = new Response.Listener<String>(){

@Override
public void onResponse(String response) {

try {
JSONObject jsonResponse = new JSONObject(response);
boolean success = jsonResponse.getBoolean("second step, recognises my JSON");

if (success) {
System.out.println("yes, succeeded, it works, it recognises my PHP file");
Intent intent = new Intent(RegisterActivity.this, LoginActivity.class);
RegisterActivity.this.startActivity(intent);

} else {
System.out.println("no, failed, but at least it recognises my JSON");
AlertDialog.Builder builder = new AlertDialog.Builder(RegisterActivity.this);
builder.setMessage("Register failed")
.setNegativeButton("Retry", null)
.create()
.show();
}


} catch (JSONException e) {

e.printStackTrace();
System.out.println("no, failed utterly");
}

System.out.println("json response: " + response);
}
};

RegisterRequest registerRequest = new RegisterRequest(username, responseListener);
RequestQueue queue = Volley.newRequestQueue(RegisterActivity.this);
queue.add(registerRequest);
}
});


Here's my logcat :

08-18 22:47:46.486 4284-4284/com.example.chris.loginregister I/System.out: first step of the test, at least my button is recognised
08-18 22:47:46.596 4284-4284/com.example.chris.loginregister W/System.err: org.json.JSONException: No value for second step, recognises my JSON
08-18 22:47:46.606 4284-4284/com.example.chris.loginregister W/System.err: at org.json.JSONObject.get(JSONObject.java:354)
08-18 22:47:46.606 4284-4284/com.example.chris.loginregister W/System.err: at org.json.JSONObject.getBoolean(JSONObject.java:375)
08-18 22:47:46.606 4284-4284/com.example.chris.loginregister W/System.err: at com.example.chris.loginregister.RegisterActivity$1$1.onResponse(RegisterActivity.java:43)
08-18 22:47:46.606 4284-4284/com.example.chris.loginregister W/System.err: at com.example.chris.loginregister.RegisterActivity$1$1.onResponse(RegisterActivity.java:36)
08-18 22:47:46.606 4284-4284/com.example.chris.loginregister W/System.err: at com.android.volley.toolbox.StringRequest.deliverResponse(StringRequest.java:60)
08-18 22:47:46.606 4284-4284/com.example.chris.loginregister W/System.err: at com.android.volley.toolbox.StringRequest.deliverResponse(StringRequest.java:30)
08-18 22:47:46.606 4284-4284/com.example.chris.loginregister W/System.err: at com.android.volley.ExecutorDelivery$ResponseDeliveryRunnable.run(ExecutorDelivery.java:99)
08-18 22:47:46.606 4284-4284/com.example.chris.loginregister W/System.err: at android.os.Handler.handleCallback(Handler.java:730)
08-18 22:47:46.606 4284-4284/com.example.chris.loginregister W/System.err: at android.os.Handler.dispatchMessage(Handler.java:92)
08-18 22:47:46.616 4284-4284/com.example.chris.loginregister W/System.err: at android.os.Looper.loop(Looper.java:137)
08-18 22:47:46.616 4284-4284/com.example.chris.loginregister W/System.err: at android.app.ActivityThread.main(ActivityThread.java:5457)
08-18 22:47:46.616 4284-4284/com.example.chris.loginregister W/System.err: at java.lang.reflect.Method.invokeNative(Native Method)
08-18 22:47:46.616 4284-4284/com.example.chris.loginregister W/System.err: at java.lang.reflect.Method.invoke(Method.java:525)
08-18 22:47:46.616 4284-4284/com.example.chris.loginregister W/System.err: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:806)
08-18 22:47:46.616 4284-4284/com.example.chris.loginregister W/System.err: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:622)
08-18 22:47:46.616 4284-4284/com.example.chris.loginregister W/System.err: at dalvik.system.NativeStart.main(Native Method)
08-18 22:47:46.626 4284-4284/com.example.chris.loginregister I/System.out: no, failed utterly
08-18 22:47:46.626 4284-4284/com.example.chris.loginregister I/System.out: json response: {"success":true}
08-18 22:48:16.866 4284-4284/com.example.chris.loginregister W/IInputConnectionWrapper: beginBatchEdit on inactive InputConnection
08-18 22:48:16.866 4284-4284/com.example.chris.loginregister W/IInputConnectionWrapper: getSelectedText on inactive InputConnection
08-18 22:48:16.876 4284-4284/com.example.chris.loginregister W/IInputConnectionWrapper: endBatchEdit on inactive InputConnection
08-18 22:48:16.876 4284-4284/com.example.chris.loginregister W/IInputConnectionWrapper: getTextBeforeCursor on inactive InputConnection
08-18 22:48:16.876 4284-4284/com.example.chris.loginregister W/IInputConnectionWrapper: getTextAfterCursor on inactive InputConnection


And here's my Register.php:

<?php

$con = mysqli_connect("12.345.67.89", "root", "password", "database_name");

// $name = $_POST["name"];

// $age = $_POST["age"];

$username = $_POST["username"];

// $password = $_POST["password"];

$statement = mysqli_prepare($con, "INSERT INTO user (username) VALUES (?)");

// $statement = mysqli_prepare($con, "INSERT INTO user (name, username,

// age, password) VALUES (?, ?, ?, ?)");

mysqli_stmt_bind_param($statement, "s", $username);

mysqli_stmt_execute($statement);

$response = array();

$response["success"] = true;

echo json_encode($response);

?>


Any help at all would be appreciated.

Answer

Your reponse is I/System.out: json response: {"success":true}. And if you read the log the problem is when you try to get the boolean value.

Just change this line

boolean success = jsonResponse.getBoolean("second step, recognises my JSON");

for

boolean success=jsonResponse.getBoolean("success");