Christiaan de Die Christiaan de Die - 2 months ago 22
Android Question

Unhandled exception type MalformedURLException

i got an MalformedURLException when trying Socket.io

My code connects to a SocketIO server and this is the standard cod for it yet.

This is my code:

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!");

}


And it shows on this line:

SocketIO socket = new SocketIO("http://MYSOCKETSERVER/");


I think this url looks vaild, But why is it giving that error and how to solve it?
Thanks

Answer

Fixed it by using this:

        try {
    SocketIO socket = new SocketIO("http://SOCKETIOSERVER/");
    } catch (MalformedURLException e1) {
        e1.printStackTrace();
    }