I am developing a chat for service for my application and have been trying to connect to socket.io from my native android application. I am using :-
my activity class looks like
private Socket socket;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
this.setContentView(R.layout.activity_main);
message = (EditText) findViewById(R.id.editText1);
send = (Button) findViewById(R.id.button1);
try {
socket = IO.socket("http://ridewithme.in:49154/socket");
} catch (URISyntaxException e) {
e.printStackTrace();
}
send.setOnClickListener(this);
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(v.getId() == R.id.button1) {
text = message.getText().toString();
socket.connect();
socket.send(text);
socket.disconnect();
}
}
but i keep on getting XHR polling error (logs from the Socket.emit())
com.github.nkzawa.engineio.client.EngineIOException: xhr poll error
com.github.nkzawa.socketio.client.SocketIOException: Connection error
My server uses socket.io v1.0.X and it seems to be working fine from a browser. What is possibly going wrong here? Is it an acces origin problem?
You can't access to Internet from main thread. Try this:
new Thread(new Runnable() {
@Override
public void run() {
text = message.getText().toString();
socket.connect();
socket.send(text);
socket.disconnect();
}
}).start();