【发布时间】:2015-12-31 05:56:35
【问题描述】:
我是一个新手,正在为 Android 创建一个基本的登录应用程序,使用 000webhost 作为我的服务器。
我的 Java 代码:
ArrayList<NameValuePair> dataToSend = new ArrayList<>();
dataToSend.add(new BasicNameValuePair("name", user.name));
dataToSend.add(new BasicNameValuePair("email", user.email));
dataToSend.add(new BasicNameValuePair("password", user.password));
dataToSend.add(new BasicNameValuePair("leagueID", user.leagueID + ""));
HttpParams httpRequestParams = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(httpRequestParams, CONNECTION_TIMEOUT);
HttpConnectionParams.setSoTimeout(httpRequestParams, CONNECTION_TIMEOUT);
HttpClient client = new DefaultHttpClient(httpRequestParams);
HttpPost post = new HttpPost("http://subdomain.site88.net/register.php");
try{
post.setEntity(new UrlEncodedFormEntity(dataToSend));
client.execute(post);
}catch(Exception e){
e.printStackTrace();
}
return null;
我的 PHP 代码
<?php
$con = mysqli_connect("mysql1.000webhost.com", "username", "password", "dbname");
/***I want to get this data from Java side of application***/
$name = $_POST["name"];
$email = $_POST["email"];
$password = $_POST["password"];
$leagueID = $_POST["LeagueID"];
/***this works
$name = "John Doe";
$email = "JohnDoe@gmail.com";
$password = "password"
$leagueID = 0;
***/
echo "Hello";//place this here to check website to see if its showing up
//Now we will add the name, email, password, and leagueID into a table called "user".
$statement = mysqli_prepare($con, "INSERT INTO user (email, name, password, leagueID) VALUES (?, ?, ?, ?)");
mysqli_stmt_bind_param($statement, "sssi", $email, $name, $password, $leagueID);
mysqli_stmt_execute($statement);
mysqli_stmt_close($statement);
//finish up by closing the connection
mysqli_close($con);
?>
如果我将值硬连线到 PHP 代码中而不是使用 $_POST 方法,它会按请求发送到数据库。但是,似乎 $_POST 变量为空。我不太确定为什么会这样。是不是 000webhost 有某种设置不允许某人发布数据?
另外,我知道我正在使用已弃用的 java 方法以及我的密码存储目前是多么不安全。我将来会修改它,但我首先想知道如何发布数据。
提前致谢!
【问题讨论】: