gpt4 book ai didi

android - 将 Android 客户端用于 socketIO 时出现 SocketIO 异常

转载 作者:太空宇宙 更新时间:2023-11-03 12:36:13 26 4
gpt4 key购买 nike

我正在构建一个 Android 聊天应用程序。我在服务器端使用 nodejs 并尝试实现 android client for socketIO.我收到 SocketIO 异常。代码有什么问题?

服务器

var http = require('http'),fs = require('fs');

var app = http.createServer(function (req, res) {

res.end();
}).listen(8000, '127.0.0.1');

var io = require('socket.io').listen(app);

io.sockets.on('connection', function(socket) {
socket.on('echo', function(data) {
socket.emit('echoback', data);
});
});

客户端

package com.jack.pri;
import java.net.MalformedURLException;
import org.json.JSONException;
import org.json.JSONObject;
import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;
import android.widget.TextView;
import io.socket.*;

public class MainActivity extends Activity {
//private SocketIO socket;
private TextView t;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
t=(TextView) findViewById(R.id.et1);
t.setText("uio");
//////

// System.out.println("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa");
SocketIO socket = null;
try {
socket = new SocketIO("http://10.0.2.2:8000");
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
socket.connect(new IOCallback() {
@Override
public void on(String event, IOAcknowledge ack, Object... args) {
if ("echoback".equals(event) && args.length > 0) {
Log.d("SocketIO", "" + args[0]);
t.setText("s ");
// -> "hello"
}
}

@Override
public void onMessage(JSONObject json, IOAcknowledge ack) {}
@Override
public void onMessage(String data, IOAcknowledge ack) {}
@Override
public void onError(SocketIOException socketIOException) { socketIOException.printStackTrace();}
@Override
public void onDisconnect() {}
@Override
public void onConnect() {}
});
socket.emit("echo", "hello");
///
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}

}

错误日志

09-10 13:49:34.587: W/System.err(1963): io.socket.SocketIOException: Error while handshaking
09-10 13:49:34.587: W/System.err(1963): at io.socket.IOConnection.handshake(IOConnection.java:322)
09-10 13:49:34.597: W/System.err(1963): at io.socket.IOConnection.access$600(IOConnection.java:39)
09-10 13:49:34.597: W/System.err(1963): at io.socket.IOConnection$ConnectThread.run(IOConnection.java:199)
09-10 13:49:34.637: W/System.err(1963): Caused by: java.net.SocketException: socket failed: EACCES (Permission denied)

最佳答案

检查您是否在 Manifest 中放置了 INTERNET PERMISSION。如果不这样做,请输入:

<uses-permission android:name="android.permission.INTERNET"/>

关于android - 将 Android 客户端用于 socketIO 时出现 SocketIO 异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18720814/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com