我正在创建简单的 GPS 跟踪器。应用程序获取 gps 纬度/经度并将其发送到远程服务器上的 php。
@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 会随着位置的变化而更新。这里有什么问题?