0

I have bellow url :

http://www.example.com/api/Video/GetListMusicRelated/0/0/null/105358/0/0/10/null/null 

This section is Fixed and unchangeable:

http://www.example.com/api/Video/GetListMusicRelated/

I set parameter to this url like bellow :

 http://www.example.com/api/Video/GetListMusicRelated/25/60/jim/105358/20/1/5/null/null 

OR :

http://www.example.com/api/Video/GetListMusicRelated/0/0/null/105358,5875,85547/0/0/10/null/null 

How I can write for this url a url builder ?

2 Answers2

0

If you want to create an UrlBuilder using the builder pattern, it could be done like this:

public class UrlBuilder {
    private final String root;
    private int myParam1;
    private String myParam2;

    public UrlBuilder(final String root) {
        this.root = root;
    }

    public UrlBuilder myParam1(int myParam1) {
        this.myParam1 = myParam1;
        return this;
    }

    public UrlBuilder myParam2(String myParam2) {
        this.myParam2 = myParam2;
        return this;
    }

    public URL build() throws MalformedURLException {
        return new URL(
            String.format("%s/%d/%s", root, myParam1, myParam2)
        );
    }
}

Then you will be able to create your URL as next

URL url = new UrlBuilder("http://www.example.com/api/Video/GetListMusicRelated")
    .myParam1(25)
    .myParam2("jim")
    .build();

NB: This only shows the idea, so I used fake parameter's name and incorrect number of parameters, please note that you are supposed to have 6 parameters and set the proper names.

Nicolas Filotto
  • 43,537
  • 11
  • 94
  • 122
-2

try this...

URL domain = new URL("http://example.com");
URL url = new URL(domain + "/abcd/abcd");
snehasish
  • 171
  • 2
  • 10
  • Read carefully please : http://www.example.com/api/Video/GetListMusicRelated/0/0/null/105358,5875,85547/0/0/10/null/null –  Sep 08 '16 at 11:02
  • thats what i did... you can easily change that string. – snehasish Sep 08 '16 at 11:10
  • This answer turned up in the low quality review queue, presumably because you don't provide any explanation of the code. If this code answers the question, consider adding adding some text explaining the code in your answer. This way, you are far more likely to get more upvotes — and help the questioner learn something new. – lmo Sep 08 '16 at 22:08