1

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;
4

1 回答 1

1

你可以这样做。它还与杰克逊进行解析:

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();
    }
}
于 2013-10-28T09:01:59.747 回答