【问题标题】:How to show text from a website? [duplicate]如何显示来自网站的文本? [复制]
【发布时间】:2012-07-12 15:44:19
【问题描述】:

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

【问题讨论】:

  • 您是否收到任何错误消息?
  • 任何错误,我都有互联网许可! ://
  • 您在阅读之前验证过输入流确实打开了吗?
  • 只需要在应用程序中显示“abierto”,从这个站点捕获

标签: java android show


【解决方案1】:

您的文本格式不同。您需要将它们都编码为UTF 格式。

编码如下:

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

【讨论】:

    【解决方案2】:

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

    我建议使用apache-commons-io 库从InputStream 读取文本

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

    【讨论】:

    • 我使用了 httpURLConnection, InputStream BufferedReader :)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-23
    • 1970-01-01
    相关资源
    最近更新 更多