我正在尝试将数据从Android应用程序发送到托管在localhost服务器上的PHP脚本。 PHP文件位于我的XAMPP文件夹的htdocs中,我总是把它放在那里。这是我发送数据的方法:
@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));
dataToSend.add(new BasicNameValuePair("phoneNumber", user.phoneNumber));
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");
HttpPost post = new HttpPost("http://127.0.0.1/register.php");
try{
post.setEntity(new UrlEncodedFormEntity(dataToSend));
client.execute(post);
}catch(Exception e){
e.printStackTrace();
}
return null;
}
当我提交数据时,收到以下错误:
org.apache.http.conn.HttpHostConnectException: Connection to http://127.0.0.1 refused
我已经尝试将ip地址更改为localhost,但这没有任何改变。
这是我的PHP脚本
<?php
$con = mysqli_connect("localhost", "root", "", "is_clients");
$name = $_POST["name"];
$age = $_POST["age"];
$username = $_POST["username"];
$password = $_POST["password"];
$phoneNumber = $_POST["phoneNumber"];
$statement = mysqli_prepare($con, "INSERT INTO user (name, age, username, password, phoneNumber) VALUES (?, ?, ?, ?, ?) ");
mysqli_stmt_bind_param($statement, "sisss", $name, $age, $username, $password, $phoneNumber);
mysqli_stmt_execute($statement);
mysqli_stmt_close($statement);
mysqli_close($con);
&GT;
其目的是在没有密码的情况下将数据发送到MySQL数据库。
如何访问PHP脚本?
提前致谢。