それは長い間、多くの調査の後、私が本当に望んでいるものに対する解決策を得ることができませんでした. GPS追跡アプリを作りました。今、電話から取得した座標をローカルホストサーバーに投稿したいと思います。それも 5 分ごとに座標を投稿します。WAMPを使ってサーバーを作りました。
PHP スクリプト
?php
echo 'Hello, world!';
$con = mysql_connect("localhost","root","");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("ric_db", $con);
$devid = $_POST["devid"];
$latitude = $_POST["latitude"];
$longitude = $_POST["longitude"];
$sql = "INSERT INTO `ric_db`.`locations` (
`id` ,
`devid` ,
`latitude` ,
`longitude` ,
`service`
)
VALUES (
NULL , '$devid', '$latitude', '$longitude', '$service'
);";
if (!mysql_query($sql,$con))
{
die('Error: ' . mysql_error());
}
mysql_close($con);
?>
Java ファイル
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
/* Use the LocationManager class to obtain GPS locations */
LocationManager mlocManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
LocationListener mlocListener = new MyLocationListener();
mlocManager.requestLocationUpdates( LocationManager.GPS_PROVIDER, 0, 0, mlocListener);
}
/* Class My Location Listener */
public class MyLocationListener implements LocationListener
{
Context context;
public void onLocationChanged(Location loc)
{
StringBuilder sb = null;
String result = null;
InputStream is = null;
double latitude = loc.getLatitude();
double longitude = loc.getLongitude();
Toast.makeText(context, "Latitude:" +latitude + "Longitude:" +longitude, 5000).show();
TelephonyManager telephonyManager = (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);
String Devid = telephonyManager.getDeviceId();
//this is JSON part to put your information inside it
String postData = "{\"request\":{\"type\":\"locationinfo\"},\"userinfo\":{\"Devid\":\""+Devid+"\",\"latitude\":\""+latitude+"\",\"longitude\":\""+longitude+"\"}}";
String url="http://localhost/mehul/store.php";
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(3);
nameValuePairs.add(new BasicNameValuePair("Devid", Devid));
nameValuePairs.add(new BasicNameValuePair("latitude", Double.toString(latitude) ));
nameValuePairs.add(new BasicNameValuePair("longitude", Double.toString(longitude)));
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(url);
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
HttpEntity http_entity = response.getEntity();
Log.i("postData", response.getStatusLine().toString());
BufferedReader br = new BufferedReader(
new InputStreamReader(http_entity.getContent()));
String res = br.readLine();
System.out.println(res);
}
public void onProviderDisabled(String arg0) {
// TODO Auto-generated method stub
}
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
}
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}
これで私を助けてください...