gpt4 book ai didi

php - 连接我的 mysql 数据库和 Android 应用程序

转载 作者:行者123 更新时间:2023-11-29 21:12:25 24 4
gpt4 key购买 nike

我正在遵循 youtube 中 tonakami TV 制作 Android 登录注册的教程。我遇到了 Android 应用程序无法通过 LOCALHOST XAMPP 更新或连接到数据库的问题。

该应用程序运行良好,直到我将注册表信息发送到数据库。一旦我尝试更新我的数据库(注册后),里面就没有新数据...

ServerRequests.java

public class ServerRequests {

ProgressDialog progressDialog;
public static final int CONNECTION_TIMEOUT = 100 * 15;
public static final String SERVER_ADDRESS = "http://10.0.2.2:8080/";

public ServerRequests(Context context) {
progressDialog = new ProgressDialog(context);
progressDialog.setCancelable(false);
progressDialog.setTitle("Processing");
progressDialog.setMessage("Please wait...");
}

public void storeUserDataInBackground(User user, GetUserCallback userCallback) {
progressDialog.show();
new StoreUserDataAsyncTask(user, userCallback).execute();
}

public void fetchUserDataInBackground(User user, GetUserCallback callBack) {
progressDialog.show();
new fetchUserDataAsyncTask(user, callBack).execute();
}

public class StoreUserDataAsyncTask extends AsyncTask<Void, Void, Void> {

User user;
GetUserCallback userCallback;

public StoreUserDataAsyncTask(User user, GetUserCallback callBack) {
this.user = user;
this.userCallback = callBack;
}

@Override
protected Void doInBackground(Void... params) {
ArrayList<NameValuePair> dataToSend = new ArrayList<>();
dataToSend.add(new BasicNameValuePair("name", user.name));
dataToSend.add(new BasicNameValuePair("username", user.username));
dataToSend.add(new BasicNameValuePair("email", user.email));
dataToSend.add(new BasicNameValuePair("password", user.password));

HttpParams httpRequestParams = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(httpRequestParams, CONNECTION_TIMEOUT);
HttpConnectionParams.setSoTimeout(httpRequestParams, CONNECTION_TIMEOUT);

HttpClient client = new DefaultHttpClient(httpRequestParams);
HttpPost post = new HttpPost(SERVER_ADDRESS + "Register.php");

try{
post.setEntity(new UrlEncodedFormEntity(dataToSend));
client.execute(post);
} catch (Exception e) {
e.printStackTrace();
}

return null;
}

@Override
protected void onPostExecute(Void aVoid) {
progressDialog.dismiss();
userCallback.done(null);
super.onPostExecute(aVoid);
}
}

public class fetchUserDataAsyncTask extends AsyncTask<Void, Void, User> {
User user;
GetUserCallback userCallback;

public fetchUserDataAsyncTask(User user, GetUserCallback userCallback) {
this.user = user;
this.userCallback = userCallback;
}

@Override
protected User doInBackground(Void... params) {
ArrayList<NameValuePair> dataToSend = new ArrayList<>();
dataToSend.add(new BasicNameValuePair("username", user.username));
dataToSend.add(new BasicNameValuePair("password", user.password));

HttpParams httpRequestParams = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(httpRequestParams, CONNECTION_TIMEOUT);
HttpConnectionParams.setSoTimeout(httpRequestParams, CONNECTION_TIMEOUT);

HttpClient client = new DefaultHttpClient(httpRequestParams);
HttpPost post = new HttpPost(SERVER_ADDRESS + "FetchUserData.php");

User returnedUser = null;

try{
post.setEntity(new UrlEncodedFormEntity(dataToSend));
HttpResponse httpResponse = client.execute(post);

HttpEntity entity = httpResponse.getEntity();
String result = EntityUtils.toString(entity);
JSONObject jObject = new JSONObject(result);

if(jObject.length() == 0){
returnedUser = null;
} else {
String name = jObject.getString("name");
String email = jObject.getString("email");

returnedUser = new User(name, user.username, email, user.password);

}
} catch (Exception e) {
e.printStackTrace();
}

return returnedUser;
}

@Override
protected void onPostExecute(User returnedUser) {
progressDialog.dismiss();
userCallback.done(returnedUser);
super.onPostExecute(returnedUser);
}
}
}

Register.php

<?php
$con=mysqli_connect("10.0.2.2:8080","root","","test");

$name = $_POST["name"];
$username = $_POST["username"];
$email = $_POST["email"];
$password = $_POST["password"];

$statement = mysqli_prepare($con, "INSERT INTO user (name, username, email, password) VALUES (?, ?, ?, ?) ");
mysqli_stmt_bind_parm($statement, "siss", $id, $name, $username, $email, $password);
mysqli_stmt_execute($statement);

mysqli_stmt_close($statement);
mysqli_close($con);
?>

数据库:

Database

我正在使用 android studio 模拟器。如果您需要任何进一步的信息或代码,请告诉我。感谢您的帮助。

最佳答案

记下您绑定(bind)到用户名的数据类型,我不确定您是否将用户名用作整数。

见下文,我已将其更改为使用“s”绑定(bind)字符串。

如何绑定(bind)不同的数据类型请引用PHP手册。 here

   $mysqli = new mysqli("10.0.2.2", "root", "", "test");
/**
* Don't use any port as mysql uses port 3306
* thats why i have removed 8080, this is used to access phpmyadmin but not mysql that why we are including it from the frontend
* but not when accessing it from terminal or as in our case on php
*/
/* check connection */
if ($mysqli->connect_errno) {
echo json_encode("Connect failed: %s\n", $mysqli->connect_error);
exit();
}

/* check if server is alive */
if ($mysqli->ping()) {
//now lets pass the post to the function register with the POST params
if (!empty($_POST)) {
//since we have intialised the class from above
$query = "INSERT INTO user (name, username, email, password) VALUES (?, ?, ?, ?) ";
$stmt = $mysqli->prepare($query);
$stmt->bind_param('s', $_POST['name']);
$stmt->bind_param('s', $_POST['username']);
$stmt->bind_param('s', $_POST['email']);
$stmt->bind_param('s', $_POST['password']);
$result = $stmt->execute();
if ($result) {
echo json_encode('user created successful');
} else {
echo json_encode($mysqli->error);
}
} else {
echo json_encode('POST is empty');
}
} else {
echo json_encode("Error: %s\n", $mysqli->error);
}

关于php - 连接我的 mysql 数据库和 Android 应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36251933/

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