简体   繁体   English

使用Uri.builder构建URL

[英]Building URL using Uri.builder

this is the URL that I want to build : https://image.tmdb.org/t/p/w185//nrsx0jEaBgXq4PWo7SooSnYJTv.jpg 这是我要构建的URLhttps : //image.tmdb.org/t/p/w185//nrsx0jEaBgXq4PWo7SooSnYJTv.jpg

 public static URL buildImageURL(String query){
    final String Image_Base_URL="image.tmdb.org";
    final String File_Size="w185";

    Uri.Builder builtUri = new Uri.Builder();
    builtUri.scheme("https")
            .authority(Image_Base_URL)
            .appendPath("t")
            .appendPath("p")
            .appendPath(File_Size).appendPath(query).build();
    URL url = null;
    try{
        url = new URL(builtUri.toString());
    }catch (MalformedURLException e){
        e.printStackTrace();
    }
return url;
}

I can't append // in the URL... 我无法在网址中加上//

If you know your url and need a Uri, use 如果您知道自己的网址并需要Uri,请使用

Uri uri =  Uri.parse( "http://www.facebook.com" );

If you want to compose the Uri, and get the correct Url, use like this: 如果要编写Uri并获取正确的Url,请按以下方式使用:

final static String Image_Base_URL="image.tmdb.org";
private static final String File_Size="w185";

public static String buildImageURL(String query){
    // https://image.tmdb.org/t/p/w185//nrsx0jEaBgXq4PWo7SooSnYJTv.jpg
    String image = "nrsx0jEaBgXq4PWo7SooSnYJTv.jpg";

    Uri.Builder builder = new Uri.Builder();
    builder.scheme("https")
            .authority(Image_Base_URL)
            .appendPath("t")
            .appendPath("p")
            .appendPath(File_Size)
            .appendPath(image);
    String myUrl = builder.build().toString();

    return myUrl;
}

I have modified your code, and tested. 我已经修改了您的代码,并进行了测试。 Now it outputs the correct formatted URL as string. 现在,它输出正确格式的URL作为字符串。 You can get the URL by using 您可以使用以下网址获取网址

URL myURL = new URL(urlString);

The output is: https://image.tmdb.org/t/p/w185/nrsx0jEaBgXq4PWo7SooSnYJTv.jpg 输出为: https://image.tmdb.org/t/p/w185/nrsx0jEaBgXq4PWo7SooSnYJTv.jpg : https://image.tmdb.org/t/p/w185/nrsx0jEaBgXq4PWo7SooSnYJTv.jpg

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM