1

我想在使用 telnet 更改位置时将 xml 文件中的两个变量(lat 和 longi)发送到本地服务器,调用 d 中的客户端代码。当我更改位置时,它显示“不幸的是,您的应用程序已停止”。我正在使用 post 方法。我是android中客户端服务器代码的新手。请任何人帮助我....

这是我的代码

String responseBody;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    /* Use the LocationManager class to obtain GPS locations */
    LocationManager mlocManager =
    (LocationManager)getSystemService(Context.LOCATION_SERVICE);
    LocationListener mlocListener = new MyLocationListener();
    mlocManager.requestLocationUpdates( LocationManager.GPS_PROVIDER, 0, 0, mlocListener);

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.activity_main, menu);
    return true;
}

/* Class My Location Listener */
public class MyLocationListener implements LocationListener
{
    double lat, longi;

public void onLocationChanged(Location loc)    {
    lat = loc.getLatitude();
    longi = loc.getLongitude();
    String Text = "My current location is: "+"Latitude = "+lat+"Longitude = "+longi;
    Toast.makeText( getApplicationContext(), Text, Toast.LENGTH_LONG).show();


    // problem starts here
     try 
    { 
    String s = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" 
                   +"<lat>" + lat +"</name>\n" 
                   +"<long>" + longi + "</number>\n"; 
    String url = "http://localhost:8080/android/test.php?data="+s; 
    HttpClient httpclient = new DefaultHttpClient(); 
     HttpPost httpPost = new HttpPost(URI.create(url)); 
     httpPost.setHeader("Content-type","text/xml; charset=ISO-8859-1"); 
    ResponseHandler<String> responseHandler = new BasicResponseHandler(); 
    responseBody = httpclient.execute(httpPost, responseHandler); 
    } 

    catch(IOException e) 
    { 
    e.printStackTrace(); 
    } 

}

public void onProviderDisabled(String provider)    {
    Toast.makeText( getApplicationContext(), "Gps Disabled", Toast.LENGTH_SHORT ).show();
}

public void onProviderEnabled(String provider)    {
    Toast.makeText( getApplicationContext(), "Gps Enabled", Toast.LENGTH_SHORT).show();
}

public void onStatusChanged(String provider, int status, Bundle extras)
{

}


}/* End of Class MyLocationListener */
4

0 回答 0