Automatically send String to webservice
I made an application that can view current gps coordinates, and now I want these coordinates automatically send to web service with out button, I made code below but wont send to web service.
public class sample_wifi extends Activity {
/** Called when the activity is first created. */
String latitude, longitude开发者_运维百科, speed;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
LocationManager mlocManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
LocationListener mlocListener = new MyLocationListener();
mlocManager.requestLocationUpdates( LocationManager.GPS_PROVIDER, 0, 0, mlocListener);
text="";
}
/* Class My Location Listener */
public class MyLocationListener implements LocationListener
{
@Override
public void onLocationChanged(Location loc)
{
loc.getLatitude();
loc.getLongitude();
loc.getSpeed();
loc.getTime();
String latitude = "latitude:" + loc.getLatitude () + "";
String longitude = "longitude:" + loc.getLongitude () + "";
String speed = "speed:" + loc.getSpeed () + "";
gps_lat=loc.getLatitude() + "";
gps_long=loc.getLongitude() + "";
speed = loc.getSpeed() + "";
Toast.makeText( getApplicationContext(),
latitude,
Toast.LENGTH_SHORT).show();
Toast.makeText( getApplicationContext(),
longitude,
Toast.LENGTH_SHORT).show();
Toast.makeText( getApplicationContext(),
speed,
Toast.LENGTH_SHORT).show();
}
@Override
public void onProviderDisabled(String provider)
{
Toast.makeText( getApplicationContext(),
"Gps Disabled",
Toast.LENGTH_SHORT ).show();
}
@Override
public void onProviderEnabled(String provider)
{
Toast.makeText( getApplicationContext(),
"Gps Enabled",
Toast.LENGTH_SHORT).show();
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras)
{
}
}
/////Posting Current coordinates
public void postData() {
// Create a new HttpClient and Post Header
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://my.webservice.php");
try {
// Add your data
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("userid", "me"));
nameValuePairs.add(new BasicNameValuePair("password", "12345"));
nameValuePairs.add(new BasicNameValuePair("latitude", latitude));
nameValuePairs.add(new BasicNameValuePair("longitude", longitude));
nameValuePairs.add(new BasicNameValuePair("speed", speed));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
// Execute HTTP Post Request
HttpResponse response = httpclient.execute(httppost);
InputStream is = response.getEntity().getContent();
BufferedInputStream bis = new BufferedInputStream(is);
ByteArrayBuffer baf = new ByteArrayBuffer(20);
int current = 0;
while((current = bis.read()) != -1){
baf.append((byte)current);
}
text = new String(baf.toByteArray());
if ("C0001".equals(text.substring(0,5)))
{
Toast.makeText( getApplicationContext(),
"Coordinates Sent",
Toast.LENGTH_SHORT).show();
}
else
{
Toast.makeText( getApplicationContext(),
"Coordinates Sending Failed! " + text,
Toast.LENGTH_SHORT).show();
}
text = new String(baf.toByteArray());
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
} catch (IOException e) {
// TODO Auto-generated catch block
}
};
}
}
精彩评论