gpt4 book ai didi

java - 使用 OkHttp3 WebSocket 和 Retrofit 持续跟踪 Android 设备位置

转载 作者:行者123 更新时间:2023-12-01 17:04:51 26 4
gpt4 key购买 nike

我有以下java代码,我想在android应用程序中使用它来查询api以获取运行客户端应用程序的设备的连续纬度/经度变化,我想跟踪该设备。我相信我尝试使用的 WebSocketCall 方法已被弃用。据我所知,我如何尝试使用 webSocket 调用来创建改造客户端并将来自 WebSocketListner 的数据排队到改造中存在问题。我研究了几个 WebSocketListener 示例,但由于我是个菜鸟,我无法弄清楚代码。我的想法是通过 WebSocket 保持对 api 的连接开放,并使用改造处理数据响应。任何帮助将不胜感激。

private WebSocketCall webSocket;

private void createWebSocket() {
final MainApplication application = (MainApplication) getActivity().getApplication();
application.getServiceAsync(new MainApplication.GetServiceCallback() {
@Override
public void onServiceReady(final OkHttpClient client, final Retrofit retrofit, WebService service) {
User user = application.getUser();
map.moveCamera(CameraUpdateFactory.newLatLngZoom(
new LatLng(user.getLatitude(), user.getLongitude()), user.getZoom()));
service.getDevices().enqueue(new WebServiceCallback<List<Device>>(getContext()) {
@Override
public void onSuccess(retrofit2.Response<List<Device>> response) {
for (Device device : response.body()) {
if (device != null) {
devices.put(device.getId(), device);
}
}

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 onClose(int code, String reason) {
reconnectWebSocket();
}
});
}
});
}

@Override
public boolean onFailure() {
return false;
}
});
}

最佳答案

因为我完全是个菜鸟,所以花了一些时间和很多问题来解决这个问题。也许它将来会对其他人有所帮助。

 private WebSocket webSocket;

private void createWebSocket() {
final MainApplication application = (MainApplication) getActivity().getApplication();
application.getServiceAsync(new MainApplication.GetServiceCallback() {
@Override
public void onServiceReady(final OkHttpClient client, final Retrofit retrofit, WebService service) {
User user = application.getUser();
map.moveCamera(CameraUpdateFactory.newLatLngZoom(
new LatLng(user.getLatitude(), user.getLongitude()), user.getZoom()));

service.getDevices().enqueue(new WebServiceCallback<List<Device>>(getContext()) {
@Override
public void onSuccess(retrofit2.Response<List<Device>> response) {
for (Device device : response.body()) {
if (device != null) {
devices.put(device.getId(), device);
}
}

Request request = new Request.Builder().url(retrofit.baseUrl().url().toString() + "api/socket").build();
Log.e("WebSockets", "Headers: " + request.headers().toString());
WebSocketListener webSocketListener = new WebSocketListener() {
private static final int NORMAL_CLOSURE_STATUS = 1000;
@Override
public void onOpen(WebSocket webSocket, Response response) {
webSocket.send("{Auth-Token:secret-api-token-here}");
Log.e("WebSockets", "Connection accepted!");
}

@Override
public void onFailure(@NotNull WebSocket webSocket, @NotNull Throwable t, @Nullable Response response) {
reconnectWebSocket();
}

@Override
public void onMessage(@NotNull WebSocket webSocket, @NotNull String text) {
final String data = text;
Log.e("WebSockets", "Receiving : " + text);
handler.post(new Runnable() {
@Override
public void run() {
try {
handleMessage(data);
} catch (IOException e) {
Log.w(MainFragment.class.getSimpleName(), e);
}
}
});
}
@Override
public void onMessage(WebSocket webSocket, ByteString bytes) {
Log.e("WebSockets", "Receiving bytes : " + bytes.hex());
}

@Override
public void onClosing(WebSocket webSocket, int code, String reason) {
webSocket.close(NORMAL_CLOSURE_STATUS, null);
Log.e("WebSockets", "Closing : " + code + " / " + reason);
}

@Override
public void onClosed(@NotNull WebSocket webSocket, int code, @NotNull String reason) {
reconnectWebSocket();
}
};

webSocket = client.newWebSocket(request, webSocketListener);
}
});
}

@Override
public boolean onFailure() {
return false;
}
});
}

关于java - 使用 OkHttp3 WebSocket 和 Retrofit 持续跟踪 Android 设备位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61471678/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com