Java 如何将phpmyadmin数据库与android应用程序连接

Java 如何将phpmyadmin数据库与android应用程序连接,java,php,android,mysql,database,Java,Php,Android,Mysql,Database,我以前问过这个问题,但那时候我没有php文件,现在我已经创建了php文件并保存在我的xampp文件夹中,我已经检查了它的工作情况,问题出在代码的某个地方。我正在尝试将我的android studio注册页面与数据库链接,但它不起作用。当用户输入他们的详细信息时,它会自动将其保存在我在phpmyadmin上创建的数据库中,但当我在应用程序上单击register时,什么都不会发生。我要通过下面的代码,有人能告诉我问题出在哪里吗? 谢谢 这是我的注册java类 ublic class Register

我以前问过这个问题,但那时候我没有php文件,现在我已经创建了php文件并保存在我的xampp文件夹中,我已经检查了它的工作情况,问题出在代码的某个地方。我正在尝试将我的android studio注册页面与数据库链接,但它不起作用。当用户输入他们的详细信息时,它会自动将其保存在我在phpmyadmin上创建的数据库中,但当我在应用程序上单击register时,什么都不会发生。我要通过下面的代码,有人能告诉我问题出在哪里吗? 谢谢

这是我的注册java类

ublic class Register extends Activity {

EditText ET_FIRST_NAME, ET_LAST_NAME, ET_ADDRESS, ET_EMAIL, ET_PASSWORD;
String first_name,last_name,address,email,password;

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

    ET_FIRST_NAME = (EditText)findViewById(R.id.etFname);
    ET_LAST_NAME = (EditText)findViewById(R.id.etLname);
    ET_ADDRESS = (EditText)findViewById(R.id.etAddress);
    ET_EMAIL = (EditText)findViewById(R.id.etEmail);
    ET_PASSWORD = (EditText)findViewById(R.id.etPassword);
}

public void btRegister(View view)
{
    first_name =  ET_FIRST_NAME.getText().toString();
    last_name = ET_LAST_NAME.getText().toString();
    address = ET_ADDRESS.getText().toString();
    email = ET_EMAIL.getText().toString();
    password = ET_PASSWORD.getText().toString();

    String method = "register";
    BackgroundTask backgroundTask = new BackgroundTask(this);
    backgroundTask.execute(method, first_name,last_name,address,email,password);
    finish();
}
}
这就是活动

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_height="match_parent"
android:padding="15dp"
android:layout_width="match_parent"
android:background="#8a73d5">


<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Register Form"
    android:textAppearance="?android:textAppearanceLarge"
    android:textStyle="bold"
    android:layout_gravity="center_horizontal"
    android:layout_marginTop="2dp"
    />

<TextView
    android:layout_width="wrap_content"
    android:text="First Name"
    android:layout_height="wrap_content"
    android:textColor="#000000" />

<EditText
    android:id="@+id/etFname"
    android:layout_width="match_parent"

    android:layout_height="wrap_content"
     />

<TextView
    android:layout_width="wrap_content"
    android:text="Last Name"
    android:layout_height="wrap_content"
    android:textColor="#000000"
    android:background="#8a73d5" />

<EditText
    android:id="@+id/etLname"
    android:layout_width="match_parent"
    android:layout_marginBottom="1dp"
    android:layout_height="wrap_content"
     />

<TextView
    android:layout_width="wrap_content"
    android:text="Address"
    android:layout_height="wrap_content"
    android:textColor="#000000" />

<EditText
    android:id="@+id/etAddress"
    android:layout_width="match_parent"
    android:layout_marginBottom="1dp"
    android:layout_height="wrap_content"
    />

<TextView
    android:layout_width="wrap_content"
    android:text="Email"
    android:layout_height="wrap_content"
    android:textColor="#000000" />

<EditText
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:inputType="textEmailAddress"
    android:id="@+id/etEmail"
     />

<TextView
    android:layout_width="wrap_content"
    android:text="Password"
    android:layout_height="wrap_content"
    android:textColor="#000000" />

