我们试图通过使用Traccar和Retrofit2来获得设备位置的实时更新,直到现在,我们只能在应用程序启动时才能获得这个位置(安卓应用程序)。
发布于 2018-03-01 21:03:46
要获得实时更新,您应该使用到Traccar服务器的WebSocket连接。Retrofit不支持任何类似的东西,所以您应该直接使用OkHttp:
Request request = new Request.Builder().url(retrofit.baseUrl().url().toString() + "api/socket").build();
webSocket = WebSocketCall.create(client, request);
webSocket.enqueue(new WebSocketListener() {
@Override
public void onOpen(WebSocket webSocket, Response response) {
}
@Override
public void onFailure(IOException e, Response response) {
reconnectWebSocket();
}
@Override
public void onMessage(ResponseBody message) throws IOException {
final String data = message.string();
handler.post(new Runnable() {
@Override
public void run() {
try {
handleMessage(data);
} catch (IOException e) {
Log.w(MainFragment.class.getSimpleName(), e);
}
}
});
}
@Override
public void onPong(Buffer payload) {
}
@Override
public void onClose(int code, String reason) {
reconnectWebSocket();
}
});有关更多信息,您可以查看官方Traccar应用程序(原生版本):
https://github.com/tananaev/traccar-manager-android/tree/native
发布于 2019-08-08 12:43:28
@Cesar gutierrez我解决了这个问题。使用地理编码的实时定位和显示已知地址
https://stackoverflow.com/questions/49037036
复制相似问题