doupingyun73833 2016-04-04 02:14
浏览 424

无法使用android studio将数据插入MySQL数据库

I am a beginner with Android Studio. I want to build a project that user can register and then login using their own accounts. I follow the Prabeesh tutorial Android - MySQL - 02 - Add data into MySQL Database, https://www.youtube.com/watch?v=cOsZHuu8Qog. Every time click register, which should insert data to the MySQL data, MySQL database doesn't update. Here is the code:

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

}

public void userReg(View view){
    startActivity(new Intent(this, Register.class));

}

public void userLogin(View view){

    }
}

Here is the code for register activity:

public class Register extends Activity {

    EditText etName, etUsername, etPassword;
    String name, user_name, user_pass;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);

        etName = (EditText)findViewById(R.id.name);
        etUsername = (EditText)findViewById(R.id.new_user_name);
        etPassword = (EditText)findViewById(R.id.new_user_pass);
    }

    public void userReg(View view){

        name = etName.getText().toString();
        user_name = etUsername.getText().toString();
        user_pass = etPassword.getText().toString();

        String method = "register";
        BackgroundTask backgroundTask = new BackgroundTask(this);
        backgroundTask.execute(method,name,user_name,user_pass);
        finish();

    }
}

Here is the code for BackgroundTask

public class BackgroundTask extends AsyncTask<String, Void, String> {

    Context ctx;
    BackgroundTask(Context ctx){
        this.ctx = ctx;
    }

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
    }

    @Override
    protected String doInBackground(String... params) {

        String reg_url = "http://10.0.2.2/webapp/register.php";
        String login_url = "http://10.0.2.2/webapp/login.php";
        String method = params[0];
        if (method.equals("register")) {
            String name = params[1];
            String user_name = params[2];
            String user_pass = params[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("user", "UTF-8") + "=" + URLEncoder.encode(name, "UTF-8") + "&" +
                        URLEncoder.encode("user_name", "UTF-8") + "=" + URLEncoder.encode(user_name, "UTF-8") + "&" +
                        URLEncoder.encode("user_pass", "UTF-8") + "=" + URLEncoder.encode(user_pass, "UTF-8");
                bufferedWriter.write(data);
                bufferedWriter.flush();
                bufferedWriter.close();
                OS.close();
                InputStream IS = httpURLConnection.getInputStream();
                IS.close();
                //httpURLConnection.connect();
                httpURLConnection.disconnect();
                return "Registration Success...";
            } 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(ctx,result,Toast.LENGTH_LONG).show();
    }
}

I use wampserver as a localhost, and I can only open my server homepage with localhost:81, instead of localhost. So I am wondering whether I should use this http://10.0.2.2/ as the IP address.

PHP file should work as following: init.php:

<?php  
 $db_name = "webappdb";  
 $mysql_user = "root";  
 $mysql_pass = "root";  
 $server_name = "localhost";  
 $con = mysqli_connect($server_name,$mysql_user,$mysql_pass,$db_name);  
 ?>  

register.php:

<?php  
 require "init.php";  
 $name = $_POST["user"];  
 $user_name = $_POST["user_name"];  
 $user_pass = $_POST["user_pass"];  
 $sql_query = "insert into user_info values('$name','$user_name','$user_pass');";  
 ?>  

Someone please tell me where I went wrong. Thanks

  • 写回答

2条回答 默认 最新

  • dongqiao1158 2016-04-04 03:14
    关注

    in register.php, you must add mysqli_query to execute the query.

    <?php  
     require "init.php";  
     $name = $_POST["user"];  
     $user_name = $_POST["user_name"];  
     $user_pass = $_POST["user_pass"];  
     $sql_query = "insert into user_info values('$name','$user_name','$user_pass')";
    mysqli_query($con,$sql_query);
     ?>
    
    评论

报告相同问题?

悬赏问题

  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题
  • ¥15 Visual Studio问题
  • ¥20 求一个html代码,有偿
  • ¥100 关于使用MATLAB中copularnd函数的问题