• 构造函数
    URIBuilder()
    URIBuilder(final String string) 内部会创建URI对象
    URIBuilder(final URI uri)
    非空的两个构造实际内部都调用了digestURI(uri) 将URI对象解析并赋值给类的属性
  • 类属性
  •     String url = "http://www.google.com/search?hl=en&q=httpclient&btnG=Google+Search&aq=f&oq=";
        URIBuilder uriBuilder = new URIBuilder(url);
        System.out.println(uriBuilder.getScheme());
        System.out.println(uriBuilder.getUserInfo());
        System.out.println(uriBuilder.getHost());
        System.out.println(uriBuilder.getPort());
        System.out.println(uriBuilder.getPath());
        System.out.println(uriBuilder.getQueryParams());
        System.out.println(uriBuilder.getFragment());
        System.out.println(uriBuilder.getCharset());
        输出如下所示:
        scheme:http
        userinfo:null
        host:www.google.com
        prot:-1  端口默认是80,当显示指定端口时,此处便能取到值
        path:/search
        queryParams:[hl=en, q=httpclient, btnG=Google Search, aq=f, oq=]
        fragment:null
        charset:null
        某些属性含义未知手动设置一次
        String url = "http://info.sporttery.cn/football/info/fb_match_hhad.php?m=102909";
        URIBuilder uriBuilder = new URIBuilder(url);
        uriBuilder.setFragment("111");
        uriBuilder.setUserInfo("222", "333");
        uriBuilder.setCharset(new GBK());
        System.out.println(uriBuilder.build());
        System.out.println(uriBuilder.getScheme());
        System.out.println(uriBuilder.getUserInfo());
        System.out.println(uriBuilder.getHost());
        System.out.println(uriBuilder.getPath());
        System.out.println(uriBuilder.getQueryParams());
        System.out.println(uriBuilder.getFragment());
        System.out.println(uriBuilder.getCharset());
        输出如下:
        http://222:333@info.sporttery.cn/football/info/fb_match_hhad.php?m=102909#111
        scheme:http
        userInfo:222:333   没见过这东西-。-
        host:info.sporttery.cn
        path:/football/info/fb_match_hhad.php
        queryParams:[m=102909]
        fragment:111   路由
        charset:GBK

    常用方法
    URIBuilder setParameters(final List nvps)
    URIBuilder addParameters(final List nvps)
    URIBuilder setParameters(final NameValuePair… nvps)
    上面这三个方法中我觉得俩set其实是一样的

  •     String url = "http://info.sporttery.cn/football/info/fb_match_hhad.php?m=102909";
        URIBuilder uriBuilder = new URIBuilder(url);
        ArrayList<NameValuePair> objects = new ArrayList<>();
        ArrayList<NameValuePair> objects1 = new ArrayList<>();
        NameValuePair m = new BasicNameValuePair("m", "1");
        objects.add(m);
        NameValuePair m1 = new BasicNameValuePair("m", "8");
        objects1.add(m1);
        uriBuilder.setParameters(objects);
        uriBuilder.addParameters(objects1);
        System.out.println(uriBuilder.build());
        输出:http://info.sporttery.cn/football/info/fb_match_hhad.php?m=1&m=8
        得出结论:set会覆盖原来的同名参数而add不会

    URIBuilder addParameter(final String param, final String value)
    URIBuilder setParameter(final String param, final String value)

  •     String url = "http://info.sporttery.cn/football/info/fb_match_hhad.php?m=102909";
        URIBuilder uriBuilder = new URIBuilder(url);
        uriBuilder.setParameter("m", "2");
        uriBuilder.addParameter("m" ,"5");
        System.out.println(uriBuilder.build());
        输出:http://info.sporttery.cn/football/info/fb_match_hhad.php?m=2&m=5
        得出结论:set会覆盖原来的同名参数而add不会

URIBuilder clearParameters()

    String url = "http://info.sporttery.cn/football/info/fb_match_hhad.php?m=102909";
    URIBuilder uriBuilder = new URIBuilder(url);
    uriBuilder.clearParameters();
    System.out.println(uriBuilder.build());
    输出:http://info.sporttery.cn/football/info/fb_match_hhad.php
    就是清除所有参数
  • NameValuePair
    只有一个实现类BasicNameValuePair就是用来设置参数键值对BasicNameValuePair(final String name, final String value)
Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