user1952629
user1952629

Reputation:

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

Upvotes: 0

Views: 5626

Answers (2)

Arghya
Arghya

Reputation: 59

MalformedURLException comes in two cases:

  1. If you made some mistake in the URL - like forgot slashes or spelling mistake of (http/https) protocol.
  2. If URL is not parse-able.

In this case SocketIO is taking a URL string as input. Since Java throws exception for potential error, if at all this situation arises you need to handle that. That is why compiler is throwing this exception.

In your case you have hardcoded the value - but compiler is unsure about that, or maybe your code is running into those scenarios mentioned before.

So throwing this exception will do the fix - as Christiaan mentioned.

Upvotes: 0

user1952629
user1952629

Reputation:

Fixed it by using this:

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

Upvotes: 1

Related Questions