enter code here
我从这样的电影服务收到 JSON 响应
{
"total": 157,
"movies": [{
"id": "771205997",
"title": "Gravity",
"year": 2013,
"mpaa_rating": "PG-13",
"runtime": 91,
-------------
-------------
对于网址
http://api.rottentomatoes.com/api/public/v1.0/lists/movies/in_theaters.json?page_limit=16&page=1&country=us&apikey=cj5a4purh8fxgcmrtn95gheu
正如,您可以看到我们在 1 个页面中获得 16 个条目,但有 157 个条目,所以我需要运行这个 url 大约 10 次才能获得不同的条目,页面参数每次都会更改。我想知道 java 中是否有办法(使用线程?)在开始解析之前立即获取所有响应?
这是我使用的代码
HttpGet httpget = new HttpGet("http://api.rottentomatoes.com/api/public/v1.0/lists/movies/in_theaters.json?page_limit=16&page=1&country=us&apikey=cj5a4purh8fxgcmrtn95gheu");
HttpResponse response = httpclient.execute(httpget);
// Get the response
BufferedReader rd = new BufferedReader
(new InputStreamReader(response.getEntity().getContent()));
String line = "";
StringBuilder textView = new StringBuilder();
while ((line = rd.readLine()) != null) {
textView.append(line);
// System.out.println(textView);
}
String resp = textView.toString();
return resp;
你可以
这样做。它还对杰克逊进行了解析:
public class Test {
public static class MoviesResponse{
public static class Movie{
public String id;
public String title;
public String year;
}
public int total;
public List<Movie> movies = new ArrayList<>();
public static MoviesResponse fetch(int page){
try {
String request = "http://api.rottentomatoes.com/api/public/v1.0/lists/movies/in_theaters.json?page_limit=16&page=" +
page +
"&country=us&apikey=cj5a4purh8fxgcmrtn95gheu";
HttpClient httpClient = new DefaultHttpClient();
HttpGet httpget = new HttpGet(request);
HttpResponse response = httpClient.execute(httpget);
BufferedReader rd = new BufferedReader
(new InputStreamReader(response.getEntity().getContent()));
String line = "";
StringBuilder textView = new StringBuilder();
while ((line = rd.readLine()) != null) {
textView.append(line);
// System.out.println(textView);
}
String resp = textView.toString();
ObjectMapper mapper = new ObjectMapper();
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
return mapper.readValue(resp, MoviesResponse.class);
} catch (IOException e) {
e.printStackTrace();
throw new RuntimeException(e);
}
}
}
public static void main(String[] args) throws Exception {
int numberOfSimultaneousThreads = 3;
ExecutorService service = Executors.newFixedThreadPool(numberOfSimultaneousThreads);
MoviesResponse firstPage = MoviesResponse.fetch(1);
System.out.println("firstPage.total = " + firstPage.total);
int pageCount = 3; //update it with formula based on firstPage.total and pageSize (i.e. firstPage.total / pageSize + 1)
List<Callable<MoviesResponse>> jobs = new ArrayList<>();
for(int i = 2;i <= pageCount;i++){
final int page = i;
jobs.add(new Callable<MoviesResponse>() {
@Override
public MoviesResponse call() throws Exception {
return MoviesResponse.fetch(page);
}
});
}
List<Future<MoviesResponse>> results = service.invokeAll(jobs, 2, TimeUnit.MINUTES);
for (Future<MoviesResponse> result : results) {
for (MoviesResponse.Movie movie : result.get().movies) {
System.out.println(movie.title + " (" + movie.year + ")");
}
}
service.shutdown();
}
}