我第一次尝试将我的Android应用程序连接到节点js服务器(在本地系统上),但它没有连接。我在过去的15天里尝试过这个但是没有取得丰硕的成果。 这是我的代码,请查看并向我提供解决方案。
这是连接服务器的主要活动代码
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
try {
SocketIO socket = new SocketIO("My_System_IP:3000/");
socket.connect(new IOCallback() {
@Override
public void onMessage(JSONObject json, IOAcknowledge ack) {
try {
System.out.println("Server said:" + json.toString(2));
// display("Connectet and " + json.toString(2));
} catch (JSONException e) {
e.printStackTrace();
}
}
@Override
public void onMessage(String data, IOAcknowledge ack) {
System.out.println("Server said: " + data);
// display("Connectet and " + data);
}
@Override
public void onError(SocketIOException socketIOException) {
System.out.println("an Error occured");
Log.d("error", "an error occured");
// display("not Connectet == error occured ");
socketIOException.printStackTrace();
}
@Override
public void onDisconnect() {
System.out.println("Connection terminated.");
Log.e("disconnect", "Connection terminated");
// display("disConnectet ");
}
@Override
public void onConnect() {
System.out.println("Connection established");
Log.v("connect", "Connection established");
// display("Connectet and done");
}
@Override
public void on(String event, IOAcknowledge ack, Object... args) {
System.out
.println("Server triggered event '" + event + "'");
Log.v("ON", "Server triggered event '" + event + "'");
// display("Connectet and " + event);
}
});
// This line is cached until the connection is establisched.
socket.send("Hello Server!");
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
这是IOcallback类代码
public interface IOCallback {
/**
* On disconnect. Called when the socket disconnects and there are no further attempts to reconnect
*/
void onDisconnect();
/**
* On connect. Called when the socket becomes ready so it is now able to receive data
*/
void onConnect();
/**
* On message. Called when the server sends String data.
*
* @param data the data.
* @param ack an {@link IOAcknowledge} instance, may be <code>null</code> if there's none
*/
void onMessage(String data, IOAcknowledge ack);
/**
* On message. Called when the server sends JSON data.
*
* @param json JSON object sent by server.
* @param ack an {@link IOAcknowledge} instance, may be <code>null</code> if there's none
*/
void onMessage(JSONObject json, IOAcknowledge ack);
/**
* On [Event]. Called when server emits an event.
*
* @param event Name of the event
* @param ack an {@link IOAcknowledge} instance, may be <code>null</code> if there's none
* @param args Arguments of the event
*/
void on(String event, IOAcknowledge ack, Object... args);
/**
* On error. Called when socket is in an undefined state. No reconnect attempts will be made.
*
* @param socketIOException the last exception describing the error
*/
void onError(SocketIOException socketIOException);
}
此代码的其他代码取自github上提供的this源代码。
请帮我解决这个问题,当我运行这段代码时会给出一个错误 “发生错误” 我在错误here
上附加了快照服务器端nodejs代码是
var http = require('http');
io = require('socket.io');
var app = http.createServer();
app.listen(3000);
console.log('Server running at http://127.0.0.1:3000/');
// Socket.IO server
var io = io.listen(app)
, nicknames = {};
io.sockets.on('connection', function (socket) {
socket.on('user message', function (msg) {
socket.broadcast.emit('user message', {user: socket.nickname, message: msg.message});
});
socket.on('nickname', function (nick, fn) {
nickname = nick.nickname;
if (nicknames[nickname]) {
fn(true);
} else {
fn(false);
nicknames[nickname] = socket.nickname = nickname;
socket.broadcast.emit('announcement', {user: nickname, action: 'connected'});
io.sockets.emit('nicknames', nicknames);
}
});
socket.on('disconnect', function () {
if (!socket.nickname) return;
delete nicknames[socket.nickname];
socket.broadcast.emit('announcement', {user: socket.nickname, action: 'disconected'});
socket.broadcast.emit('nicknames', nicknames);
});
});
提前致谢