2014-01-21 47 views
0

我想發送GPS座標到服務器得到我的應用程序,它顯示GPS座標正確,但無法將其發送到服務器,問題是在我的MainActivity.java 這裏是我的代碼無法發送GPS座標到服務器

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

也許你應該檢查LogCat stacktrace? – hendrix

+0

問題是,您沒有執行asyncTask(LoadServerASYNC)。 –

+0

@amitsingh你能告訴我,怎麼做? –

回答

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法();

+0

我跟着你的帖子,但仍然沒有得到任何服務器端的迴應。我需要服務器返回字符串 –

+0

你應該使用日誌 – pozuelog

相關問題