简体   繁体   中英

Building URL using Uri.builder

this is the URL that I want to build : https://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.parse( "http://www.facebook.com" );

If you want to compose the Uri, and get the correct Url, use like this:

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. You can get the URL by using

URL myURL = new URL(urlString);

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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