gpt4 book ai didi

java - 无法将我的 android studio 与在线服务器 MySQL 数据库连接

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

伙计们。我是 android studio 的新手。我按照 Tonikami 教程登录并注册 Android Studio。然而,这不起作用。当我注册新帐户时,我的数据库不会更新,因此我无法使用该用户名和密码登录。有人说这是因为android studio没有连接到web服务器。我认为这可能是问题所在,因为即使我在 AndroidManifest.xml 文件中添加了互联网权限,当我按照其他教程操作时,我无法使用我的应用程序打开网址,但我可以在模拟器上打开该网站。我不知道为什么...

日志猫:

03-27 18:56:54.242 2501-2516/com.tonikamitv.pilldispenser W/System.err: java.net.UnknownHostException: Unable to resolve host "shiyulian.comxa.comRegister.php": No address associated with hostname
03-27 18:56:54.242 2501-2516/com.tonikamitv.pilldispenser W/System.err: at java.net.InetAddress.lookupHostByName(InetAddress.java:470)
03-27 18:56:54.242 2501-2516/com.tonikamitv.pilldispenser W/System.err: at java.net.InetAddress.getAllByNameImpl(InetAddress.java:252)
03-27 18:56:54.242 2501-2516/com.tonikamitv.pilldispenser W/System.err: at java.net.InetAddress.getAllByName(InetAddress.java:215)
03-27 18:56:54.242 2501-2516/com.tonikamitv.pilldispenser W/System.err: at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:142)
03-27 18:56:54.242 2501-2516/com.tonikamitv.pilldispenser W/System.err: at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:169)
03-27 18:56:54.242 2501-2516/com.tonikamitv.pilldispenser W/System.err: at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:124)
03-27 18:56:54.242 2501-2516/com.tonikamitv.pilldispenser W/System.err: at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:366)
03-27 18:56:54.242 2501-2516/com.tonikamitv.pilldispenser W/System.err: at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:560)
03-27 18:56:54.242 2501-2516/com.tonikamitv.pilldispenser W/System.err: at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:492)
03-27 18:56:54.243 2501-2516/com.tonikamitv.pilldispenser W/System.err: at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:470)
03-27 18:56:54.243 2501-2516/com.tonikamitv.pilldispenser W/System.err: at com.tonikamitv.pilldispenser.ServerRequests$StoreUserDataAsyncTask.doInBackground(ServerRequests.java:93)
03-27 18:56:54.243 2501-2516/com.tonikamitv.pilldispenser W/System.err: at com.tonikamitv.pilldispenser.ServerRequests$StoreUserDataAsyncTask.doInBackground(ServerRequests.java:67)
03-27 18:56:54.243 2501-2516/com.tonikamitv.pilldispenser W/System.err: at android.os.AsyncTask$2.call(AsyncTask.java:295)
03-27 18:56:54.243 2501-2516/com.tonikamitv.pilldispenser W/System.err: at java.util.concurrent.FutureTask.run(FutureTask.java:237)
03-27 18:56:54.243 2501-2516/com.tonikamitv.pilldispenser W/System.err: at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:234)
03-27 18:56:54.243 2501-2516/com.tonikamitv.pilldispenser W/System.err: at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
03-27 18:56:54.243 2501-2516/com.tonikamitv.pilldispenser W/System.err: at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
03-27 18:56:54.243 2501-2516/com.tonikamitv.pilldispenser W/System.err: at java.lang.Thread.run(Thread.java:818)
03-27 18:56:54.244 2501-2516/com.tonikamitv.pilldispenser W/System.err: Caused by: android.system.GaiException: android_getaddrinfo failed: EAI_NODATA (No address associated with hostname)
03-27 18:56:54.244 2501-2516/com.tonikamitv.pilldispenser W/System.err: at libcore.io.Posix.android_getaddrinfo(Native Method)
03-27 18:56:54.244 2501-2516/com.tonikamitv.pilldispenser W/System.err: at libcore.io.ForwardingOs.android_getaddrinfo(ForwardingOs.java:55)
03-27 18:56:54.244 2501-2516/com.tonikamitv.pilldispenser W/System.err: at java.net.InetAddress.lookupHostByName(InetAddress.java:451)
03-27 18:56:54.244 2501-2516/com.tonikamitv.pilldispenser W/System.err: ... 17 more

shiyulian.comxa.com是我的网络服务器域名

这是我的 Register.PHP 文件:

<?php
$con=mysqli_connect("localhost","my_user","my_password","my_db");

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

$statement = mysqli_prepare($con, "INSERT INTO User (name, age, username, password) VALUES (?, ?, ?, ?)");
mysqli_stmt_bind_param($statement, "siss", $name, $age, $username, $password);
mysqli_stmt_execute($statement);

mysqli_stmt_close($statement);

mysqli_close($con);
?>

当我在服务器上通过文件管理器打开此文件时,顺便说一句,我使用的是000webhost,与教程中相同,它显示如下警告消息:

