2

My question is how to parse url where protocol is different?

String str = "olqmeeting://192.168.43.55/uam/meeting/meeting.php?id=A72U5AF9";
URL url = new URL(str);
2
  • 1
    What do you mean by "parse URL"? If there isn't an URLStreamHandler that knows how to process the olqmeeting protocol, you'll get a MalformedURLException for an unknown protocol. Commented Oct 26, 2015 at 10:49
  • its working : URI url = new URI( "olqmeeting"+"://"+serverName+"/uam/meeting/meeting.php?type="+type); System.out.println("Scheme: "+url.getScheme()); System.out.println("HostName: "+url.getHost()) Commented Oct 26, 2015 at 12:08

2 Answers 2

3

You can use the URI class to parse those URLs. It will work ... though you are limited to operations on the URL itself, no on the resource that it refers to. (Refer to the URI javadoc for more details.)

If you want URL to work (and to be able to "connect" to the resource), then you need to implement and configure a URLStreamHandlerFactory that (minimally) understands the URL's protocol and how to handle it.

(If you don't supply a non-null URLStreamHandlerFactory explicitly when you create the URL object, the constructor will attempt to find the default one for the URL's protocol. If it can't do that, you get a MalformedURLException.)

Sign up to request clarification or add additional context in comments.

Comments

0

URL has other constructors where you can define the protocol

URL(String protocol, String host, int port, String file)
Creates a URL object from the specified protocol, host, port number, and file.
URL(String protocol, String host, int port, String file, URLStreamHandler handler)
Creates a URL object from the specified protocol, host, port number, file, and handler.
URL(String protocol, String host, String file)
Creates a URL from the specified protocol name, host name, and file name.

Eventually you can even use the .set() method but it is protected so you need to implement a new custom URL class

2 Comments

when trying to parse to get hostname and to get protocol its giving a MalformedURLException for an unknown protocol.Can i make custom protocol??

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.