現在彈珠台上已經有感測器可以紀錄資料,

利用在Udemy上購買的課程學習使用php建立資料庫在以手機app手動輸入資料,

第一個頁面

按下按鈕後會運行java檔

第二個介面

宣告3個editext以及1個按鈕的功能

<package com.example.user.demodatabase;

import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.EditText;

public class register extends AppCompatActivity {

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
EditText e_name,e_password,e_contact,e_country;
String name,password,contact;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_register);
    e_name=(EditText) findViewById(R.id.editname);
    e_password=(EditText) findViewById(R.id.editpassword);
    e_contact=(EditText) findViewById(R.id.editcontact);



}
public void reguser (View view){
    name=e_name.getText().toString();
    password=e_password.getText().toString();
    contact=e_contact.getText().toString();

    String method ="register";
    bakeground bakeground=new bakeground(this);
    bakeground.execute(method,name,password,contact);
    finish();
}

}>

在這個頁面輸入資料後按下Insert後會觸發一個背景程式

<package com.example.user.demodatabase;

import android.content.Context; import android.os.AsyncTask; import android.widget.Toast;

import java.io.BufferedWriter; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.io.OutputStreamWriter; import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.URL; import java.net.URLEncoder;

/* * Created by user on 2018/3/14. /

public class bakeground extends AsyncTask< { Context ctx; bakeground(Context ctx){ this.ctx=ctx; }

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
protected String doInBackground(String... voids) {
    String reg_url ="http://";
    String method=voids[0];
    if(method.equals("register")){
        String name=voids[1];
        String password=voids[2];
        String contact=voids[3];
        String country=voids[4];
        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("name","UTF-8")+"="+URLEncoder.encode(name,"UTF-8")+"&"+
                    URLEncoder.encode("password","UTF-8")+"="+URLEncoder.encode(password,"UTF-8")+"&"+
                    URLEncoder.encode("contact","UTF-8")+"="+URLEncoder.encode(contact,"UTF-8")+"&"+
                    URLEncoder.encode("country","UTF-8")+"="+URLEncoder.encode(country,"UTF-8");

            bufferedWriter.write(data);
            bufferedWriter.flush();
            bufferedWriter.close();
            os.close();
            InputStream IS=httpURLConnection.getInputStream();
            IS.close();
            return "Registration success";


        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    return null;
}

public bakeground() {
    super();
}

@Override
protected void onPostExecute(String result) {

    Toast.makeText(ctx,result,Toast.LENGTH_LONG).show();
}
@Override
protected void onPreExecute() {
    super.onPreExecute();
}

@Override
protected void onProgressUpdate(Void... values) {
    super.onProgressUpdate(values);
}

}

這個程式會發送http網路請求到伺服端,將上面輸入的資料送進資料庫裡


Comments

comments powered by Disqus