0

我正在尝试的是获得我当前的位置(坐标),并且我想在网站上发布这些坐标。我已经使用 WAMP 制作了一个服务器。我已经编写了 php 代码和 java 中的代码。没有错误但没有数据发布在网站上

PHP代码

<?php
echo 'Hello, world!';
$json = $_GET['jsonpost'];//get the post you sent...
$data = json_decode($json); //decode the json formatted string...
print_r($data);
$id = $data->id;
$devid = $data->devid;
$latitude = $data->latitude;
$longitude = $data->longitude;
$service = $data->service;
$con = mysql_connect("","","");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }
mysql_select_db("a5234826_ul", $con);
$devid = $_POST['devid']; 
$latitude = $_POST['latitude'];
$longitude = $_POST['longitude'];
echo "devid" +$devid;
echo "latitude" + $latitude;
echo "longitude" + $longitude; 
$sql = "INSERT INTO  `a5234826_ul`.`locations` (
`id` ,
`devid` ,
`latitude` ,
`longitude` ,
`service`
)
VALUES (
NULL ,  '$devid',  '$latitude',  '$longitude', '$service'  
)";
if (!mysql_query($sql,$con))
  {
  die('Error: ' . mysql_error());
  }
mysql_close($con);
echo json_encode($variable);
?>

编辑 LocationService.java

    @Override
public IBinder onBind(Intent intent) {
    // TODO Auto-generated method stub
    return null;
}

public int onStartCommand(Intent intent, int flags, int startId) {
    PowerManager pm = (PowerManager)getSystemService(Context.POWER_SERVICE);
    wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "lock");
    wl.acquire();
    context = this;
    final String who = intent.getStringExtra("who");
    final LocationManager locMan = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
    final LocationListener listener = new LocationListener(){

        // start location changed

        public void onLocationChanged(Location loc) {
            double latitude = loc.getLatitude();
            double longitude = loc.getLongitude();


            // Create a new HttpClient and Post Header
            HttpClient httpclient = new DefaultHttpClient();
            HttpPost httppost = new HttpPost("http://.../serverFile.php");
            JSONObject json = new JSONObject();


            TelephonyManager telephonyManager = (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);
            String devid = telephonyManager.getDeviceId();

            String postData = "{\"request\":{\"type\":\"locationinfo\"},\"userinfo\":{\"latitude\":\""+latitude+"\",\"longitude\":\""+longitude+"\",\"devid\":\""+devid+"\"}}";




            try {  

                json.put("longitude", longitude);//place each of the strings as you did in postData method
                json.put("latitude", latitude);

                json.put("devid", devid);

                JSONArray postjson=new JSONArray();
                postjson.put(json);
                httppost.setHeader("json",json.toString());
                httppost.getParams().setParameter("jsonpost",postjson);     
                HttpResponse response = httpclient.execute(httppost);

                // for JSON retrieval:
                if(response != null)
                { 
                InputStream is = response.getEntity().getContent();
            BufferedReader reader = new BufferedReader(new InputStreamReader(is));
            StringBuilder sb = new StringBuilder();
            String line = null;
            try {
                while ((line = reader.readLine()) != null) {
                sb.append(line + "\n");
                }
                } catch (IOException e) {
                e.printStackTrace();
                } finally {
                try {
                is.close();
                } catch (IOException e) {
                e.printStackTrace();
                }
                }
                String jsonStr = sb.toString(); //take the string you built place in a string



                JSONObject rec = new JSONObject(jsonStr);
                String longitudecord = rec.getString("lon");
                    String latitudecord = rec.getString("lat");
                // ...
                }
                }catch (ClientProtocolException e) {
                // TODO Auto-generated catch block
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    } catch (JSONException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }




            if (who.equals("me")){
                Intent i = new Intent(context.getPackageName()+".LocationReceived");
                i.putExtra("lat", String.valueOf(latitude));
                i.putExtra("longitude", String.valueOf(longitude));
                i.putExtra("accuracy", String.valueOf(loc.getAccuracy()));
                context.sendBroadcast(i);
                Notification notif = new Notification();
                NotificationManager nm = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
                notif.tickerText = "Location Found!";
                notif.icon = R.drawable.ic_launcher;
                notif.flags = Notification.FLAG_AUTO_CANCEL;
                notif.when = System.currentTimeMillis();
                Intent notificationIntent = new Intent(context, TestLocatorActivity.class);
                notificationIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                notificationIntent.putExtra("lat", String.valueOf(latitude));
                notificationIntent.putExtra("longitude", String.valueOf(longitude));
                notificationIntent.putExtra("accuracy", String.valueOf(loc.getAccuracy()));
                PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0);
                notif.setLatestEventInfo(context, "Location Found!", "Click to open.", contentIntent);
                nm.notify(0, notif);
            } else {
                SmsManager smsMan = SmsManager.getDefault();
                smsMan.sendTextMessage(who, null, "http://maps.google.com/maps?q=loc:"+latitude+","+longitude, null, null);
                smsMan.sendTextMessage(who, null, "Latitude: "+latitude+"\nLongitude: "+longitude, null, null);
            }
            locMan.removeUpdates(this);
            try {
                wl.release();
            } catch (Exception e){
                e.printStackTrace();
            }
            stopSelf();
        }

        public void onProviderDisabled(String provider){


        }

        public void onProviderEnabled(String provider) {
            //Log.i(tag, "GPS IS ON");
        }

        public void onStatusChanged(String provider, int status, Bundle extras){
            switch(status) {
                case LocationProvider.OUT_OF_SERVICE:
                case LocationProvider.TEMPORARILY_UNAVAILABLE:
                case LocationProvider.AVAILABLE:
                    break;
            }
        } };


    locMan.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, listener);
    locMan.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, listener);

    return 2;
}

}
4

3 回答 3

0

就像第一个猜测一样,第 3 行不应该是:

$json = $_POST['jsonpost'];//get the post you sent...
于 2012-06-08T14:03:30.387 回答
0

如果将第 3 行更改为:

$json = $_REQUEST['jsonpost'];

您将通过 HTTP 支持 POST 和 GET 方法。如果您在开发环境中工作以确保请求在没有 HTTP 请求细节的情况下发送到您的服务器,这将很有用。

应谨慎使用 $_REQUEST,因为该变量将包含 $_COOKIES、$_GET 和 $_POST 的内容。

在脚本中访问数据后,您应该始终对其进行清理

于 2012-06-08T14:15:53.990 回答
0

他们与服务器有任何连接吗?

您是否检查了 /etc/httpd/logs/.. 或您的分发记录 http 流量的任何位置?我会首先这样做以确定流量实际上是从设备发送的。

如果成功,是否会填充数据库?您还可以简单地添加语句以将 php 代码跟踪到文件(例如 /tmp)并跟踪该文件以查看错误是否存在于代码中,或者只是遵循代码执行模式,即使在这种简单的情况下也是如此。

你正在做一个死('无法连接:'。mysql_error()); 和 print_r,以及写 hello world,你实际上在哪里查看这个!?正如我提到的,将其写入 /tmp 文件进行分析。

当然,我正在假设您的服务运行正常。日志猫说什么?他们的任何可辨别的输出是否表明存在问题?

php 端的代码本身看起来有些仓促,在这里并不太关键,但这里的一些纪律可能会在将来为您节省很多问题。echo json_encode($variable); 是什么?在代码的末尾做什么?

于 2012-06-08T23:00:34.790 回答