我正在使用烂番茄api,出于某种原因,如果我想获得有关电影导演或工作室等电影的更多信息,我需要另一个http页面。这是我正在获取电影信息的网址。从这个JSON我得到id(“自我”:“http://api.rottentomatoes.com/api/public/v1.0/movies/771205893.json”)。 我想使用那个id-我想与该特定网址建立另一个http连接,并获得有关每部电影的更多信息。 这是电影信息JSON:
{
"total": 591,
"movies": [{
"title": "Jack and Jill",
"year": 2011,
"runtime": "",
"release_dates": {"theater": "2011-11-11"},
"ratings": {
"critics_score": -1,
"audience_score": 90
},
"synopsis": "",
"posters": {
},
"abridged_cast": [
{
"name": "Al Pacino",
"characters": []
},
{
"name": "Adam Sandler",
"characters": []
},
{
"name": "Katie Holmes",
"characters": []
}
],
"links": {
"self": "http://api.rottentomatoes.com/api/public/v1.0/movies/771205893.json",
}
}],
这是我的代码:
if (response != null) {
try {
// convert the String response to a JSON object,
// because JSON is the response format Rotten Tomatoes uses
JSONObject jsonResponse = new JSONObject(response);
// fetch the array of movies in the response
JSONArray movies = jsonResponse.getJSONArray("movies");
// add each movie's title to an array
movieTitles = new String[movies.length()];
for (int i = 0; i < movies.length(); i++) {
JSONObject movie = movies.getJSONObject(i);
movieTitles[i] = movie.getString("title");
}
try {
movieId = new String[movies.length()];
for (int i = 0; i < movies.length(); i++) {
JSONObject movie = movies.getJSONObject(i);
movieId[i] = movie.getString("id");
}
在我获得movieId后,我只想获得用户按下的电影ID 所以我用:
public void onItemClick(AdapterView<?> arg0, View arg1,
int position, long id) {
Intent i = new Intent(getApplicationContext(),
AfterClickingMovieFromInternet.class);
i.putExtra("movieName", movieTitles[position]);
getMovieInfo( movieId[position]);
并在getMovieInfo(movieId [position]中)我试图与api建立第二个连接但由于某种原因它不会执行..... 如果有人有任何想法,我会很高兴知道!
答案 0 :(得分:0)
连续2次连接有什么问题? 你检查了图书馆吗?
我会推荐这个 - https://github.com/kodart/Httpzoid 在内部使用GSON并提供与对象一起使用的API。所有JSON详细信息都被隐藏。
Http http = HttpFactory.create(context);
http.get("http://example.com/users")
.handler(new ResponseHandler<User[]>() {
@Override
public void success(User[] users, HttpResponse response) {
}
}).execute();