如何解决JSONException:我的Android项目没有价值?

时间:2016-08-18 22:12:03

标签: php android mysql json

有人知道如何解决我的问题或者哪里开始寻找?我真的不知道。

这是一个小小的Android教程,我稍微修改了一下,使用了MySql和Php。当我点击我的注册'时,根本没有任何事情发生。我的应用程序上的按钮 - 它应该继续进行新的活动。

这是我的Android代码:

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

这是我的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

这是我的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);

?>

任何帮助都将不胜感激。

1 个答案:

答案 0 :(得分:0)

您的回复是I/System.out: json response: {"success":true}。 如果您阅读日志,问题就是当您尝试获取布尔值时。

只需更改此行

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

代表

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