使用OkHttp3 WebSocket和Retrofit来连续跟踪android设备的位置

时间:2020-04-28 02:32:33

标签: java android websocket retrofit2 okhttp

我有以下Java代码,我想在android应用程序中使用该代码查询api,以获取正在运行客户端应用程序的设备的持续经纬度变化,我想跟踪该设备。我相信我尝试使用的WebSocketCall方法已被弃用。据我所知,我尝试使用webSocket调用来创建改造客户端并将WebSocketListner中的数据排队入改造的方式存在问题。我研究了几个WebSocketListener示例,总计n00b,但我一直无法弄清楚代码。我的想法是通过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;
            }
        });
    }

1 个答案:

答案 0 :(得分:1)

因此,由于我的总成绩为n00b,因此花了一些时间和很多问题来解决这个问题。也许将来会对别人有帮助。

userCodeAppPanel:55 Uncaught TypeError: view.setEnableDrives is not a function
    at showSheetPicker (userCodeAppPanel:55)
    at df (1551822985-mae_html_user_bin_i18n_mae_html_user__en_gb.js:72)
    at 1551822985-mae_html_user_bin_i18n_mae_html_user__en_gb.js:15
    at qg.J (1551822985-mae_html_user_bin_i18n_mae_html_user__en_gb.js:99)
    at Kd (1551822985-mae_html_user_bin_i18n_mae_html_user__en_gb.js:47)
    at Gd (1551822985-mae_html_user_bin_i18n_mae_html_user__en_gb.js:48)
    at Ed.b (1551822985-mae_html_user_bin_i18n_mae_html_user__en_gb.js:44)
相关问题