将android应用程序连接到mysql数据库

时间:2016-06-29 15:37:33

标签: php android mysql

我正在尝试使用wampp服务器和php脚本将我的android应用程序连接到mysql数据库。在编写php脚本和所需的java类之后,为了使其工作,我仍然无法将数据插入数据库。这是我的代码:

public void submitAccount(View view) throws IOException {

    EditText info1 = (EditText) (findViewById(R.id.username_edit_text));
    String message1 = info1.getText().toString();
    EditText info2 = (EditText) (findViewById(R.id.password_edit_text));
    String message2 = info2.getText().toString();
    EditText info3 = (EditText) (findViewById(R.id.retype_password_edit_text));
    String message3 = info3.getText().toString();
    EditText info4 = (EditText) (findViewById(R.id.email_edit_text));
    String message4 = info4.getText().toString();
    EditText info5 = (EditText) (findViewById(R.id.passphrase_edit_text));
    String message5 = info5.getText().toString();
    String operation = "register";
    AddDataToDatabase task = new AddDataToDatabase(this);
    task.execute(operation, message1, message2, message3, message4, message4);
    finish();
}

这是我的CreateAccountActivity

herepublic class AddDataToDatabase extends AsyncTask<String,Void,String> {
Context myContext;
AddDataToDatabase(Context ctx){
    this.myContext=ctx;
}
@Override
protected void onPreExecute(){

}
@Override
protected String doInBackground(String... params){
    String registration_url="http://192.168.0.19/webapp/register.php";
    String message=params[0];
    if(message.equals("register")){
        String username=params[1];
        String password=params[2];
        String retypedPassword=params[3];
        String email=params[4];
        String passphrase=params[5];
        try {
            URL url=new URL(registration_url);
            HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
            urlConnection.setDoOutput(true);
            OutputStream OS= urlConnection.getOutputStream();
            try {
                BufferedWriter myBufferedWriter=new BufferedWriter(new OutputStreamWriter(OS,"UTF-8"));
                String myData= URLEncoder.encode("username","UTF-8")+"="+URLEncoder.encode(username,"UTF-8")+"&"
                        +URLEncoder.encode("password","UTF-8")+"="+URLEncoder.encode(password,"UTF-8")+"&"
                +URLEncoder.encode("retypedPassword","UTF-8")+"="+URLEncoder.encode(retypedPassword,"UTF-8")+"&"+
                        URLEncoder.encode("email","UTF-8")+"="+URLEncoder.encode(email,"UTF-8")+"&"+
                        URLEncoder.encode("passphrase","UTF-8")+"="+URLEncoder.encode(passphrase,"UTF-8");
                myBufferedWriter.write(myData);
                myBufferedWriter.flush();
                myBufferedWriter.close();
                OS.close();
                InputStream IS=urlConnection.getInputStream();
                IS.close();
                return "Registration Complete!";

            } catch (IOException e) {
                e.printStackTrace();
            }
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }


    }
    return null;
}

@Override
protected void onProgressUpdate(Void... values) {
    super.onProgressUpdate(values);
}

@Override
protected void onPostExecute(String result) {
    Toast.makeText(myContext,result,Toast.LENGTH_LONG).show();
}

} 这是我用来连接数据库的外部java类AddDataToDatabase。

<?php require "initialize.php";
$username=$_POST["username"];
$password=$_POST["password"];
$retypedPassword=$_POST["retypedPassword"];
$email=$_POST["email"];
$passphrase=$_POST["passphrase"];
$sql_query="insert into accounts values('username','$password','$retypedPassword','$email','$passphrase');";
if(mysqli_query($con,$sql_query))
{
echo "Data insertion succes..";
}
else
{
echo "Data insertion error..".mysqli_error($con);
}
?>

这是我的register.php脚本

<?php
$db_name="android_accounts_db";
$mysql_user="root";
$mysql_pass="root";
$server_name="localhost";
$con=mysqli_connect($server_name,$mysql_user,$mysql_pass,$db_name);
if(!$con){
}else{
}
?>

这是我的initialize.php脚本。 根据我在互联网上的红色,AddDataToDatabase类中的registration_url变量可能存在问题。我必须提到我使用了我的IPV4地址。在数据库中,我可以看到在accounts表中创建的所有列,但其中没有数据。提前谢谢!

0 个答案:

没有答案