我想創建一個獲取我當前位置(緯度和經度)的android應用程序,並且我試圖通過http post發送數據到mysql數據庫,但是我的php端不支持' t似乎收到我發送的數據,並且不會插入到數據庫中。通過codeigniter從android到mysql發佈數據
我使用本地主機,我試圖通過手機瀏覽器訪問鏈接,它工作正常。
Geolocation.java
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.content_main);
textlat = (TextView) findViewById(R.id.textlat);
textlong = (TextView) findViewById(R.id.textlong);
LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
LocationListener ll = new mylocationlistener();
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, ll);
InputStream is = null;
Location lastLocation = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (lastLocation != null)
{
textlat.setText(Double.toString(lastLocation.getLatitude()));
textlong.setText(Double.toString(lastLocation.getLongitude()));
double pLong1 = lastLocation.getLongitude();
double pLat1 = lastLocation.getLatitude();
String lat = String.valueOf(pLat1);
String lng = String.valueOf(pLong1);
ArrayList<NameValuePair> postParameters = new ArrayList<NameValuePair>();
postParameters.add(new BasicNameValuePair("lat", lat));
postParameters.add(new BasicNameValuePair("lng", lng));
try {
HttpClient client = new DefaultHttpClient();
String postURL = "http://192.168.254.100/capstone/vehiclemonitoring/coordinates";
HttpPost post = new HttpPost(postURL);
UrlEncodedFormEntity ent = new UrlEncodedFormEntity(postParameters);
post.setEntity(ent);
HttpResponse responsePOST = client.execute(post);
HttpEntity resEntity = responsePOST.getEntity();
is = resEntity.getContent();
String msg = "Data entered successfully";
Toast.makeText(getApplicationContext(), msg,Toast.LENGTH_LONG).show();
} catch (Exception e) {
e.printStackTrace();
}
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
class mylocationlistener implements LocationListener{
@Override
public void onLocationChanged(Location location) {
double pLong = location.getLongitude();
double pLat = location.getLatitude();
String lat = String.valueOf(pLat);
String lon = String.valueOf(pLong);
ArrayList<NameValuePair> postParameters = new ArrayList<NameValuePair>();
postParameters.add(new BasicNameValuePair("lat", lat));
postParameters.add(new BasicNameValuePair("lng", lon));
try {
HttpClient client = new DefaultHttpClient();
String postURL = "http://192.168.254.100/capstone/vehiclemonitoring/coordinates";
HttpPost post = new HttpPost(postURL);
UrlEncodedFormEntity ent = new UrlEncodedFormEntity(postParameters, HTTP.UTF_8);
post.setEntity(ent);
HttpResponse responsePOST = client.execute(post);
HttpEntity resEntity = responsePOST.getEntity();
if (resEntity != null) {
Log.i("RESPONSE", EntityUtils.toString(resEntity));
}
} catch (Exception e) {
e.printStackTrace();
}
textlat.setText(Double.toString(pLat));
textlong.setText(Double.toString(pLong));
try {
Thread.sleep(50000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
coordinates.php
function coordinates(){
if(isset($_REQUEST['lat']) && isset($_REQUEST['lng'])){
$lat = $_POST['lat'];
$lng = $_POST['lng'];
echo "OK";
}else{
echo "not ok";
}
$this->vm_model->insertcoordinates($lat,$lng);
`
應該有'return'語句而不是'echo' –
是的,我知道我使用回聲測試目的。我認爲$ _REQUEST數據爲空,因爲它不會進行數據庫插入 – Marc