【发布时间】:2016-04-04 02:14:33
【问题描述】:
我是 Android Studio 的初学者。我想建立一个用户可以注册然后使用自己的帐户登录的项目。我遵循 Prabeesh 教程 Android - MySQL - 02 - Add data into MySQL Database, https://www.youtube.com/watch?v=cOsZHuu8Qog。每次点击注册,应该向 MySQL 数据中插入数据,MySQL 数据库不会更新。 代码如下:
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){
}
}
这里是注册活动的代码:
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();
}
}
这是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();
}
}
我使用 wampserver 作为 localhost,我只能使用 localhost:81 打开我的服务器主页,而不是 localhost。所以我想知道我是否应该使用这个http://10.0.2.2/作为IP地址。
PHP 文件应该如下工作: 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);
?>
注册.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');";
?>
有人请告诉我哪里出错了。谢谢
【问题讨论】: