dptsivmg82908 2016-08-18 14:12
浏览 93
已采纳

如何解决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条回答 默认 最新

  • dongtan2603 2016-08-18 14:26
    关注

    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");
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
编辑
预览

报告相同问题?

悬赏问题

  • ¥15 MATLAB解决问题
  • ¥35 哪位专业人士知道这是什么原件吗?哪里可以买到?
  • ¥15 关于#c##的问题:treenode反序列化后获取不到上一节点和下一节点,Fullpath和Handle报错
  • ¥15 一部手机能否同时用不同的app进入不同的直播间?
  • ¥15 没输出运行不了什么问题
  • ¥20 输入import torch显示Intel MKL FATAL ERROR,系统驱动1%,: Cannot load mkl_intel_thread.dll.
  • ¥15 点云密度大则包围盒小
  • ¥15 nginx使用nfs进行服务器的数据共享
  • ¥15 C#i编程中so-ir-192编码的字符集转码UTF8问题
  • ¥15 51嵌入式入门按键小项目