2011-10-22 100 views
0

我想獲取網頁源代碼。現在只顯示。 我無法成功編譯此代碼。 Eclipse說未處理的聲明類型IOExeption。當我添加「拋出聲明」時仍然存在一些問題。我很抱歉我的英語。感謝您的回答。Android HtmlGet麻煩

package com.html; 

import java.io.BufferedReader; 
// More and more import 
import android.widget.TextView; 


public class GetHtmlActivity extends Activity 
{ 
    String html; 

    public String HtmlGet(String url) throws IOException 
    { 
      HttpClient client = new DefaultHttpClient(); 
      HttpGet request = new HttpGet(url); 
      HttpResponse response = client.execute(request); 
      InputStream in = response.getEntity().getContent(); 
      BufferedReader reader = new BufferedReader(new InputStreamReader(in)); 
      StringBuilder str = new StringBuilder(); 
      String line = null; 
      while((line = reader.readLine()) != null) 
       { 
        str.append(line); 
       } 
      in.close(); 
      return str.toString(); 
    } 

    public void onCreate(Bundle Spusteni) 
    { 
     super.onCreate(Spusteni); 
     setContentView(R.layout.main); 

     HtmlGet("***"); 

     TextView tv = new TextView(this); 
     tv.setText(html); 
     setContentView(tv); 
    } 

} 

回答

0

您需要捕捉異常。

try { 
    HtmlGet("***"); 
} catch (IOException e) { 
    // do something in case of error 
} 
+0

當我這樣做的只有一件事是我得到的是 \t趕上(IOExeption E)命令 – Nokimey