-4

下面是完整的方法。它以字符串形式返回给定 URL 的 HTML 代码。经过某些测试后,我得出的结论是 try 块被跳过了。但我不知道为什么。

    public static String getHtml(String url)
 {
     StringBuilder result = new StringBuilder();
     try {
         HttpClient httpClient = new DefaultHttpClient();
         HttpContext localContext = new BasicHttpContext();
         HttpGet httpGet = new HttpGet(url);
         HttpResponse response = httpClient.execute(httpGet, localContext);


         BufferedReader reader = new BufferedReader(
             new InputStreamReader(
               response.getEntity().getContent()
             )
           );

         String line = null;
         char c = '"'; //The Java complains about this type of char So I removed it from the source.
         while ((line = reader.readLine()) != null){
           for(int i = line.length()-1; i >0; --i){
               if (line.charAt(i) != c){
                   result.append(line);
               }
           }
         }
         return result.toString();
     } catch(Exception e){
            result.append("<p><img src=http://desperateshadows.files.wordpress.com/2013/02/img_0721.jpg?w=730 class=size-full alt=Green green not evergreen /></p><p>Little flower at a glance</p>"); //A test Source

         return result.toString();
     }



 }
4

2 回答 2

0

您正在从 UI 线程调用 execute()。自 Android 3.0+ 起,不允许在 UI 线程上运行阻塞操作。

于 2013-04-26T10:14:35.070 回答
0

在 android manifest.xml 中放上网权限

<manifest xlmns:android...> ...
    <application android:label="@string/app_name" >...
    </application>    
    <uses-permission android:name="android.permission.INTERNET"></uses-permission>
</manifest>
于 2013-04-26T10:19:18.930 回答