如何显示网站上的文字?

时间:2012-07-11 16:08:28

标签: java android show

我正在尝试捕获页面文本,然后在我的应用程序中显示它。 但是当我运行程序时不显示文本。 该网站为http://www.bovalpo.com/cgi-local/xml_bcv.pl?URL=1

public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_examplebackground);

    try {
        URL url = new URL("http://www.bovalpo.com/cgi-local/xml_bcv.pl?URL=1"); 
        BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));

        String inputLine="";
        String inputText = ""; 
        while ((inputLine = in.readLine()) != null) {
            inputText = inputText + inputLine;
        }

        System.out.println(inputText);
    }catch(MalformedURLException t){
        System.out.println("error de url");
    } catch(Throwable t){
        System.out.println("error de bufffer");
    }
}

2 个答案:

答案 0 :(得分:0)

您的文字格式不同。您需要以UTF格式对它们进行编码。

将其编码为:

String data = URLEncoder.encode("data", "UTF-8");

答案 1 :(得分:0)

如果readLine没有提供换行符,请不要使用InputStream,否则会阻止(等待换行符)。

我建议使用apache-commons-io库来阅读InputStream

中的文字
String charset = "UTF-8";
inputText = org.apache.commons.io.IOUtils.toString (url, charset);