尝试 Socket.io 时出现 MalformedURLException
我的代码连接到 SocketIO 服务器,这是它的标准代码。
这是我的代码:
import java.net.MalformedURLException;
import org.json.*;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import io.socket.*;
@SuppressWarnings("unused")
public class HostActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_host);
SocketIO socket = new SocketIO("http://MYSOCKETSERVER/");
socket.connect(new IOCallback() {
@Override
public void onMessage(JSONObject json, IOAcknowledge ack) {
try {
System.out.println("Server said:" + json.toString(2));
} catch (JSONException e) {
e.printStackTrace();
}
}
@Override
public void onMessage(String data, IOAcknowledge ack) {
System.out.println("Server said: " + data);
}
@Override
public void onError(SocketIOException socketIOException) {
System.out.println("an Error occured");
socketIOException.printStackTrace();
}
@Override
public void onDisconnect() {
System.out.println("Connection terminated.");
}
@Override
public void onConnect() {
System.out.println("Connection established");
}
@Override
public void on(String event, IOAcknowledge ack, Object... args) {
System.out.println("Server triggered event '" + event + "'");
}
});
// This line is cached until the connection is establisched.
socket.send("Hello Server!");
}
它显示在这一行:
SocketIO socket = new SocketIO("http://MYSOCKETSERVER/");
我认为这个 url 看起来很有效,但是为什么它会给出这个错误以及如何解决它?谢谢