伙计们。我是一个非常新的Android工作室初学者。我遵循Tonkami教程登录并注册Android Studio。但是,它不起作用。当我注册一个新帐户时,我的数据库不会更新,因此我无法使用该用户名和密码登录。有人说这是因为android studio没有连接到web服务器。我认为这可能是问题,因为即使我在我的AndroidManifest.xml文件中添加权限到互联网,当我按照其他教程,我无法使用我的应用程序打开一个URL,但我可以打开我的模拟器上的网站。我不知道为什么...无法连接我的android studio与MySQL数据库,在线服务器
的logcat:
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是我的Web服务器的域
这里是我的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);
}
}
}
对不起,这样的日志代码。 如果需要更多信息,请让我知道。非常感谢您的帮助!