javaandroidurluribuilder

URL not being properly build in android


I am using URI Builder class to build this url http://image.tmdb.org/t/p/w185//qjn3fzCAHGfl0CzeUlFbjrsmu4c.jpg

Here is my code:

       `final String TMDB_results = "results";
        final String TMDB_title = "original_title";
        final String TMDB_poster = "backdrop_path";

        JSONObject moviesJson = new JSONObject(moviesJsonStr);
        JSONArray resultArray = moviesJson.getJSONArray(TMDB_results);

        String[] resultnameStrs = new String[resultArray.length()];
        String[] resultposterStrs = new String[resultArray.length()];
        for (int i = 0; i < resultArray.length(); i++) {

            String moviename;
            String movieposter;

            // Get the JSON object in which movie title is there
            JSONObject movietitle = resultArray.getJSONObject(i);
            moviename = movietitle.get(TMDB_title).toString();
            movieposter = movietitle.get(TMDB_poster).toString();

            //Poster URL Builder
            Uri posterbuiltUri = Uri.parse("http://image.tmdb.org/t/p/w185/").buildUpon()
                    .appendPath(movieposter).build();
            String PosterUrl = posterbuiltUri.toString();

            resultposterStrs[i] = PosterUrl;
            resultnameStrs[i] = moviename;
        }

But the URL being build is http://image.tmdb.org/t/p/w185/%2Fqjn3fzCAHGfl0CzeUlFbjrsmu4c.jpg

Here is a part of JSON String from which I am retrieving data:

{"page":1,"results":[{"adult":false,"backdrop_path":"/kvXLZqY0Ngl1XSw7EaMQO0C1CCj.jpg","genre_ids":[28,12,878],"id":102899,"original_language":"en","original_title":"Ant-Man","overview":"Armed with the astonishing ability to shrink in scale but increase in strength, con-man Scott Lang must embrace his inner-hero and help his mentor, Dr. Hank Pym, protect the secret behind his spectacular Ant-Man suit from a new generation of towering threats. Against seemingly insurmountable obstacles, Pym and Lang must plan and pull off a heist that will save the world.","release_date":"2015-07-17","poster_path":"/D6e8RJf2qUstnfkTslTXNTUAlT.jpg","popularity":54.222073,"title":"Ant-Man","video":false,"vote_average":6.9,"vote_count":1859},.......

I think the '/' is being encoded to '%2F'. Is there any way to stop that?

Any help regrading this is appreciated.


Solution

  • appendPath is encoding the / character from your image path -- you're likely seeing %2F as the encoded (aka URL safe) alternative. Your quickest bet here is to do a quick removal of that first slash (which will also prevent double slash from the base URL and the image URL path).

    Uri posterbuiltUri = Uri.parse("http://image.tmdb.org/t/p/w185/").buildUpon() .appendPath(movieposter.replace("/","").build();