检索GPS位置并将其发送到Web服务器

时间:2011-03-25 13:58:15

标签: blackberry gps httpconnection

  

可能重复:
  Blackberry send a HTTPPost request

如何检索GPS坐标并将其发送到我的网络服务器。对于我的项目,我可以在线程中检索GPS位置,但我无法将其发送到我的服务器。

class SiganlerDabScreen extends MainScreen 
{
    private LabelField _coordLabel;
    private static String response;
    private static double _latitude;
    private static double _longitude;
    private int _modeUsed;
    private String _mode;
    BlackBerryCriteria myCriteria;



    public SiganlerDabScreen() 
    {
        super(DEFAULT_CLOSE | DEFAULT_MENU);
        setTitle(new LabelField("GPS", Field.USE_ALL_WIDTH | DrawStyle.HCENTER));


        this._coordLabel = new LabelField();
        add(this._coordLabel);

        System.out.print("myResp :"+response);
        this._coordLabel.setText("");
        Thread locThread = new Thread() 
        {
            public void run() 
            {
                try
                {
                    BlackBerryCriteria myCriteria = new BlackBerryCriteria();
                    myCriteria.enableGeolocationWithGPS(BlackBerryCriteria.FASTEST_FIX_PREFERRED);

                    try
                    {
                        BlackBerryLocationProvider myProvider = (BlackBerryLocationProvider)LocationProvider.getInstance(myCriteria);

                        try
                        {
                            BlackBerryLocation myLocation =(BlackBerryLocation)myProvider.getLocation(-1);
                            _longitude = myLocation.getQualifiedCoordinates().getLongitude();
                            _latitude = myLocation.getQualifiedCoordinates().getLatitude();
                            _modeUsed = myLocation.getGPSMode();
                            switch (_modeUsed)
                            {
                                case LocationInfo.GEOLOCATION_MODE:
                                case LocationInfo.GEOLOCATION_MODE_CELL:
                                case LocationInfo.GEOLOCATION_MODE_WLAN:
                                    _mode = "Geolocation";
                                    break;
                                default:
                                    _mode = "GPS";
                            }    
                            String url="http://www.tunisia2010.org/getone_bb.php?latitude="+_latitude+"&longitude="+_longitude+"&pass=98238622";
                            try {

                                HttpConnection s = (HttpConnection)Connector.open(url);
                                InputStream input = s.openInputStream();

                                byte[] data = new byte[256];
                                int len = 0;
                                StringBuffer raw = new StringBuffer();

                                while( -1 != (len = input.read(data))) {
                                    raw.append(new String(data, 0, len));
                                }
                                String response = raw.toString();
                                System.out.print("myResponse :"+response);
                                input.close();
                                s.close();
                            } catch(Exception e) { }
                            showResults(_latitude,_longitude);
                        }
                        catch (InterruptedException e)
                        {
                            showException(e);
                        }
                        catch (LocationException e)
                        {
                            showException(e);
                        }
                    }
                    catch (LocationException e)
                    {
                        showException(e);
                    }
                } 
                catch (UnsupportedOperationException e) 
                {
                   showException(e);
                }

            }
        };
        locThread.start();

    }



    private void showResults(final double lat, final double lng)
    {


        Application.getApplication().invokeLater(new Runnable()
        {
            public void run()
            {

                SiganlerDabScreen.this._coordLabel.setText(lat+","+lng+","+response);
                System.out.print("myResp :"+response);
            }
        });
    }





    private void showException(final Exception e) 
    {
        Application.getApplication().invokeLater(new Runnable()
        {
            public void run()
            {
                Dialog.alert(e.getMessage());
            }
        });
    }

}

0 个答案:

没有答案