如何使用 JSON 定期向 Web 服务器发送两个参数?

发布于 2024-11-02 04:03:09 字数 83 浏览 0 评论 0原文

该应用程序基于 GPS 跟踪,必须定期发送纬度和经度坐标。目前,我们通过 DDMS 手动输入坐标。发送数据一直是一个很大的问题。那么,你们有什么建议吗?

The application is based on GPS tracking where the latitude and longitude co-ordinates have to be sent at regular intervals. For now we are manually entering the co-ordinates through DDMS. Sending the data has been quite a problem. So, do any of you have some suggestions?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

天涯沦落人 2024-11-09 04:03:09

我认为你必须存储某个位置,所以创建ApplicationController,它从android扩展Application类,并将其设置在android清单中,在AC中创建字段Location并在onLocationChanged(Location location)中创建函数,该函数将在AC中保存新位置,而不是按频率创建TimerTask它应该多久从 AC 获取位置并将其发送到您想要的任何地方。

只要应用程序在该类中“活动”,ApplicationController 就会保留所有信息,您还必须创建访问器;)下面的示例代码:

ApplicationController.java

import android.app.Application;
import android.location.Location;

public class ApplicationController extends Application{
    private Location location = null;

    @Override
    public void onCreate() {
        super.onCreate();
    }

    public void setLocation(Location location){
        this.location = location;
    }

    public Location getLocation(){
        return location; 
    }
}

Android 清单中的条目:

    <application 
        android:label="@string/app_name" 
        android:allowClearUserData="false" android:name="core.ApplicationController" 
android:icon="@drawable/driving">

代码中的示例使用:

AC = (ApplicationController) getApplicationContext();

    private void initializeGpsTask(long frequency){
        gpsTask = new TimerTask() {
            @Override
            public void run() {
                Log.i(TAG, "GPS TASK");
                if(AC.getLocation()!=null){
                insertIntoDatabase(transactions.generateRequest(
                                        AC.getLocation()));
                }
            }
        };
        Timer timer = new Timer();
        timer.schedule(gpsTask, frequency, frequency);-
}

这可能会有所帮助 链接

I think you have to store somewhere location so create ApplicationController which extends Application class from android and set it in android manifest, in AC create field Location and in onLocationChanged(Location location) made function which will save new location in AC than made TimerTask with frequency how often it should get location from AC and send it wherever you want.

ApplicationController will keep all information as long as application is "alive" in that class you have to also create accessors ;) sample code below:

ApplicationController.java

import android.app.Application;
import android.location.Location;

public class ApplicationController extends Application{
    private Location location = null;

    @Override
    public void onCreate() {
        super.onCreate();
    }

    public void setLocation(Location location){
        this.location = location;
    }

    public Location getLocation(){
        return location; 
    }
}

Entry in android manifest:

    <application 
        android:label="@string/app_name" 
        android:allowClearUserData="false" android:name="core.ApplicationController" 
android:icon="@drawable/driving">

Sample use in code:

AC = (ApplicationController) getApplicationContext();

    private void initializeGpsTask(long frequency){
        gpsTask = new TimerTask() {
            @Override
            public void run() {
                Log.i(TAG, "GPS TASK");
                if(AC.getLocation()!=null){
                insertIntoDatabase(transactions.generateRequest(
                                        AC.getLocation()));
                }
            }
        };
        Timer timer = new Timer();
        timer.schedule(gpsTask, frequency, frequency);-
}

That might be helpful LINK

卸妝后依然美 2024-11-09 04:03:09

您必须实现 locationListener 类来侦听位置

onLocationChanged(Location location) {

lat = location.getLatitude();
lon = location.getLongitude(0;
}

现在,当您获得纬度和经度时,您必须定期将其发送到服务器。
您可以使用该服务从后台监听位置信息,如果您收到 onLocationChanged 事件,则与您的 Web 服务建立 http 连接并将纬度和经度发布到服务器。

You have to implement locationListener class to listen the location

onLocationChanged(Location location) {

lat = location.getLatitude();
lon = location.getLongitude(0;
}

Now when you get the latitude and longitude you will have to sent it to the server at regular interval.
You can use the service to listen the location information from background and if you got the onLocationChanged event make a http connection to your web service and post the latitude and longitude to the server.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文