2017-02-07 144 views
0

我在Android項目中使用okhttp3進行網絡連接,以及io.socket:socket.io-client:socket.io連接的0.8.3。socket.io和okhttp3衝突

以下是我的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:[email protected]') { 
     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但沒有成功

任何想法?

回答

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:[email protected]') { 
     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' 
    } 
} 
1

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

您需要向他們發送一個pull請求來更新他們的實現以使用OkHttp的新websockets API。或者您可以將自己的代碼回滾到OkHttp 3.4.1。