Warning: mysqli_connect() [function.mysqli-connect]: (28000/1045): Access denied for user 'my_user'@'localhost' (using password: YES) in /home/a6969519/public_html/Register.php on line 2
Warning: mysqli_prepare() expects parameter 1 to be mysqli, boolean given in /home/a6969519/public_html/Register.php on line 9
Warning: mysqli_stmt_bind_param() expects parameter 1 to be mysqli_stmt, null given in /home/a6969519/public_html/Register.php on line 10
Warning: mysqli_stmt_execute() expects parameter 1 to be mysqli_stmt, null given in /home/a6969519/public_html/Register.php on line 11
Warning: mysqli_stmt_close() expects parameter 1 to be mysqli_stmt, null given in /home/a6969519/public_html/Register.php on line 13

Warning: mysqli_close() expects parameter 1 to be mysqli, boolean given in /home/a6969519/public_html/Register.php on line 15

这是 FetchUserData.php

<?php
$con=mysqli_connect("localhost","my_user","my_password","my_db");


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

$statement = mysqli_prepare($con, "SELECT * FROM User WHERE username = ? AND password = ?");
mysqli_stmt_bind_param($statement, "ss", $username, $password);
mysqli_stmt_execute($statement);

mysqli_stmt_store_result($statement);
mysqli_stmt_bind_result($statement, $userID, $name, $age, $username, $password);

$user = array();

while(mysqli_stmt_fetch($statement)){
$user[name] = $name;
$user[age] = $age;
$user[username] = $username;
$user[password] = $password;
}

echo json_encode($user);

mysqli_stmt_close($statement);


mysqli_close($con);
?>

这是警告:

Warning: mysqli_connect() [function.mysqli-connect]: (28000/1045): Access denied for user 'my_user'@'localhost' (using password: YES) in /home/a6969519/public_html/FetchUserData.php on line 2
Warning: mysqli_prepare() expects parameter 1 to be mysqli, boolean given in /home/a6969519/public_html/FetchUserData.php on line 8
Warning: mysqli_stmt_bind_param() expects parameter 1 to be mysqli_stmt, null given in /home/a6969519/public_html/FetchUserData.php on line 9
Warning: mysqli_stmt_execute() expects parameter 1 to be mysqli_stmt, null given in /home/a6969519/public_html/FetchUserData.php on line 10
Warning: mysqli_stmt_store_result() expects parameter 1 to be mysqli_stmt, null given in /home/a6969519/public_html/FetchUserData.php on line 12

Warning: mysqli_stmt_bind_result() expects parameter 1 to be mysqli_stmt, null given in /home/a6969519/public_html/FetchUserData.php on line 13
Warning: mysqli_stmt_fetch() expects parameter 1 to be mysqli_stmt, null given in /home/a6969519/public_html/FetchUserData.php on line 17

Warning: mysqli_stmt_close() expects parameter 1 to be mysqli_stmt, null given in /home/a6969519/public_html/FetchUserData.php on line 26
Warning: mysqli_close() expects parameter 1 to be mysqli, boolean given in /home/a6969519/public_html/FetchUserData.php on line 29

这是我的 ServerRequests java 类的代码:

public class ServerRequests {

ProgressDialog progressDialog;
public static final int CONNECTION_TIMEOUT = 1000 * 15;
public static final String SERVER_ADDRESS = "http://shiyulian.comxa.com";

public ServerRequests(Context context){
progressDialog = new ProgressDialog(context);
progressDialog.setCancelable(false);
progressDialog.setTitle("Process");
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 userCallback){
this.user = user;
this.userCallback = userCallback;
}
@Override
protected Void doInBackground(Void... params) {

ArrayList<NameValuePair> dataToSend = new ArrayList<>();
dataToSend.add(new BasicNameValuePair("name", user.name));
dataToSend.add(new BasicNameValuePair("age", user.age+""));
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 + "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");
int age = jObject.getInt("age");

returnedUser = new User(name,age, user.username, user.password );
}
}catch (Exception e){
e.printStackTrace();
}

return returnedUser;
}

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

抱歉有这样的日志代码。如果需要更多信息,请告诉我。非常感谢您帮助我!

最佳答案

另一方面,我建议您通过 ping 服务器来连接到 MySQL,而不是直接通过 Android 连接。原因如下

  1. 通过 Android Ping MySQL 是一项消耗资源和计算密集型任务

  2. 安全漏洞问题。

相反,创建一个服务器,如 Node JS、PHP Yii、Rails、Play 等,并在服务器上 ping 通,该服务器实际上会通过执行查询与数据库进行交互,并在 Android 上为您带来结果。

但是,如果您仍然想继续 MySQL 与 Android 的交互,Android 网站上有一些有关 Android 开发人员文档的相关教程。

关于java - 无法将我的 android studio 与在线服务器 MySQL 数据库连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36251532/

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