0
我正在創建簡單的GPS跟蹤器。應用程序獲取gps緯度/經度並將其發送到遠程服務器上的php。onLocationChange() - 不能發送帖子請求
@Override
public void onLocationChanged(Location loc)
{
String infLat = Double.toString(loc.getLatitude());
String infLon = Double.toString(loc.getLongitude());
String Text = "My current location is: " +
"Latitud = " + infLat +
"Longitud = " + infLon;
Toast.makeText(getApplicationContext(),
Text,
Toast.LENGTH_SHORT).show();
uploadLoc(infLat, infLon); // calling method which sends location info
}
這裏是uploadLoc:
public void uploadLoc(String a, String b) {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://link to script");
try {
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("latitude", a));
nameValuePairs.add(new BasicNameValuePair("longitude", b));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
// Execute HTTP Post Request
HttpResponse response = httpclient.execute(httppost);
} catch (ClientProtocolException e) {
//
} catch (IOException e) {
//
}
}
但我經常收到 「應用程序已停止」。當我刪除正在調用uploadLoc方法的行時,一切正常,並且Toast更新爲位置更改。這裏有什麼可能是錯的?
您確定沒有看到NetworkOnMainThreadException? – wtsang02