<EditText
    android:id="@+id/etPassword"
    android:layout_width="match_parent"
    android:layout_marginBottom="1dp"
    android:inputType="textPassword"
    android:layout_height="wrap_content"
     />


<Button
    android:id="@+id/btRegister"
    android:text="Register"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />

这是后台任务类,其中所有代码都用于将信息(登录和注册)保存到数据库

public class BackgroundTask extends AsyncTask <String, Void, String> {
AlertDialog alertDialog;
Context ctx;

BackgroundTask(Context ctx){

    this.ctx = ctx;
}
@Override
protected void onPreExecute() {

    alertDialog = new AlertDialog.Builder(ctx).create();
    alertDialog.setTitle("Login Infomration");
}

@Override
protected String doInBackground(String... params) {
   String reg_url = "http://192.168.0.10/webapp/register.php";
    String login_url = "http://192.168.0.10/webapp/login.php";
    String method = params [0];
    if (method.equals("register") ){
        String first_name = params [1];
        String last_name = params [2];
        String address = params [3];
        String email = params [4];
        String password = params [5];
        try {
            URL url = new URL(reg_url);
            HttpURLConnection httpURLConnection = (HttpURLConnection)url.openConnection();
            httpURLConnection.setRequestMethod("POST");
            httpURLConnection.setDoOutput(true);
            OutputStream OS = httpURLConnection.getOutputStream();
            BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(OS, "UTF-8"));

            String data = URLEncoder.encode("first_name", "UTF-8") + "=" + URLEncoder.encode(first_name, "UTF-8") + "&" +
                    URLEncoder.encode("last_name", "UTF-8") + "=" + URLEncoder.encode(last_name, "UTF-8") + "&" +
                    URLEncoder.encode("address", "UTF-8") + "=" + URLEncoder.encode(address, "UTF-8")+ "&" +
                    URLEncoder.encode("email", "UTF-8") + "=" + URLEncoder.encode(email, "UTF-8")+ "&" +
                    URLEncoder.encode("password", "UTF-8") + "=" + URLEncoder.encode(password, "UTF-8");
            bufferedWriter.write(data);
            bufferedWriter.flush();
            bufferedWriter.close();
            OS.close();
            InputStream IS = httpURLConnection.getInputStream();
            IS.close();
            httpURLConnection.disconnect();
            return "Registration Has Been Successful.";
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    else if(method.equals("login"))
    {
        String login_name = params[1];
        String login_pass = params[2];
        try {
            URL url = new URL(login_url);
            HttpURLConnection httpURLConnection = (HttpURLConnection)url.openConnection();
            httpURLConnection.setRequestMethod("POST");
            httpURLConnection.setDoOutput(true);
            httpURLConnection.setDoInput(true);
            OutputStream outputStream = httpURLConnection.getOutputStream();
            BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream,"UTF-8"));
            String data = URLEncoder.encode("login_name","UTF-8")+"="+URLEncoder.encode(login_name,"UTF-8")+"&"+
                    URLEncoder.encode("login_pass","UTF-8")+"="+URLEncoder.encode(login_pass,"UTF-8");
            bufferedWriter.write(data);
            bufferedWriter.flush();
            bufferedWriter.close();
            outputStream.close();
            InputStream inputStream = httpURLConnection.getInputStream();
            BufferedReader bufferedReader = new BufferedReader (new InputStreamReader(inputStream,"iso-8859-1"));
            String response = "";
            String line = "";
            while ((line = bufferedReader.readLine())!=null)
            {
                response+= line;
            }
            bufferedReader.close();
            inputStream.close();
            httpURLConnection.disconnect();
            return response;
        } 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) {
    if(result.equals("Registration Has Been Successful."))
    {
        Toast.makeText(ctx, result, Toast.LENGTH_LONG).show();
    }
else
    {
        alertDialog.setMessage(result);
        alertDialog.show();
    }

}
}
公共类BackgroundTask扩展了AsyncTask{
警报对话框警报对话框;
上下文ctx;
背景任务(上下文ctx){
this.ctx=ctx;
}
@凌驾
受保护的void onPreExecute(){
alertDialog=新建alertDialog.Builder(ctx.create();
alertDialog.setTitle(“登录信息”);
}
@凌驾
受保护的字符串doInBackground(字符串…参数){
字符串注册表url=”http://192.168.0.10/webapp/register.php";
字符串登录\u url=”http://192.168.0.10/webapp/login.php";
字符串方法=参数[0];
if(方法等于(“寄存器”)){
字符串first_name=params[1];
字符串last_name=params[2];
字符串地址=参数[3];
字符串email=params[4];
字符串密码=参数[5];
试一试{
URL=新URL(注册URL);
HttpURLConnection HttpURLConnection=(HttpURLConnection)url.openConnection();
httpURLConnection.setRequestMethod(“POST”);
httpURLConnection.setDoOutput(true);
OutputStream OS=httpURLConnection.getOutputStream();
BufferedWriter BufferedWriter=新的BufferedWriter(新的OutputStreamWriter(OS,“UTF-8”));
字符串数据=urlcoder.encode(“名字”,“UTF-8”)+”=“+urlcoder.encode(名字,“UTF-8”)+”&”+
URLEncoder.encode(“姓氏”,“UTF-8”)+”=“+URLEncoder.encode(姓氏,“UTF-8”)+”&”+
URLEncoder.encode(“地址”,“UTF-8”)+”=“+URLEncoder.encode(地址,“UTF-8”)+”&”+
URLEncoder.encode(“电子邮件”,“UTF-8”)+”=“+URLEncoder.encode(电子邮件,“UTF-8”)+”&”+
urlcoder.encode(“密码”,“UTF-8”)+“=”+urlcoder.encode(密码,“UTF-8”);
bufferedWriter.write(数据);
bufferedWriter.flush();
bufferedWriter.close();
OS.close();
InputStream=httpURLConnection.getInputStream();
IS.close();
httpURLConnection.disconnect();
return“注册已成功。”;
}捕获(格式错误){
e、 printStackTrace();
}捕获(IOE异常){
e、 printStackTrace();
}
}
else if(method.equals(“login”))
{
字符串login_name=params[1];
字符串login_pass=params[2];
试一试{
URL=新URL(登录\ URL);
HttpURLConnection HttpURLConnection=(HttpURLConnection)url.openConnection();
httpURLConnection.setRequestMethod(“POST”);
httpURLConnection.setDoOutput(true);
httpURLConnection.setDoInput(true);
OutputStream OutputStream=httpURLConnection.getOutputStream();
BufferedWriter BufferedWriter=新的BufferedWriter(新的OutputStreamWriter(outputStream,UTF-8));
字符串数据=urlcoder.encode(“登录名”,“UTF-8”)+“=”+urlcoder.encode(登录名,“UTF-8”)+“&”+
urlcoder.encode(“登录密码”,“UTF-8”)+“=”+urlcoder.encode(登录密码,“UTF-8”);
bufferedWriter.write(数据);
bufferedWriter.flush();
bufferedWriter.close();
outputStream.close();
InputStream InputStream=httpURLConnection.getInputStream();
BufferedReader BufferedReader=新的BufferedReader(新的InputStreamReader(inputStream,“iso-8859-1”);
字符串响应=”;
字符串行=”;
而((line=bufferedReader.readLine())!=null)
{
响应+=行;
}
bufferedReader.close();
inputStream.close();
httpURLConnection.disconnect();
返回响应;
}捕获(格式错误){
e、 printStackTrace();
}捕获(IOE异常){
e、 printStackTrace();
}
}
返回null;
}
@凌驾
受保护的void onProgressUpdate(void…值){
super.onProgressUpdate(值);
}
@凌驾
受保护的void onPostExecute(字符串结果){
if(result.equals(“已成功注册”))
{
Toast.makeText(ctx,result,Toast.LENGTH_LONG).show();
}
其他的
{
alertDialog.setMessage(结果);
alertDialog.show();
}
}
}

除了使用asynctask与php连接外,您还可以使用volley库来自动化该过程。它将自己发出http请求,并直接为您获取结果。一个很好的介绍指南是:

在尝试使用asyctask完成任务后,我切换到截击,并且只进行了一次尝试

快乐编码