2011-07-22 49 views
1

我想連接到網站,過濾一些內容,然後將其放入字符串中,但我不知道如何執行此操作。將Android URL內容轉換爲字符串

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? 
    } 

回答

2

聲明String輸出到while循環之前:

String output = ""; 

然後,只需追加到該字符串中的每個迭代:

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

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

    } 

然後就去做sp.toString();

+0

很好,非常感謝你! – Janjan

+0

我很高興我幫你。 –

0

Nik ola的回答是可以的,只是對使用StringBuilder的改進:

StringBuilder sb = new StringBuilder(); 

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