URL 未在 android 中正确构建
URL not being properly build in android
我正在使用 URI Builder class 来构建这个 url
http://image.tmdb.org/t/p/w185//qjn3fzCAHGfl0CzeUlFbjrsmu4c.jpg
这是我的代码:
`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;
}
但是正在构建的 URL 是
http://image.tmdb.org/t/p/w185/%2Fqjn3fzCAHGfl0CzeUlFbjrsmu4c.jpg
这是 JSON 字符串的一部分,我从中检索数据:
{"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},.......
我认为“/”被编码为“%2F”。有什么办法可以阻止吗?
感谢任何对此进行重新分级的帮助。
您那里有一个额外的“/”,请改为编码:
http://image.tmdb.org/t/p/w185/qjn3fzCAHGfl0CzeUlFbjrsmu4c.jpg
appendPath
正在对图像路径中的 /
字符进行编码——您可能会看到 %2F
作为编码(又名 URL 安全)替代。你最快的选择是快速删除第一个斜杠(这也将防止双斜杠来自基本 URL 和图像 URL 路径)。
Uri posterbuiltUri = Uri.parse("http://image.tmdb.org/t/p/w185/").buildUpon()
.appendPath(movieposter.replace("/","").build();
我正在使用 URI Builder class 来构建这个 url http://image.tmdb.org/t/p/w185//qjn3fzCAHGfl0CzeUlFbjrsmu4c.jpg
这是我的代码:
`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;
}
但是正在构建的 URL 是 http://image.tmdb.org/t/p/w185/%2Fqjn3fzCAHGfl0CzeUlFbjrsmu4c.jpg
这是 JSON 字符串的一部分,我从中检索数据:
{"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},.......
我认为“/”被编码为“%2F”。有什么办法可以阻止吗?
感谢任何对此进行重新分级的帮助。
您那里有一个额外的“/”,请改为编码:
http://image.tmdb.org/t/p/w185/qjn3fzCAHGfl0CzeUlFbjrsmu4c.jpg
appendPath
正在对图像路径中的 /
字符进行编码——您可能会看到 %2F
作为编码(又名 URL 安全)替代。你最快的选择是快速删除第一个斜杠(这也将防止双斜杠来自基本 URL 和图像 URL 路径)。
Uri posterbuiltUri = Uri.parse("http://image.tmdb.org/t/p/w185/").buildUpon()
.appendPath(movieposter.replace("/","").build();