自动将String发送到webservice

时间:2011-08-04 07:10:10

标签: android string http gps

我创建了一个可以查看当前gps坐标的应用程序,现在我希望这些坐标自动发送到带有按钮的Web服务,我在下面创建了代码,但不会发送到Web服务。

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
    }
};

}

}

0 个答案:

没有答案