将位置从GPS传递到Web服务时遇到困难

时间:2013-03-04 11:16:42

标签: java php android mysql httpwebrequest

我正在创建一个Android应用程序,它将获取我当前的位置并将其传递给我的Web服务。我现在没有错误,但我的问题是我无法将位置传递给我的网络服务。

这是我的代码,如果你知道那个问题,请向我指出 传递部分是在phpconnect类上。

**

public class Mapping extends MapActivity {
    private MapView mapView;
    private MapController mapController;
    private LocationManager locationManager;
    private LocationListener locationListener;

    // ** This declarations was for passing of data to web service
        // Progress Dialog
        private ProgressDialog pDialog;
        // JSONParser Object creation
        JSONParser jsonParser = new JSONParser();
        // url to pass location to web
        private static String url_create_product = "http://student-thesis.netii.net/location_adding.php";
        //private static String url_create_product = "http://10.0.2.2/TheCalling/location_adding.php";
        // JSON Node names
        private static final String TAG_SUCCESS = "success";
        //Latitude and Longitude
        public static double ILatitude;
        public static double ILongitude;
        // ** End
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.maps2);
        mapView = (MapView) findViewById(R.id.mapview);
        // enable to show Satellite view
        mapView.setSatellite(true);
        // enable to show Traffic on map
        mapView.setTraffic(true);
        mapView.setBuiltInZoomControls(true);
        mapController = mapView.getController();
        mapController.setZoom(16);
        locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        locationListener = new GPSLocationListener();
        locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 25000,
                5, locationListener);
    }
    private class GPSLocationListener implements LocationListener {
        @Override
        public void onLocationChanged(Location location) {
            // TODO Auto-generated method stub
            if (location != null) {
                ILatitude = (int)(location.getLatitude() * 1E6);
                ILongitude = (int)(location.getLongitude() * 1E6);

                GeoPoint point = new GeoPoint(
                        (int) (ILatitude),
                        (int) (ILongitude));
                Toast.makeText(
                        getBaseContext(),
                        "Latitude: " + location.getLatitude() + " Longitude: "
                                + location.getLongitude(), Toast.LENGTH_LONG)
                        .show();

                // add marker
                  MapOverlay mapOverlay = new MapOverlay();
                  mapOverlay.setPointToDraw(point);
                  List<Overlay> listOfOverlays = mapView.getOverlays();
                  listOfOverlays.clear();
                  listOfOverlays.add(mapOverlay);

                 String address = ConvertPointToLocation(point);
                 Toast.makeText(getBaseContext(), address, Toast.LENGTH_LONG).show();
                mapController.animateTo(point);
                mapController.setZoom(16);
                mapView.invalidate();
                new phpconnect().execute();
            }
        }
        @Override
        public void onProviderDisabled(String provider) {
            // TODO Auto-generated method stub
        }
        @Override
        public void onProviderEnabled(String provider) {
            // TODO Auto-generated method stub
        }
        @Override
        public void onStatusChanged(String provider, int status, Bundle extras) {
            // TODO Auto-generated method stub
        }
    }

    class MapOverlay extends Overlay
    {
      private GeoPoint pointToDraw;
      public void setPointToDraw(GeoPoint point) {
        pointToDraw = point;
      }
      public GeoPoint getPointToDraw() {
        return pointToDraw;
      }

      @Override
      public boolean draw(Canvas canvas, MapView mapView, boolean shadow, long when) {
        super.draw(canvas, mapView, shadow);           
        // convert point to pixels
        Point screenPts = new Point();
        mapView.getProjection().toPixels(pointToDraw, screenPts);
        // add marker
        Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.reddot);
        canvas.drawBitmap(bmp, screenPts.x, screenPts.y - 24, null);    
        return true;
      }
    }

    class phpconnect extends AsyncTask<String, String, String> {
        @Override
        protected String doInBackground(String... args) {
            String strLatitude = Double.toString(ILatitude);
            String strLongitude = Double.toString(ILongitude);
            // Building parameters
            List<NameValuePair> params = new ArrayList<NameValuePair>();
            params.add(new BasicNameValuePair("latitude", strLatitude));
            params.add(new BasicNameValuePair("longitude", strLongitude));
            // getting JSON Object
            // Note that create product url accepts POST method
            JSONObject json = jsonParser.makeHttpRequest(url_create_product,
                    "POST", params);
            // check log cat fro response
            Log.d("Create Response", json.toString());
            try {
                int success = json.getInt(TAG_SUCCESS);
                if (success == 1) {
                    // successfully updated
                    locationListener = new GPSLocationListener();
                } else {
                    // failed to create product
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
            return null;
        }
    }

    public String ConvertPointToLocation(GeoPoint point) {   
        String address = "";
        Geocoder geoCoder = new Geocoder(
            getBaseContext(), Locale.getDefault());
        try {
          List<Address> addresses = geoCoder.getFromLocation(
            point.getLatitudeE6()  / 1E6, 
            point.getLongitudeE6() / 1E6, 1);

          if (addresses.size() > 0) {
            for (int index = 0; 
        index < addresses.get(0).getMaxAddressLineIndex(); index++)
              address += addresses.get(0).getAddressLine(index) + " ";
          }
        }
        catch (IOException e) {        
          e.printStackTrace();
        }   

        return address;
      } 
    @Override
    protected boolean isRouteDisplayed() {
        // TODO Auto-generated method stub
        return false;
    }
}

**

这是我的网络代码:

**

<?php
$latitude=($_POST['latitude']);
$longitude=($_POST['longitude']);

$response = array();

    if(isset($_POST['latitude']) && isset($_POST['longitude'])){

       mysql_connect("XXXXXXx", "XXXXXXXX", "XXXXXXX" ) or die("could not find!");
       mysql_select_db("XXXXXXX") or die("Database do not exist!"); 

       $pid = 3;

        $result = mysql_query("UPDATE users SET latitude = '$latitude', longitude = '$longitude',  WHERE id=$pid");

        if ($result) {
        // successfully updated
        $response["success"] = 1;
        $response["message"] = "User information successfully updated.";

        // echoing JSON response
        echo json_encode($response);
    } else {

    }
} else {
    // required field is missing
    $response["success"] = 0;
    $response["message"] = "Required field(s) is missing";

    // echoing JSON response
    echo json_encode($response);
}
?>

**

提前谢谢你们。 :)

0 个答案:

没有答案