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);
IKavanagh
  • 6,089
  • 11
  • 42
  • 47
Salik Reza
  • 31
  • 1
  • 3
  • 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. – Kayaman Oct 26 '15 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()) – Salik Reza Oct 26 '15 at 12:08

2 Answers2

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.)

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
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

Sam
  • 2,950
  • 1
  • 18
  • 26
  • when trying to parse to get hostname and to get protocol its giving a MalformedURLException for an unknown protocol.Can i make custom protocol?? – Salik Reza Oct 26 '15 at 10:57
  • @SalikReza see here http://stackoverflow.com/questions/26363573/registering-and-using-a-custom-java-net-url-protocol – Sam Oct 26 '15 at 11:00