0

我有以下几点url

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

本节是固定不变的:

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

我将参数设置为url如下所示:

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

或者 :

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

我怎么能写url这个url builder

4

2 回答 2

0

如果你想创建一个UrlBuilderusing builder pattern,可以这样做:

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)
        );
    }
}

然后你就可以创建你URL的下一个

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

注意:这只是说明想法,所以我使用了假参数的名称和错误的参数数量,请注意你应该有 6 个参数并设置正确的名称。

于 2016-09-08T11:14:56.237 回答
-2

尝试这个...

URL domain = new URL("http://example.com");
URL url = new URL(domain + "/abcd/abcd");
于 2016-09-08T10:58:01.487 回答