2012-04-30 25 views
2

它很長,經過大量的研究,我無法得到我真正想要的解決方案。我做了一個GPS跟蹤應用程序。現在我想要將我從手機獲取的座標發佈到本地主機服務器。而且它也會每5分鐘發佈一次座標。我用WAMP製作了一臺服務器。從Android手機發布在服務器上

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 

} 

請一定幫我這個...

+0

請提供更多的細節。我可以告訴你正在試圖將數據放到服務器上的mysql數據庫中,但是你沒有提到你遇到了什麼錯誤或者遇到了什麼問題。 – DeaconDesperado

+0

另外,您是使用數據交換格式(json/xml)還是僅將這些內容作爲嵌入請求中的post/get字段發送? – DeaconDesperado

+1

您沒有在您的帖子中提出任何問題。請詢問具體的問題。 – dm03514

回答

0

不要在URL中使用localhost,使用服務器的IP地址來代替。 和你的PHP腳本有壞的開始標籤:的?php代替<?php

請評論什麼錯誤,你得到,如果這沒有幫助...

+0

如何獲取服務器的ip地址......?在windows中打開 –

+0

:打開命令提示符('run> cmd')並鍵入'ipconfig',你將得到你的IP地址。在Ubuntu中: :右鍵單擊網絡小程序並選擇「連接信息」 – vtuhtan