Android利用html get傳值給php儲存到mysql
Android利用html get傳值給php儲存到mysql
資料來源:http://charleslin74.pixnet.net/blog/post/435113753
Android程式碼部分如下
package com.example.user.myapplication;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONObject;
import android.app.Activity;
import android.os.Bundle;
import android.os.Looper;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
new Thread(new Runnable(){
@Override
public void run() {
Looper.prepare();
// TODO Auto-generated method stub
HttpClient client = new DefaultHttpClient();
HttpGet myget = new HttpGet("http://改成你主機的IP/login2.php?age=25");
try {
HttpResponse response = client.execute(myget);
} catch (Exception e) {
e.printStackTrace();
}
Looper.loop();
}}).start();
}
}
PHP程式碼部分如下
<?php
$getval = $_GET['age'];
echo $getval;
$link=mysql_connect("localhost","test123","test123");
mysql_query("SET NAMES utf8");
mysql_select_db("dbname",$link);
$sql=mysql_query("insert into tablename(age) values('$getval')",$link);
mysql_close();
?>
以上完成