Java 为什么android应用程序在socket.connect()使用'时崩溃;nkzawa:socket.io客户端:0.6.0';?
我正在使用标准客户端代码向服务器发送字符串。我正在将Socket.io与com.github.nkzawa:Socket.io client:0.6.0一起使用,当Socket与服务器成功连接时,我的应用程序崩溃。我正在解决3天前的错误,但没有找到任何解决方案。而且,没有任何一行或一个图书馆提及会引起问题。请帮帮我 这是一个错误Java 为什么android应用程序在socket.connect()使用'时崩溃;nkzawa:socket.io客户端:0.6.0';?,java,android,websocket,socket.io,okhttp,Java,Android,Websocket,Socket.io,Okhttp,我正在使用标准客户端代码向服务器发送字符串。我正在将Socket.io与com.github.nkzawa:Socket.io client:0.6.0一起使用,当Socket与服务器成功连接时,我的应用程序崩溃。我正在解决3天前的错误,但没有找到任何解决方案。而且,没有任何一行或一个图书馆提及会引起问题。请帮帮我 这是一个错误 E/AndroidRuntime: FATAL EXCEPTION: OkHttp Dispatcher Process: com.example.leader
E/AndroidRuntime: FATAL EXCEPTION: OkHttp Dispatcher
Process: com.example.leader, PID: 20246
java.lang.NoSuchMethodError: No virtual method callEngineGetConnection(Lcom/squareup/okhttp/Call;)Lcom/squareup/okhttp/Connection; in class
Lcom/squareup/okhttp/internal/Internal; or its super classes (declaration of 'com.squareup.okhttp.internal.Internal' appears in /data/app/com.example.leader-gVzHDO6ESuVvW77d1HOUUw==/base.apk:classes3.dex)
at com.squareup.okhttp.ws.WebSocketCall.createWebSocket(WebSocketCall.java:154)
at com.squareup.okhttp.ws.WebSocketCall.access$000(WebSocketCall.java:42)
at com.squareup.okhttp.ws.WebSocketCall$1.onResponse(WebSocketCall.java:102)
at com.squareup.okhttp.Call$AsyncCall.execute(Call.java:177)
at com.squareup.okhttp.internal.NamedRunnable.run(NamedRunnable.java:33)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1162)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:636)
at java.lang.Thread.run(Thread.java:764)
这是我的密码
private Socket mSocket;
{
try {
mSocket = IO.socket("http://13.251.168.7:4000/");
} catch (URISyntaxException e) {
Toast.makeText(this, e.getMessage().toString(), Toast.LENGTH_SHORT).show();
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mSocket.connect();
mSocket.emit("authenticationCon", "hello");
mSocket.on("unauthorization", onNewMessage);
}
private Emitter.Listener onNewMessage = new Emitter.Listener() {
@Override
public void call(final Object... args) {
runOnUiThread(new Runnable() {
@Override
public void run() {
JSONObject data = (JSONObject) args[0];
Toast.makeText(MainActivity.this, "response : " + data.toString(), Toast.LENGTH_SHORT).show();
}
});
}
};
我的项目有build.gradle
apply plugin: 'com.android.application'
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
repositories {
google()
jcenter()
maven { url 'https://jitpack.io' }
}
dependencies {
classpath 'com.android.tools.build:gradle:3.2.1'
classpath 'com.google.gms:google-services:4.2.0'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
google()
jcenter()
maven { url 'https://jitpack.io' }
maven { url 'https://maven.fabric.io/public' }
}
}
android {
compileSdkVersion 28
// Configure only for each module that uses Java 8
// language features (either in its source code or
// through dependencies).
compileOptions {
sourceCompatibility = 1.8
targetCompatibility = 1.8
}
defaultConfig {
applicationId "com.example.leader"
minSdkVersion 24
targetSdkVersion 28
versionCode 1
versionName "1.0"
multiDexEnabled true
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
dexOptions {
javaMaxHeapSize "4G"
}
useLibrary 'org.apache.http.legacy'
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:appcompat-v7:28.0.0'
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
testImplementation 'junit:junit:4.12'
implementation 'com.android.support:multidex:1.0.3'
implementation 'com.android.support:support-v4:28.0.0'
implementation 'com.android.support:design:28.0.0'
implementation 'com.android.support:recyclerview-v7:28.0.0'
implementation 'com.android.support:cardview-v7:28.0.0'
//noinspection GradleCompatible
implementation 'com.google.maps.android:android-maps-utils:0.5+'
implementation 'com.google.android.gms:play-services-gcm:16.1.0'
implementation 'com.google.android.gms:play-services-maps:16.1.0'
implementation 'com.google.android.gms:play-services-location:16.0.0'
implementation 'com.google.android.gms:play-services-places:16.1.0'
implementation 'com.google.android.gms:play-services-auth:16.0.1'
implementation 'com.google.firebase:firebase-database:16.0.1'
implementation 'com.google.firebase:firebase-auth:16.0.3'
implementation 'com.google.firebase:firebase-core:16.0.3'
// (Recommended) Add Analytics
implementation 'com.google.firebase:firebase-analytics:16.0.3'
// Add dependency
implementation 'com.crashlytics.sdk.android:crashlytics:2.10.1'
implementation 'com.google.firebase:firebase-messaging:17.3.0'
// Stripe
implementation 'dev.dworks.libs:volleyplus:+'
implementation 'com.google.firebase:firebase-storage:15.0.0'
implementation 'com.firebase:geofire-android:2.3.0'
implementation 'com.github.bumptech.glide:glide:4.8.0'
annotationProcessor 'com.github.bumptech.glide:compiler:4.8.0'
implementation 'com.jcminarro:RoundKornerLayouts:0.4.0'
implementation 'com.sothree.slidinguppanel:library:3.4.0'
implementation 'de.hdodenhof:circleimageview:3.0.1'
implementation 'com.mikhaellopez:circularimageview:2.0.1'
implementation 'com.squareup.picasso:picasso:2.71828'
implementation 'com.google.maps:google-maps-services:0.1.20'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
//Socket.io
implementation('com.github.nkzawa:socket.io-client:0.6.0') {
exclude group: 'org.json', module: 'json'
}
implementation 'com.squareup.okhttp3:okhttp:4.4.1'
}
apply plugin: 'com.google.gms.google-services'
com.google.gms.googleservices.GoogleServicesPlugin.config.disableVersionCheck = true
无论您使用什么库,它本身都使用非常古老的OKHTTP2.x版本。让库升级到最新的OkHttp(4.4.1或更新版本)应该会有所帮助。@JesseWilson先生,我想这个库'com.github.nkzawa:socket.io client:0.6.0'正在使用自己的OkHttp,请告诉我如何将其中的OkHttp升级到最新版本?请与项目的维护人员联系!或者叉子!您需要在更改后的web sockets API上添加以下说明:实际上,您只需要升级到最新的socket.io库。那应该容易多了。你找到解决办法了吗?