2012-07-11 81 views
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"); 
    } 
} 
+0

或http://stackoverflow.com/questions/2423498/how-to-get-the-html-source-of-a-page閱讀文本-from-a-html-link-in-android – kcoppock 2012-07-11 16:13:01

+0

你有沒有收到任何錯誤信息? – ThN 2012-07-11 16:13:29

+0

任何錯誤,我有互聯網許可證和所有! :/ – 2012-07-11 16:14:51

回答

0

您文本的格式是不同的。您需要使用UTF格式對它們進行編碼。

編碼它是這樣的:

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

+0

感謝我修復了錯誤:D – 2012-07-12 13:14:57

0

不要使用readLine如果InputStream不提供換行字符,或者,它會被阻塞(等待換行字符)那裏。

我建議使用apache-commons-io庫從InputStream

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

感謝我修復了錯誤:D – 2012-07-12 13:15:10

+0

im使用httpURLConnection,InputStream BufferedReader :) – 2012-07-12 13:18:16