这是我的代码,用于获取当前位置和地址,并使用post方法将其发送到网址
final Handler myHandler = new Handler();
runnableforadd = new Runnable()
{
public void run()
{
gps = new GPSTracker(dashboard.this);
// check if GPS enabled
if(gps.canGetLocation()){
double latitude = gps.getLatitude();
double longitude = gps.getLongitude();
geocoder = new Geocoder(dashboard.this, Locale.getDefault());
try {
List<Address> addresses = geocoder.getFromLocation(latitude, longitude, 1);
if(addresses != null) {
Address returnedAddress = addresses.get(0);
strReturnedAddress = new StringBuilder("");
for(int i=0; i<returnedAddress.getMaxAddressLineIndex(); i++) {
strReturnedAddress.append(returnedAddress.getAddressLine(i)).append("\n");
}
ArrayList<NameValuePair> postParameters = new ArrayList<NameValuePair>();
postParameters.add(new BasicNameValuePair("uid", uid));
postParameters.add(new BasicNameValuePair("location",strReturnedAddress.toString()));
postParameters.add(new BasicNameValuePair("lat", String.valueOf(latitude)));
postParameters.add(new BasicNameValuePair("lng", String.valueOf(longitude)));
postParameters.add(new BasicNameValuePair("did", dsid));
CustomHttpClient.executeHttpPost("http://www.cjcld.org/dvts1/mobile/updatesPeriod.php", postParameters);
Toast.makeText(getApplicationContext(), "Your Location is - \nLat: " + latitude + "\nLong: " + longitude+"\n"+strReturnedAddress.toString(), Toast.LENGTH_LONG).show();
myHandler.postDelayed(runnableforadd, 10000);
}else{
Toast.makeText(getApplicationContext(), "Your Location is - \nLat: " + latitude + "\nLong: " + longitude+"\nNo address found", Toast.LENGTH_LONG).show();
myHandler.postDelayed(runnableforadd, 10000);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// \n is for new line
}else{
// can't get location
// GPS or Network is not enabled
// Ask user to enable GPS/network in settings
gps.showSettingsAlert();
}
}
};
myHandler.postDelayed(runnableforadd, 10000);
但是这部分没有执行:
ArrayList<NameValuePair> postParameters = new ArrayList<NameValuePair>();
postParameters.add(new BasicNameValuePair("uid", uid));
postParameters.add(new BasicNameValuePair("location",strReturnedAddress.toString()));
postParameters.add(new BasicNameValuePair("lat", String.valueOf(latitude)));
postParameters.add(new BasicNameValuePair("lng", String.valueOf(longitude)));
postParameters.add(new BasicNameValuePair("did", dsid));
CustomHttpClient.executeHttpPost("http://www.cjcld.org/dvts1/mobile/updatesPeriod.php", postParameters);
我没有收到任何错误,只是没有执行。
这是logcat警告。
02-13 21:07:57.320: W/System.err(24264): java.io.IOException: Unable to parse response from server
02-13 21:07:57.330: W/System.err(24264):at android.location.Geocoder.getFromLocation(Geocoder.java:136)
02-13 21:07:57.330: W/System.err(24264): at com.example.deliveryvantrackingsystem.dashboard$1.run(dashboard.java:85)
02-13 21:07:57.330: W/System.err(24264):at android.os.Handler.handleCallback(Handler.java:605)
02-13 21:07:57.330: W/System.err(24264): at android.os.Handler.dispatchMessage(Handler.java:92)
02-13 21:07:57.330: W/System.err(24264):at android.os.Looper.loop(Looper.java:137)
02-13 21:07:57.330: W/System.err(24264):at android.app.ActivityThread.main(ActivityThread.java:4476)
02-13 21:07:57.330: W/System.err(24264):at java.lang.reflect.Method.invokeNative(Native Method)
02-13 21:07:57.330: W/System.err(24264):at java.lang.reflect.Method.invoke(Method.java:511)
02-13 21:07:57.340: W/System.err(24264):at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:816)
02-13 21:07:57.340: W/System.err(24264):at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:583)
02-13 21:07:57.340: W/System.err(24264):at dalvik.system.NativeStart.main(Native Method)`