Android - 数据没有添加到MySQL数据库 - 没有错误

时间:2016-07-01 20:50:50

标签: java php android json

我一直致力于为我正在处理的应用创建一个登录/注册功能(注意:此应用目前仅供我个人使用,所以我不想制作我的密码截至目前的超级安全)。当我在模拟的Android设备上运行我的应用程序时单击注册按钮时,Android Studio不会抛出任何错误,但在检查PHP MyAdmin时没有显示任何错误。我使用了不同的用户名和密码组合(这里显然是示例),一堆不同的localhost / 127.0.0.1 / 10.0.2.2组合,似乎没有任何工作。在线测试时,10.0.2.2:8080 / android_login_api / register.php表示无法显示页面。 localhost:8080 / android_login_api / register.php至少显示,但有错误说有一个未知的主机(10.0.2.2:8080)。此外,我使用端口8080与WAMP。非常感谢任何帮助,谢谢!

register.php:

<?php
$con = mysqli_connect("10.0.2.2:8080" , "EXAMPLE_USERNAME" , "EXAMPLE_PASSWORD" , "android_api");
$name = $_POST["name"];
$email = $_POST["email"];
$password = $_POST["password"];
$statement = mysqli_prepare($con, "INSERT INTO users (name, email, password) VALUES (?, ?, ?)");
mysqli_stmt_bind_param($statement, "sss", $name, $email, $password);
mysqli_stmt_execute($statement);
$response = array();
$response["success"] = true;

echo json_encode($response);
?>

RegisterRequest.java:

package example.com.musicapptest;

import com.android.volley.Response;
import com.android.volley.toolbox.StringRequest;

import java.util.HashMap;
import java.util.Map;

/**
* Created by Carter Klein on 6/26/2016.
*/
public class RegisterRequest extends StringRequest {
    private static final String REGISTER_REQUEST_URL = "http://10.0.2.2:8080/android_login_api/register.php";
    private Map<String, String> params;

    public RegisterRequest(String name, String username, String password, Response.Listener<String> listener) {
        super(Method.POST, REGISTER_REQUEST_URL, listener, null);
        params = new HashMap<>();
        params.put("name", name);
        params.put("username", username);
        params.put("password", password);
    }

    @Override
    public Map<String, String> getParams() {
        return params;
    }
}

RegisterActivity.java:

package example.com.musicapptest;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
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 name = (EditText) findViewById(R.id.name);
        final EditText email = (EditText) findViewById(R.id.email);
        final EditText password = (EditText) findViewById(R.id.password);

        final Button registerButton = (Button) findViewById(R.id.btnRegister);
        final Button toLogin = (Button) findViewById(R.id.btnLinkToLoginScreen);

        registerButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                final String clickName = name.getText().toString();
                final String clickEmail = email.getText().toString();
                final String clickPassword = password.getText().toString();

                Response.Listener<String> responseListener = new Response.Listener<String>() {

                    @Override
                    public void onResponse(String response) {
                        try {
                            JSONArray jsonResponse = new JSONArray(response);
                            boolean success = jsonResponse.getBoolean(Integer.parseInt("success"));
                            if (success) {
                                Intent intent = new Intent(RegisterActivity.this, LoginActivity.class);
                                RegisterActivity.this.startActivity(intent);
                            } else {
                                AlertDialog.Builder builder = new AlertDialog.Builder(RegisterActivity.this);
                                builder.setMessage("Register Failed")
                                        .setNegativeButton("Retry", null)
                                        .create()
                                        .show();
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                };

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

1 个答案:

答案 0 :(得分:1)

显然您正在尝试连接到无效的mysql主机。在PHP代码的第一行中,第一个参数应该是MySql服务器地址,而不是WebServer地址。