socket.io和okhttp3冲突

时间:2017-02-07 13:41:32

标签: java android socket.io okhttp okhttp3

我在Android项目中使用okhttp3进行网络连接以及io.socket:socket.io-client:0.8.3用于socket.io连接。

以下是我的gradle文件的摘录。

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    compile group: 'com.google.code.gson', name: 'gson', version: '2.4'
    compile('com.digits.sdk.android:digits:2.0.5@aar') {
        transitive = true;
    }
    compile 'com.android.support:appcompat-v7:24.2.1'
    compile 'com.android.support:support-v4:24.2.1'
    compile 'com.android.support:cardview-v7:24.2.1'
    compile 'com.android.support:recyclerview-v7:24.2.1'
    compile 'com.squareup.okhttp3:okhttp:3.6.0'
    compile 'com.squareup.okhttp3:okhttp-ws:3.4.2'
    testCompile 'junit:junit:4.12'
    compile('io.socket:socket.io-client:0.8.3') {
        exclude group: 'org.json', module: 'json'
    }
}

但是,当我运行项目时,抛出以下异常:

java.lang.NoSuchMethodError: No direct method <init>(Lokhttp3/Request;Lokhttp3/WebSocketListener;Ljava/util/Random;)V in class Lokhttp3/internal/ws/RealWebSocket; or its super classes (declaration of 'okhttp3.internal.ws.RealWebSocket' appears in /data/data/com.fouadkada.mysioapplicationpoc/files/instant-run/dex/slice-okhttp-ws-3.4.2_208c84938ec045e30eddb95384859b3e594c4d81-classes.dex)
                   at okhttp3.OkHttpClient.newWebSocket(OkHttpClient.java:414)
                   at io.socket.engineio.client.transports.WebSocket.doOpen(WebSocket.java:78)
                   at io.socket.engineio.client.Transport$1.run(Transport.java:82)
                   at io.socket.thread.EventThread.exec(EventThread.java:55)
                   at io.socket.engineio.client.Transport.open(Transport.java:77)
                   at io.socket.engineio.client.Socket.probe(Socket.java:472)
                   at io.socket.engineio.client.Socket.onOpen(Socket.java:485)
                   at io.socket.engineio.client.Socket.onHandshake(Socket.java:526)
                   at io.socket.engineio.client.Socket.onPacket(Socket.java:499)
                   at io.socket.engineio.client.Socket.access$1000(Socket.java:31)
                   at io.socket.engineio.client.Socket$5.call(Socket.java:313)
                   at io.socket.emitter.Emitter.emit(Emitter.java:117)
                   at io.socket.engineio.client.Transport.onPacket(Transport.java:134)
                   at io.socket.engineio.client.transports.Polling.access$700(Polling.java:17)
                   at io.socket.engineio.client.transports.Polling$2.call(Polling.java:124)
                   at io.socket.engineio.parser.Parser.decodePayload(Parser.java:251)
                   at io.socket.engineio.client.transports.Polling._onData(Polling.java:134)
                   at io.socket.engineio.client.transports.Polling.onData(Polling.java:106)
                   at io.socket.engineio.client.transports.PollingXHR$5$1.run(PollingXHR.java:111)
                   at io.socket.thread.EventThread$2.run(EventThread.java:80)
                   at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
                   at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
                   at java.lang.Thread.run(Thread.java:818)

我尝试过这里建议的解决方案:https://github.com/socketio/socket.io-client-java/issues/337但没有成功

有什么想法吗?

2 个答案:

答案 0 :(得分:1)

看起来Socket IO不支持OkHttp 3.5.0中的新websockets API launched

您需要向他们发送拉取请求以更新其实现以使用OkHttp的新websockets API。或者您可以将自己的代码回滚到OkHttp 3.4.1。

答案 1 :(得分:1)

在socket.io-client-java的github页面上的问题部分发布后,我得到了一个解决问题的答案。

解决方案是删除对okhttp-ws的依赖:3.4.2所以依赖块变为如下:

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    compile group: 'com.google.code.gson', name: 'gson', version: '2.4'
    compile('com.digits.sdk.android:digits:2.0.5@aar') {
        transitive = true;
    }
    compile 'com.android.support:appcompat-v7:24.2.1'
    compile 'com.android.support:support-v4:24.2.1'
    compile 'com.android.support:cardview-v7:24.2.1'
    compile 'com.android.support:recyclerview-v7:24.2.1'
    compile 'com.squareup.okhttp3:okhttp:3.6.0'
    testCompile 'junit:junit:4.12'
    compile('io.socket:socket.io-client:0.8.3') {
        exclude group: 'org.json', module: 'json'
    }
}