我的URL Reader类运行速度太慢

时间:2015-05-30 12:47:12

标签: java imdb urlread

在这里,我将String url作为参数。这是imdb链接,例如MovieURL imitation = new MovieURL(“http://www.imdb.com/title/tt3477064/”);

我拿名字,点数和年份信息。但它运行得太慢了。哪种方法让它变慢。我怎么能加快这个?

import java.net.*;
import java.io.*;

public class MovieURL {

// PROPERTIES

private String name;
private String year;
private String point;

private URL link;

private String data;

// CONSTRUCTOR

public MovieURL ( String url )
{

    try {

        link = new URL( url );

        URLConnection connection = link.openConnection();
        BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream()));

        String inputLine;

        while ((inputLine = br.readLine()) != null) 
        {
            data = data + inputLine;                
        }
        br.close();
    }
    catch ( Exception e ) {
        e.printStackTrace();
    }

    name = findName();
    year = findYear();
    point = findPoint();
}

// METHODS

public String findName() {

    int pos = data.indexOf("Discuss");
    int indexOfPar = data.indexOf('(', pos);

    String name = data.substring(pos+8, indexOfPar-1);

    return name;
}

public String getName() {
    return name;
}

public String findYear() {

    int pos = data.indexOf("Discuss");
    int indexOfPar = data.indexOf('(', pos);

    String year = data.substring(indexOfPar+1, indexOfPar+5);

    return year;

}

public String getYear() {
    return year;
}    

public String findPoint() {

    int pos = data.indexOf("Users rated this");

    String point = data.substring(pos+17, pos+20);

    return point;
}

public String getPoint() {
    return point;
}   

public String toString()
{
    return name + " | " + point;

}
}

0 个答案:

没有答案