Build URL in java

前端 未结 5 1477
故里飘歌
故里飘歌 2021-02-05 00:56

Trying to build http://IP:4567/foldername/1234?abc=xyz. I don\'t know much about it but I wrote below code from searching from google:

import java.n         


        
5条回答
  •  北荒
    北荒 (楼主)
    2021-02-05 01:30

    Use OkHttp

    There is a very popular library named OkHttp which has been starred 20K times on GitHub. With this library, you can build the url like below:

    import okhttp3.HttpUrl;
    
    URL url = new HttpUrl.Builder()
        .scheme("http")
        .host("example.com")
        .port(4567)
        .addPathSegments("foldername/1234")
        .addQueryParameter("abc", "xyz")
        .build().url();
    

    Or you can simply parse an URL:

    URL url = HttpUrl.parse("http://example.com:4567/foldername/1234?abc=xyz").url();
    

提交回复
热议问题