与线程的Java连接

时间:2017-05-13 18:00:32

标签: java multithreading url connection

我想创建一个应用程序,它可以显示最新的电影数据,以及来自电影数据库的预告片

我尝试连接url,用线程搜索预告片。但它只加载前38个连接成功,但我需要60.我不知道为什么。 有人有吗?

P.S。:对不起我的英语

    private Thread loadTrailers = new Thread(){
    @Override
    public void run(){
        int counter = 0;
        String str = new String("");
        BufferedReader br = null;
        try { // try open source site
            URL url = new URL("https://api.themoviedb.org/3/movie/"+id+"/videos?api_key="+myApikey);
            br = new BufferedReader(new InputStreamReader(url.openConnection().getInputStream()));  // problem with openConnection()
            if(br != null){
                if((str = br.readLine()) != null){
                    String[] moviedatas = str.split("\"id\"");
                    for(int i = 1; i < moviedatas.length && counter < 3;++i){
                        Pattern isTrailer = Pattern.compile(".*Trailer.*");
                        Matcher matc_tr = isTrailer.matcher(moviedatas[i]);
                        if(matc_tr.matches()){
                            Pattern getKey = Pattern.compile(".*key\":\"(.*)\",\"name.*");
                            Matcher key = getKey.matcher(moviedatas[i]);
                            if(key.matches()){
                                links.add("https://www.youtube.com/watch?v="+key.group(1));
                                counter++;
                            }
                        }
                    }
                }
            }
        } catch (MalformedURLException e) {
            System.err.println("Invalid URL!");
        } catch (IOException e) {
            System.err.println("Connection failed from trailers.");
        } finally {
            try {
                if(br != null)
                    br.close();
            } catch (IOException e) {
                System.err.println("File close not success.");
            }
        }
    }
};

0 个答案:

没有答案