public class MainActivity extends AppCompatActivity {
CheckBox box;
Button btn_test;
private static final String REGISTER_URL="http://192.168.0.106/test/checkin.php";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.mainactivity);
box = (CheckBox) findViewById(R.id.checkBox);
btn_test = (Button) findViewById(R.id.button);
btn_test.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
registerUser();
}
});
}
private void registerUser() {
// box.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
//public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {
// if(isChecked){
// String date = box.toString().trim().toLowerCase();
// register(date); } } } );
Boolean box1 = ((CheckBox)box).isChecked();
if (box1) {
String date = "yes";
} else {
String date = "No";
}
String date = box1.toString();
register(date);
}
private void register(String date){
String urlSuffix = "?date=" + date;
class RegisterUser extends AsyncTask<String, Void, String> {
ProgressDialog loading;
@Override
protected void onPreExecute() {
super.onPreExecute();
loading = ProgressDialog.show(MainActivity.this, "Please Wait", null, true, true);
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
loading.dismiss();
Toast.makeText(getApplicationContext(),"Registered", Toast.LENGTH_SHORT).show();
}
@Override
protected String doInBackground(String... params) {
String s = params[0];
BufferedReader bufferReader=null;
try {
URL url=new URL(REGISTER_URL+s);
HttpURLConnection con=(HttpURLConnection)url.openConnection();
bufferReader=new BufferedReader(new InputStreamReader(con.getInputStream()));
String result;
result=bufferReader.readLine();
return result;
}catch (Exception e){
return null;
}
}
}
RegisterUser ur=new RegisterUser();
ur.execute(urlSuffix);
}
}
我的php程序,
<?php
require "conn.php";
$date = $_GET['date'];
$sql = "INSERT INTO check (date) VALUES('$date')";
if($conn->query($sql)){
echo 'successfully registered';
}
else{
echo 'oops! Please try again!';
}
$conn->close();
}
?>