gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 01:13:55 25 4
gpt4 key购买 nike

我一直在为我正在开发的应用程序创建登录/注册功能(注意:这个应用程序目前仅供我个人使用,所以我不打算让我的密码 super 安全现在)。当我在模拟的 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)。另外,我在 WAMP 中使用端口 8080。任何帮助将不胜感激,谢谢!

注册.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);
?>

注册请求.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);
}
});
}
}

最佳答案

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

关于java - Android - 数据未添加到 MySQL 数据库 - 没有错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38153709/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com