Socket.IO 错误尝试在空对象引用上调用虚拟方法 'java.lang.String okhttp3.MediaType.toString()'
Socket.IO error Attempt to invoke virtual method 'java.lang.String okhttp3.MediaType.toString()' on a null object reference
我尝试将我的 android 应用程序连接到 nodeJs 服务器,但它无法正常工作...我收到错误 Attempt to invoke virtual method 'java.lang.String okhttp3.MediaType.toString()' on a null object reference
这是我的 nodeJs 服务器代码
var io = require('socket.io').listen(8080),
dns = require('dns'),
express = require('express'),
mysql = require('mysql'),
interval = 2000,
google = 'www.google.com';
var connect = mysql.createClient({
host : 'localhost',
user : 'root',
password : '',
database : 'database'
});
io.sockets.on('connection', function (socket) {
console.log(socket);
})
这是我的 Java 代码...
//importing socket.io dependencies
import io.socket.client.IO;
import io.socket.client.Socket;
import io.socket.emitter.Emitter;
//initializing socket variable
static Socket socket;
try {
socket = IO.socket(socketUrl);
socket.on(Socket.EVENT_CONNECT, new Emitter.Listener() {
@Override
public void call(Object... args) {
runOnUiThread(new Runnable() {
@Override
public void run() {
socket.emit("connected", userId);
}
});
}
});
socket.connect();
} catch (URISyntaxException e) {
e.printStackTrace();
}
这是我的日志
E/AndroidRuntime: FATAL EXCEPTION: OkHttp Dispatcher
Process: com.pixtanta.android, PID: 26214
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String okhttp3.MediaType.toString()' on a null object reference
at io.socket.engineio.client.transports.PollingXHR$Request.onLoad(PollingXHR.java:271)
at io.socket.engineio.client.transports.PollingXHR$Request.access0(PollingXHR.java:148)
at io.socket.engineio.client.transports.PollingXHR$Request.onResponse(PollingXHR.java:232)
at okhttp3.internal.connection.RealCall$AsyncCall.run(RealCall.kt:519)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1133)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:607)
at java.lang.Thread.run(Thread.java:761)
我还是 Android Studio 的新手,Java...有人知道如何解决这个问题吗?
我尝试将我的 android 应用程序连接到 nodeJs 服务器,但它无法正常工作...我收到错误 Attempt to invoke virtual method 'java.lang.String okhttp3.MediaType.toString()' on a null object reference
这是我的 nodeJs 服务器代码
var io = require('socket.io').listen(8080),
dns = require('dns'),
express = require('express'),
mysql = require('mysql'),
interval = 2000,
google = 'www.google.com';
var connect = mysql.createClient({
host : 'localhost',
user : 'root',
password : '',
database : 'database'
});
io.sockets.on('connection', function (socket) {
console.log(socket);
})
这是我的 Java 代码...
//importing socket.io dependencies
import io.socket.client.IO;
import io.socket.client.Socket;
import io.socket.emitter.Emitter;
//initializing socket variable
static Socket socket;
try {
socket = IO.socket(socketUrl);
socket.on(Socket.EVENT_CONNECT, new Emitter.Listener() {
@Override
public void call(Object... args) {
runOnUiThread(new Runnable() {
@Override
public void run() {
socket.emit("connected", userId);
}
});
}
});
socket.connect();
} catch (URISyntaxException e) {
e.printStackTrace();
}
这是我的日志
E/AndroidRuntime: FATAL EXCEPTION: OkHttp Dispatcher
Process: com.pixtanta.android, PID: 26214
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String okhttp3.MediaType.toString()' on a null object reference
at io.socket.engineio.client.transports.PollingXHR$Request.onLoad(PollingXHR.java:271)
at io.socket.engineio.client.transports.PollingXHR$Request.access0(PollingXHR.java:148)
at io.socket.engineio.client.transports.PollingXHR$Request.onResponse(PollingXHR.java:232)
at okhttp3.internal.connection.RealCall$AsyncCall.run(RealCall.kt:519)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1133)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:607)
at java.lang.Thread.run(Thread.java:761)
我还是 Android Studio 的新手,Java...有人知道如何解决这个问题吗?