0

我正在尝试捕获页面的文本,然后将其显示在我的应用程序中。但是当我运行程序时不显示文本。该网站是 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");
    }
}
4

2 回答 2

0

readLine如果不提供换行符,请不要使用InputStream,否则它将在那里被阻塞(等待换行符)。

我建议使用apache-commons-io库从InputStream

String charset = "UTF-8";
inputText = org.apache.commons.io.IOUtils.toString (url, charset);
于 2012-07-12T06:43:36.983 回答
0

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

将其编码为:

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

于 2012-07-11T16:23:52.973 回答