无法将GPS坐标发送到服务器

时间:2014-01-21 09:56:46

标签: java android apache gps

我正在尝试将GPS坐标发送到我的应用程序获取的服务器,它正确显示GPS坐标,但无法将其发送到服务器,问题出在我的MainActivity.java中 这是我的代码

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.NameValuePair;

import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.AsyncTask;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity implements LocationListener {

  private TextView latituteField;
  private TextView longitudeField;
  private LocationManager locationManager;
  private String provider;
  int lat,lng;


  /** Called when the activity is first created. */

  @Override
    public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_show_location);
      latituteField = (TextView) findViewById(R.id.TextView02);
      longitudeField = (TextView) findViewById(R.id.TextView04);

      // Get the location manager
      locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
      // Define the criteria how to select the locatioin provider -> use
      // default
      Criteria criteria = new Criteria();
      provider = locationManager.getBestProvider(criteria, false);
      Location location = locationManager.getLastKnownLocation(provider);

      // Initialize the location fields
      if (location != null) {
        System.out.println("Provider " + provider + " has been selected.");
        onLocationChanged(location);
      } else {
        latituteField.setText("Location not available");
        longitudeField.setText("Location not available");
      }
    }

  /* Request updates at startup */
  @Override
    protected void onResume() {
      super.onResume();
      locationManager.requestLocationUpdates(provider, 400, 1, this);
    }

  /* Remove the locationlistener updates when Activity is paused */
  @Override
    protected void onPause() {
      super.onPause();
      locationManager.removeUpdates(this);
    }



  @Override
    public void onLocationChanged(Location location) {
      lat = (int) (location.getLatitude());
      lng = (int) (location.getLongitude());
      latituteField.setText(String.valueOf(lat));
      longitudeField.setText(String.valueOf(lng));



    }



  @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {
      // TODO Auto-generated method stub

    }

  @Override
    public void onProviderEnabled(String provider) {
      Toast.makeText(this, "Enabled new provider " + provider,
          Toast.LENGTH_SHORT).show();

    }

  @Override
    public void onProviderDisabled(String provider) {
      Toast.makeText(this, "Disabled provider " + provider,
          Toast.LENGTH_SHORT).show();
    }

  private class LoadServerASYNC extends AsyncTask<String, Void, String> {

    @Override
      protected String doInBackground(String... arg0) {
        // TODO Auto-generated method stub
        return null;
      }

    void postData() {
      // Create a new HttpClient and Post Header
      HttpClient httpclient = new DefaultHttpClient();
      HttpPost httppost = new HttpPost("http://182.18.144.140:80"); //your php file path

      try {
        // Add your data
        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
        nameValuePairs.add(new BasicNameValuePair("lat", "1.0256"));
        nameValuePairs.add(new BasicNameValuePair("lng", "1.0256"));
        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

        // Execute HTTP Post Request
        HttpResponse response = httpclient.execute(httppost);

      } catch (ClientProtocolException e) {
        // TODO Auto-generated catch block
      } catch (IOException e) {
        // TODO Auto-generated catch block
      }

    }
  }
}

我没有得到什么问题

1 个答案:

答案 0 :(得分:1)

您没有在任何地方初始化和执行LoadServerAsync。

如果您想在每次更新文本视图时发送坐标,请执行以下操作:

@Override
public void onLocationChanged(Location location) {
    lat = (int) (location.getLatitude());
    lng = (int) (location.getLongitude());
    latituteField.setText(String.valueOf(lat));
    longitudeField.setText(String.valueOf(lng));
    LoadServerASYNC task = new LoadServerASYNC();
    task.execute();
   }

你应该在doInBackground();

中调用postData方法