LaVOZs

The World’s Largest Online Community for Developers

'; java - Using okhttp3 sockets and retrofit - LavOzs.Com

I am working on a project to track a devices movements. The clients lat/lng location is uploaded to an API and I'm using retrofit query the API to display the location of the client device on the management device. Because lat/lng will be constantly changing, I would like to use a persistent socket to get the data from the API instead of creating a new session for every query. I am getting a duplicate class error and not sure why. Any help would be greatly appreciated.

Gradle Dependencies:

dependencies {
    implementation 'androidx.multidex:multidex:2.0.1'
    implementation 'androidx.appcompat:appcompat:1.1.0'
    implementation 'androidx.appcompat:appcompat-resources:1.1.0'
    implementation 'com.google.android.material:material:1.1.0'
    implementation 'com.google.android.gms:play-services-maps:17.0.0'
    implementation 'com.squareup.okhttp3:okhttp:4.4.1'
    implementation 'com.squareup.okhttp3:okhttp-ws:3.4.2'
    implementation 'com.squareup.okhttp3:okhttp-urlconnection:4.4.1'
    implementation 'com.squareup.retrofit2:retrofit:2.8.1'
    implementation 'com.squareup.retrofit2:converter-jackson:2.8.1'
    testImplementation 'junit:junit:4.13'
    testImplementation 'org.robolectric:robolectric:4.3.1'
}

Method to create socket and query data:

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 onPong(Buffer payload) {
                            }

                            @Override
                            public void onClose(int code, String reason) {
                                reconnectWebSocket();
                            }
                        });
                    }
                });
            }

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

Compiler error:

Duplicate class okhttp3.internal.ws.RealWebSocket found in modules jetified-okhttp-4.4.1.jar (com.squareup.okhttp3:okhttp:4.4.1) and jetified-okhttp-ws-3.4.2.jar (com.squareup.okhttp3:okhttp-ws:3.4.2)
Duplicate class okhttp3.internal.ws.WebSocketProtocol found in modules jetified-okhttp-4.4.1.jar (com.squareup.okhttp3:okhttp:4.4.1) and jetified-okhttp-ws-3.4.2.jar (com.squareup.okhttp3:okhttp-ws:3.4.2)
Duplicate class okhttp3.internal.ws.WebSocketReader found in modules jetified-okhttp-4.4.1.jar (com.squareup.okhttp3:okhttp:4.4.1) and jetified-okhttp-ws-3.4.2.jar (com.squareup.okhttp3:okhttp-ws:3.4.2)
Duplicate class okhttp3.internal.ws.WebSocketReader$FrameCallback found in modules jetified-okhttp-4.4.1.jar (com.squareup.okhttp3:okhttp:4.4.1) and jetified-okhttp-ws-3.4.2.jar (com.squareup.okhttp3:okhttp-ws:3.4.2)
Duplicate class okhttp3.internal.ws.WebSocketWriter found in modules jetified-okhttp-4.4.1.jar (com.squareup.okhttp3:okhttp:4.4.1) and jetified-okhttp-ws-3.4.2.jar (com.squareup.okhttp3:okhttp-ws:3.4.2)
Duplicate class okhttp3.internal.ws.WebSocketWriter$FrameSink found in modules jetified-okhttp-4.4.1.jar (com.squareup.okhttp3:okhttp:4.4.1) and jetified-okhttp-ws-3.4.2.jar (com.squareup.okhttp3:okhttp-ws:3.4.2)
Related
Implementing OAuth2 with AccountManager, Retrofit and Dagger
Session Cookie Not Persistant in Retrofit Android
Logging with Retrofit 2
Duplicate entry: okhttp3/internal/ws/RealWebSocket$1.class
Duplicate zip entry [okhttp-3.10.0.jar:okhttp3/HttpUrl$Builder$ParseResult.class]
Android upload gif file to .net web API got an internal server error