通过codeigniter将数据从android发布到mysql

时间:2016-01-27 05:34:56

标签: php android mysql codeigniter gps

我正在尝试创建一个获取我当前位置(经度和纬度)的Android应用程序,我正在尝试通过http post将数据发送到mysql数据库,但我的php端似乎没有收到我的数据发送并不会插入数据库。

我正在使用localhost,我尝试通过手机浏览器访问该链接并且工作正常。

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

`

1 个答案:

答案 0 :(得分:0)

代码,也检查null

<?php
function coordinates()
{
    if(isset($_REQUEST['lat']) && isset($_REQUEST['lng']) && !empty($_REQUEST['lat']) && !empty($_REQUEST['lng'])) {
        $lat = $_POST['lat'];
        $lng = $_POST['lng'];
        return "OK";
    } else {
        return "not ok";
    }
}

$data = $this->vm_model->insertcoordinates($lat,$lng);
return $data;
?>