如何在查询字符串中发送我的GPS位置(纬度,经度)?

mej*_*l57 5 gps android query-string

我需要为Android制作一个简单的应用程序,每隔25秒将我的智能手机的位置发送到Web应用程序.我的网络应用程序在线,现在我可以手动传递值,如下所示:

http://mywebapp.com/coordinates/create?latitude=18.463108&longitude=-69.929117

我是Android开发的绝对初学者,所以试着一步一步解释我.

mej*_*l57 8

经过深入研究后,我发现我认为最简单的方法是在查询字符串中发送GPS位置(纬度,经度).也许网上没有比此更短的代码.所以我们将分2步完成:

  1. 首先将此权限添加到 AndroidManifest.xml

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    
    Run Code Online (Sandbox Code Playgroud)
  2. 现在你MainActivity.java的类里面的代码public class MainActivity extends Activity { }

    public class MainActivity extends Activity {
    
        public double latitude;
        public double longitude;
    
        @Override
        protected 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);
        }
    
        /* Class My Location Listener */
    
        public class MyLocationListener implements LocationListener {
    
            @Override
            public void onLocationChanged(Location loc) {
                latitude = loc.getLatitude();
                longitude = loc.getLongitude();
                String Text = "My current Latitude = " + latitude  + " Longitude = " + longitude;
                Toast.makeText( getApplicationContext(),Text,Toast.LENGTH_SHORT).show();
    
                SendQueryString(); // for send the  Query String of latitude and logintude to the webapp.
            }
    
    
    
            @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) {
            }
    
        }/* End of Class MyLocationListener */
    
    
        public void SendQueryString() {
            new Thread() {  
                public void run() {
    
                    String url = "http://mywebapp.com/coordinates/create?latitude=" + latitude +"&longitude=" + longitude;
    
                    try {
                        HttpClient Client = new DefaultHttpClient();
                        HttpGet httpget = new HttpGet(url);
                        Client.execute(httpget);
                    }
                    catch(Exception ex) {
                        String fail = "Fail!";
                        Toast.makeText( getApplicationContext(),fail,Toast.LENGTH_SHORT).show();
                    }
                }
            }.start();
        }
    }
    
    Run Code Online (Sandbox Code Playgroud)

注意:正如您所看到我new Thread() { public void run() {/*code here*/}}.start();在函数中使用SendQueryString(),那是因为我们正在尝试向服务器发出请求,因此在Android中您无法向主体线程中的服务器发出请求,其中图形和主要过程是.实际上,如果您尝试在主线程中发出请求,它将返回错误.