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

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.

1个回答



您的响应是 I / System.out:json response:{“success”:true} </ code>。\ n如果您读取日志,问题就在于您尝试获取布尔值。</ p>

只需更改此行</ p>

  boolean success =  jsonResponse.getBoolean(“第二步,识别我的JSON”); 
</ code> </ pre>

</ p>

  boolean success = jsonResponse  .getBoolean(“success”); 
</ code> </ pre>
</ div>

展开原文

原文

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

dongyanfeng0563
dongyanfeng0563 谢谢,它现在进入下一个活动。 虽然...我注册的用户名没有保存到我的数据库中。 知道为什么不呢?
大约 4 年之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问