将GPS从Android发送到服务器

将GPS从Android发送到服务器,android,http,gps,coordinates,Android,Http,Gps,Coordinates,我希望我的后台服务在位置发生变化时将当前GPS发送到服务器。现在我有这个代码: 公共无效启动(Intent Intent,int startid){ 问题是,当位置发生变化时,永远不会调用方法postData。postData使用HTTP将当前GPS发送到服务器 public void postData(String currLat, String currLon) { // List with arameters and their values String Text2

我希望我的后台服务在位置发生变化时将当前GPS发送到服务器。现在我有这个代码:

公共无效启动(Intent Intent,int startid){

问题是,当位置发生变化时,永远不会调用方法postData。postData使用HTTP将当前GPS发送到服务器

public void postData(String currLat, String currLon) { 
    // List with arameters and their values

    String Text2 = "String is: " +

    "Latitud = " + currLat +

    "Longitud = " + currLon;

    Toast.makeText(getApplicationContext(), Text2, Toast.LENGTH_LONG).show();

    HttpPost post = new HttpPost("http://www.url.com/*.php");
    List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
    nameValuePairs.add(new BasicNameValuePair("myusername", currLat));
    nameValuePairs.add(new BasicNameValuePair("mypassword", currLon));
    HttpClient client = new DefaultHttpClient();    

    try {
        post.setEntity(new UrlEncodedFormEntity(nameValuePairs));
        HttpResponse response = client.execute(post);
        HttpEntity entity = response.getEntity();
        String responseText = EntityUtils.toString(entity);
        responseText = responseText.trim();

    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    }
public void postData(字符串currLat,字符串currLon){
//列出参数及其值
String Text2=“字符串是:”+
“Latitud=“+currLat+
“Longitud=“+currLon;
Toast.makeText(getApplicationContext(),Text2,Toast.LENGTH_LONG.show();
HttpPost=新的HttpPost(“http://www.url.com/*.php”);
List nameValuePairs=新的ArrayList(2);
添加(新的BasicNameValuePair(“myusername”,currLat));
添加(新的BasicNameValuePair(“mypassword”,currLon));
HttpClient=new DefaultHttpClient();
试一试{
setEntity(新的UrlEncodedFormEntity(nameValuePairs));
HttpResponse response=client.execute(post);
HttpEntity=response.getEntity();
String responseText=EntityUtils.toString(实体);
responseText=responseText.trim();
}捕获(例外e){
//TODO自动生成的捕捉块
e、 printStackTrace();
}
}

它没有调用什么函数postData?即使它一次也没有调用它。

您从未向locationManager注册过locationListener。您应该使用一种形式的


不要忘了在您的服务顶部与您的侦听器通话。

我看不到您开始请求位置更新的任何地方。您是否可以确认您正在呼叫以下用户:

locationManager.requestLocationUpdates(provider, 0, 0, this);

提示:与其每次位置更改都发布,不如只在新位置优于前一个位置时发布。这是确定一个位置优于另一个位置的方法。

postData
从未调用过还是整个
onLocationChanged
从未调用过?还要注意,onLocationChanged可以调用很多次。如果在onLocationChanged中设置调试点,您将看到它可以每秒调用一次,甚至可能在一秒钟内多次调用。您可能需要设置更新的最小时间。
locationManager.requestLocationUpdates(provider, 0, 0, this);