Android URL内容到字符串

时间:2011-07-22 18:53:47

标签: android

我想连接到一个网站,过滤一些内容然后把它放在一个字符串中,但我不知道该怎么做。

public void zahlenLaden (View view) throws Exception { 

    URL oracle = new URL("http://www.blabla.de");
    URLConnection yc = oracle.openConnection();
    BufferedReader in = new BufferedReader(
                            new InputStreamReader(
                            yc.getInputStream()));
    String inputLine;

    while ((inputLine = in.readLine()) != null) {
  //What I have to write here?
    }

3 个答案:

答案 0 :(得分:2)

在while循环之前声明要输出的字符串:

String output = "";

然后在每次迭代中追加到该字符串:

output += inputLine + "\n"; (don't forget the omitted newline)

答案 1 :(得分:2)

StringBuilder sb = new StringBuilder();

 while ((inputLine = in.readLine()) != null) {
      sb.append(inputLine + "\n");

    }

然后只做sp.toString();

答案 2 :(得分:0)

Nikola的回答是可以的,只是改进了StringBuilder的使用:

StringBuilder sb = new StringBuilder();

 while ((inputLine = in.readLine()) != null) {
      sb.append(inputLine).append("\n");
 }