Java线程连接
我想创建一个应用程序,它可以显示最新的电影数据,从电影数据库预告片 我尝试连接url,用线程搜索预告片。但它只加载了前38个连接成功,但我需要60个。我不知道为什么。 有人有吗 附言:对不起,我的英语不好Java线程连接,java,multithreading,url,connection,Java,Multithreading,Url,Connection,我想创建一个应用程序,它可以显示最新的电影数据,从电影数据库预告片 我尝试连接url,用线程搜索预告片。但它只加载了前38个连接成功,但我需要60个。我不知道为什么。 有人有吗 附言:对不起,我的英语不好 private Thread loadTrailers = new Thread(){ @Override public void run(){ int counter = 0; String str = new String("");
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.");
}
}
}
};
private Thread loadtrails=new Thread(){
@凌驾
公开募捐{
int计数器=0;
String str=新字符串(“”);
BufferedReader br=null;
try{//try开放源代码站点
URL=新URL(“https://api.themoviedb.org/3/movie/“+id+”/videos?api_key=“+myApikey”);
br=新的BufferedReader(新的InputStreamReader(url.openConnection().getInputStream());//openConnection()有问题
如果(br!=null){
如果((str=br.readLine())!=null){
字符串[]moviedatas=str.split(“\”id\”);
对于(int i=1;i
Post code如何创建和调用线程。loadtrailes.start()在电影构造函数中调用。因此,当创建一部电影时,开始下载图片和预告片链接,两者都带有踏板。公共电影(字符串标题、字符串语言、字符串海报、字符串概述、字符串isadult、字符串速率、字符串日期、字符串id){this.title=new String(title);this.language=new String(language);this.overview=new String(overview);如果(isadult.equals(“false”){this.isadult=false;}else{this.isadult=true;}this.rate=Double.parseDouble(rate);this.date=date;this.id=id;this.\u poster\uu=poster;loadImage.start();loadtrailes.start();}
有太多地方可能会出错……你甚至确定所有的响应都是有效的吗?不幸的是,是的,正如我说的,它在前38次运行得很好,但在最后22次尝试url.openConnection()时,它说“从预告片连接失败”Post code如何创建和调用线程。LoadTrailes.start()在电影构造函数中调用。因此,当创建电影开始时,下载图片和带有踏板的拖车链接。公共电影(字符串标题、字符串语言、字符串海报、字符串概述、字符串Isadalt、字符串速率、字符串日期、字符串id){this.title=new String(title);this.language=new String(language);this.overview=new String(overview);if(isadult.equals(“false”){this.isadult=false;}else{this.isadult=true;}this.rate=Double.parseDouble(rate);this.date=date;this.id=id=id;this.\u poster=poster;loadImage.start();loadtrailes.start();}
有太多地方可能会出错……你甚至确定所有的响应都是有效的吗?不幸的是,是的,正如我说的,它在前38次运行得很好,只有在最后22次尝试url.openConnection()时,它说“拖车连接失败”