【问题标题】:Display Latitude and longitude on a webserver在网络服务器上显示纬度和经度
【发布时间】:2012-04-27 19:11:51
【问题描述】:

几天以来,我一直在尝试显示与 Web 服务器协调的 gps,但尚未得出任何结论。 我想做的是获取一个人的纬度和经度,然后将它们显示在网络服务器上。我有一个 php 服务器和 mysql 数据库。 没有错误但url上没有显示任何内容

public class UseGps extends Activity 

{ 


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

@Override 

public void onCreate(Bundle savedInstanceState) 

{ 

super.onCreate(savedInstanceState); 

setContentView(R.layout.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 

{ 
 Context context;
 public void onLocationChanged(Location loc) 

{ 


double latitude = loc.getLatitude(); 

double longitude = loc.getLongitude(); 
Toast.makeText(context, "Latitude:" +latitude + "Longitude:" +longitude, 5000).show();
TelephonyManager telephonyManager =    (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);
String Devid = telephonyManager.getDeviceId();




//this is JSON part to put your information inside it
String postData = "{\"request\":{\"type\":\"locationinfo\"},\"userinfo\":{\"Devid\":\""+Devid+"\",\"latitude\":\""+latitude+"\",\"longitude\":\""+longitude+"\"}}";

Uri uri = Uri.parse("http://location.site88.net/store.php"); 
Intent intent = new Intent(Intent.ACTION_VIEW, uri); 
startActivity(intent); 
String url="http://location.site88.net/storeg.php"; 

DefaultHttpClient httpClient = new DefaultHttpClient(); 
HttpPost httpPost = new HttpPost(url); 

List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(3);
nameValuePairs.add(new BasicNameValuePair("Devid", Devid));
nameValuePairs.add(new BasicNameValuePair("latitude", Double.toString(latitude) )); 
nameValuePairs.add(new BasicNameValuePair("longitude", Double.toString(longitude))); 

try {
    httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
} catch (UnsupportedEncodingException e1) {
    // TODO Auto-generated catch block
    e1.printStackTrace();
} 
HttpResponse httpResponse = null;
String result = null;
try {
    httpResponse = httpClient.execute(httpPost);
    HttpEntity httpEntity = httpResponse.getEntity();
    if (httpEntity != null) {
        InputStream instream = null;
        instream = httpEntity.getContent();
        BufferedReader reader = new BufferedReader(new InputStreamReader(instream,"iso-8859-1"),8); 
        StringBuilder sb = new StringBuilder(); 
        String line = null; 
        while ((line = reader.readLine()) != null) { 
            sb.append(line + "\n"); 
            Toast.makeText(getApplicationContext(), "pass", Toast.LENGTH_SHORT).show(); 
        } 
         instream.close();
              result=sb.toString(); 
      }
} catch (ClientProtocolException e1) {
    // TODO Auto-generated catch block
    e1.printStackTrace();
} catch (IOException e1) {
    // TODO Auto-generated catch block
    e1.printStackTrace();
} 
}
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub

}
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub

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

  }}}

【问题讨论】:

    标签: android web-services http latitude-longitude


    【解决方案1】:

    使用

    nameValuePairs.add(new BasicNameValuePair("latitude", Double.toString(latitude) )); 
    nameValuePairs.add(new BasicNameValuePair("longitude", Double.toString(longitude))); 
    nameValuePairs.add(new BasicNameValuePair("devid", devid)); 
    try {
        httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
    } catch (UnsupportedEncodingException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    } 
    HttpResponse httpResponse = null;
    try {
        httpResponse = httpClient.execute(httpPost);
        HttpEntity httpEntity = httpResponse.getEntity();
        if (httpEntity != null) {
            InputStream instream = null;
            instream = httpEntity.getContent();
            BufferedReader reader = new BufferedReader(new InputStreamReader(instream,"iso-8859-1"),8); 
            StringBuilder sb = new StringBuilder(); 
            String line = null; 
            while ((line = reader.readLine()) != null) { 
                sb.append(line + "\n"); 
                Toast.makeText(getApplicationContext(), "pass", Toast.LENGTH_SHORT).show(); 
            } 
             instream.close();
                  result=sb.toString(); 
          }
    } catch (ClientProtocolException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    } catch (IOException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    } 
    

    而不是

    HttpResponse Response = httpClient.execute(httpPost); 
    InputStream httpEntity = httpResponse.getEntity().getContent(); 
    

    【讨论】:

    • 有没有其他方法可以在网上发帖..?
    • 这是在服务器上发布数据的正确方法,但为什么你要在服务器上发布两次数据?
    • Richard 我回答了你关于为什么The error says httpResponse cannot be resolved 的问题,所以如果你有任何其他问题,请提出新问题
    【解决方案2】:

    使用这个在真实设备上工作。

    public class GpslocationActivity extends Activity implements LocationListener{
    
    private TextView latituteField;
    private TextView longitudeField;
    private LocationManager locationManager;
    private String provider;
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        latituteField = (TextView) findViewById(R.id.TextViewLatitudereply);
        longitudeField = (TextView) findViewById(R.id.TextViewLongitutereply);
    
        // Get the location manager
        locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
    
        Criteria criteria = new Criteria();
        provider = locationManager.getBestProvider(criteria, false);
        Location location = locationManager.getLastKnownLocation(provider);
    
    
        if (location != null) {
            System.out.println("Provider " + provider + " has been selected.");
            int lat = (int) (location.getLatitude());
            int lng = (int) (location.getLongitude());
            latituteField.setText(String.valueOf(lat));
            longitudeField.setText(String.valueOf(lng));
        } else {
            latituteField.setText("Provider not available");
            longitudeField.setText("Provider 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) {
        int lat = (int) (location.getLatitude());
        int 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();
    }
    }
    

    别忘了给权限

        <uses-permission android:name="android.permission.INTERNET"/>
        <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
        <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/> 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-09-24
      • 1970-01-01
      • 2023-03-29
      • 2021-11-20
      • 1970-01-01
      相关资源
      最近更新 更多