我正在尝试将数据从我的应用程序插入到MYSQL数据库中。我正在使用HttpURLConnection
打开连接并使用BufferedWriter
进行编写。一切正常(没有任何异常捕获)但不知何故没有数据插入表中。 (我正在关注this教程)。
我正在使用AsyncTask
在后台进行网络连接。如下所示,我创建了一个从AsyncTask
扩展的新类。
public class BackgroundTask extends AsyncTask<String, Void, String>
{
Context ctx;
BackgroundTask(Context ctx)
{
this.ctx = ctx;
}
这是doInBackground方法。
@Override
protected String doInBackground(String... strings)
{
String registerUrl = "http://localhost/APPoint/register.php";
String method = strings[0];
if (method.equals("register"))
{
try
{
String gebruikersnaam = strings[1];
String wachtwoord = strings[2];
String voornaam = strings[3];
String tussenvoegsel = strings[4];
String achternaam = strings[5];
String bedrijfsnaam = strings[6];
String email = strings[7];
String telefoonnummer = strings[8];
URL url = new URL(registerUrl);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("POST");
conn.setDoOutput(true);
OutputStream OS = conn.getOutputStream();
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(OS));
String data = URLEncoder.encode("Gebruikersnaam", "UTF-8")+ "=" + URLEncoder.encode(gebruikersnaam, "UTF-8")+"&"+
URLEncoder.encode("wachtwoord", "UTF-8")+ "=" + URLEncoder.encode(wachtwoord, "UTF-8")+"&"+
URLEncoder.encode("voornaam", "UTF-8")+ "=" + URLEncoder.encode(voornaam, "UTF-8")+"&"+
URLEncoder.encode("tussenvoegsel", "UTF-8")+ "=" + URLEncoder.encode(tussenvoegsel, "UTF-8")+"&"+
URLEncoder.encode("achternaam", "UTF-8")+ "=" + URLEncoder.encode(achternaam, "UTF-8")+"&"+
URLEncoder.encode("bedrijfsnaam", "UTF-8")+ "=" + URLEncoder.encode(bedrijfsnaam, "UTF-8")+"&"+
URLEncoder.encode("email", "UTF-8")+ "=" + URLEncoder.encode(email, "UTF-8")+"&"+
URLEncoder.encode("telefoonnummer", "UTF-8")+ "=" + URLEncoder.encode(telefoonnummer, "UTF-8");
try {
bw.write(data);
}catch(Exception e)
{
e.printStackTrace();
}
bw.flush();
InputStream inputS = conn.getInputStream();
inputS.close();
bw.close();
OS.close();
} catch (IOException e) {
e.printStackTrace();
}
return "Registreren voltooid";
}
return null;
}
我的php数据库连接初始化程序。
<?php
define('HOST', 'localhost');
define('USER', 'root');
define('PASS', '');
define('DB', 'appoint');
$conn = new PDO('mysql:dbname=' . DB . ';host=' . HOST, USER, PASS);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
我的register.php文件
<?php
$username = filter_var(trim($_POST['gebruikersnaam']), FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_HIGH);
$password = filter_var(trim($_POST['wachtwoord']), FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_HIGH);
$voornaam = filter_var(trim($_POST['voornaam']), FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_HIGH);
$tussenvoegsel = filter_var(trim($_POST['tussenvoegsel']), FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_HIGH);
$achternaam = filter_var(trim($_POST['achternaam']), FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_HIGH);
$bedrijfsnaam = filter_var(trim($_POST['bedrijfsnaam']), FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_HIGH);
$email = filter_var(trim($_POST['email']), FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_HIGH);
$telefoonnummer = filter_var(trim($_POST['telefoonnummer']), FILTER_SANITIZE_NUMBER_INT, FILTER_FLAG_STRIP_HIGH);
if ($username == '' || $password == '' || $voornaam == '' || $tussenvoegsel == '' || $achternaam == '' || $bedrijfsnaam == '' || $email == '' || $telefoonnummer == '') {
//echo 'Verplichte velden zijn niet ingevoerd!';
} else {
require_once('DBConnect.php');
$stmnt = $conn->prepare("INSERT INTO gebruikers (`gebruikersnaam`, `wachtwoord`, `voornaam`, `tussenvoegsel`, `achternaam`, `bedrijfsnaam`, `email`, `telefoonnummer`) "
. "VALUES (?, ? , ? , ?, ?, ?, ?, ?)");
try {
$stmnt->execute(array($username, $password, $voornaam, $tussenvoegsel, $achternaam, $bedrijfsnaam, $email, $telefoonnummer));
} catch (PDOException $ex) {
if ($ex->errorInfo[1] == 1062) {
echo 'duplicate entry';
} else {
var_dump($ex);
}
}
}
起初我以为我的网址错了。我尝试过使用:
"http://127.0.0.1/APPoint/register.php"
"http://localhost/APPoint/register.php"
"http://10.0.2.2/APPoint/register.php"
"http://192.168.0.1/APPoint/register.php"
似乎什么都没做。如果有人可以得到一些意见,将不胜感激。
编辑我应该提到我没有使用AVD来测试代码。我正在使用我自己的三星设备。
修改为backgroundtask添加执行
if(!errors)
{
String method = "register";
BackgroundTask bgTask = new BackgroundTask(this);
bgTask.execute(method, gebruikersnaam, wachtwoord, voornaam, tussenvoegsel, achternaam, bedrijfsnaam, email, telefoonnummer);
finish();
}
编辑在logcat上运行AVD时出现此错误(可能是android错误): getSlotFromBufferLocked:未知缓冲区0xb4095170 谷歌搜索后,它看起来像一个关于权限的一些Android错误。但我已经在我的清单中获得了互联网许可。
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
答案 0 :(得分:0)
当您调用conn.getInputStream()或conn.getResponseCode()时,会将请求发送到服务器。如果你不这样做,没有任何反应。请将这些方法之一添加到doInBackground()。
答案 1 :(得分:0)
您是否为AsyncTask调用execute()方法?
如何将您的服务器托管到应用程序?您无法使用手机访问本地主机。
有一个名为ngrok的智能工具可以将您的本地主机打开到互联网上。在Android开发中使用localhost作为Android应用程序的服务器非常容易。 访问https://ngrok.com