我正在学习php for android,我正在尝试在数据库中插入注册数据。提交数据没有错误,但数据在数据库中不可见。 主要活动: -
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void userLogin(View view)
{
}
public void userReg(View view)
{
startActivity(new Intent(this, Register.class));
}
}
Register.java
public class Register extends AppCompatActivity {
EditText etName, etUname, etUpass;
String name, uname, upass;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.register);
etName = (EditText)findViewById(R.id.etName);
etUname = (EditText)findViewById(R.id.etUname);
etUpass = (EditText)findViewById(R.id.etUpass);
}
public void userReg(View view)
{
name = etName.getText().toString();
uname = etUname.getText().toString();
upass = etUpass.getText().toString();
String method = "register";
BackgroundTask bt = new BackgroundTask(this);
bt.execute(method,name,uname,upass);
finish();
}
}
BackgroundTask.java
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 regURL = "http://10.0.0.2/webapp/register.php";
String logURL = "http://10.0.0.2/webapp/login.php";
String method = params[0];
if(method.equals("register"))
{
String name = params[1];
String uname = params[2];
String upass = params[3];
try {
URL reg_url = new URL(regURL);
HttpURLConnection htc = (HttpURLConnection)reg_url.openConnection();
htc.setRequestMethod("POST");
htc.setDoOutput(true);
OutputStream os = htc.getOutputStream();
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(os,"UTF-8"));
String data = URLEncoder.encode("user","UTF-8") + "=" +URLEncoder.encode(name,"UTF-8")+ "&" +
URLEncoder.encode("username","UTF-8") + "=" +URLEncoder.encode(uname,"UTF-8")+ "&" +
URLEncoder.encode("userpass","UTF-8") + "=" +URLEncoder.encode(upass,"UTF-8");
bw.write(data);
bw.flush();
bw.close();
os.close();
InputStream is = htc.getInputStream();
is.close();
return "Registration Successful";
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}
@Override
protected void onProgressUpdate(Void... values) {
super.onProgressUpdate(values);
}
protected void onPostExecute(String result) {
Toast.makeText(ctx,result, Toast.LENGTH_LONG).show();
}
}
register.php
<?php
require 'db_connect.php';
$name = $_POST['user'];
$uname = $_POST['username'];
$upass = $_POST['userpass'];
$query = "INSERT INTO user_info (name,username,userpass) values('$name','$uname','$upass')";
$res = mysqli_query($conn,$query);
if($res)
{
//echo "Done";
}
else
{
//die(mysqli_error($conn));
}
?>
我点击按钮时最后会发出祝酒通知,但我甚至没有收到祝酒通知。日志中没有任何类型的错误。