【发布时间】:2016-04-10 12:22:21
【问题描述】:
我正在尝试将数据从 android 插入到在线数据库,我的应用程序没有给出任何错误以便我可以修复它,当我将我的应用程序连接到 localhost 并且它成功插入数据但当我尝试时它工作正常将数据插入在线数据库它不起作用。有人可以告诉我做错了什么吗? 在您查看我的代码之前,我希望您查看我的托管信息,以防你们认为这样做有问题。
Main Domain securekid.base.pk
FTP hostname: ftp.base.pk
FTP username: basep_****
MySQL hostname: sql104.base.pk
MySQL username: basep_****
Hosting Volume vol14_2
我的数据库连接文件:
<?php
$host = "sql104.base.pk";
$user = "basep_****";
$password = "******";
$db = "basep_*****_android";
$con = mysqli_connect($host, $user, $password, $db);
if($con)
{
echo "successful";
}
else
{
die("error" . mysqli_connect_error());
}
?>
注册.php
<?php
require "db_connect.php";
$username =$_POST["username"];
$password =$_POST["password"];
$phone_no =$_POST["phone_no"];
$mysql_qry = "insert into user(username,password,phone_no) values('$username', '$password', '$phone_no')";
if($con->query($mysql_qry) === TRUE)
{
echo "Registration Successful";
}
else{
echo "failed";
}
?>
这是 background.java 文件
class backgroundTask extends AsyncTask<String,Void,String> {
AlertDialog alertDialog;
Context ctx;
// Globals session_id = Globals.getInstance();
// Globals session_id_child = Globals.getInstance();
backgroundTask(Context ctx) {
this.ctx = ctx;
}
String reg_url;
@Override
protected void onPreExecute() {
reg_url = "http://securekid.base.pk/app/register.php";
}
@Override
protected String doInBackground(String... args) {
String method = args[0];
if (method.equals("register")) {
String username = args[1];
String password = args[2];
String phone_no = args[3];
try {
URL url = new URL(reg_url);
HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
httpURLConnection.setRequestMethod("POST");
httpURLConnection.setDoOutput(true);
httpURLConnection.setDoInput(true);
OutputStream OS = httpURLConnection.getOutputStream();
BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(OS, "UTF-8"));
String data = URLEncoder.encode("username", "UTF-8") + "=" + URLEncoder.encode(username, "UTF-8") + "&" +
URLEncoder.encode("password", "UTF-8") + "=" + URLEncoder.encode(password, "UTF-8") + "&" +
URLEncoder.encode("phone_no", "UTF-8") + "=" + URLEncoder.encode(phone_no, "UTF-8");
bufferedWriter.write(data);
bufferedWriter.flush();
bufferedWriter.close();
OS.close();
InputStream IS = httpURLConnection.getInputStream();
IS.close();
//httpURLConnection.connect();
httpURLConnection.disconnect();
return "Registration Successful";
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
【问题讨论】:
标签: java php android mysql database