如何将您的Android应用程序与数据库连接

时间:2015-12-13 17:10:16

标签: java php android mysql

我在android studio上创建了一个登录表单,我想将它与我的数据库链接。现在,我在youtube上看到了很少的教程,但几乎每个人都使用wampserver和windows计算机。我有mac,我已经下载了xampp并在phpmyadmin上创建了一个简单的表。 我看到的教程,他打开webapp文件夹并从计算机编辑php文件,但我怎么能这样做? 我要粘贴一些代码,希望你们知道最新情况。

public 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();
}
}

上面显示的代码是我的注册java类。以下是我创建的另一个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 reg_url = "http://10.0.2.2/";
    String login_url = "http://10.0.2.2/";
    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();
    }


}
}

任何人都可以帮助我如何将其与我的数据库连接。我想当有人填写注册表单并单击注册时,它会将该信息保存在我的数据库中。

0 个答案:

没有答案