你好,游客 登录 注册 搜索
背景:
阅读新闻

Android实现网络图片查看器和网页源码查看器

[日期:2011-09-26] 来源:Linux社区  作者:furongkang [字体:大 中 小]

网页源码查看器

如果网页的源码超过屏幕的显示位置的话,要求出现滚动条.

  1. <ScrollView  
  2.  Android:layout_width="wrap_content"   
  3.  android:layout_height="wrap_content"   
  4. >  
  5.   <TextView    
  6.  android:layout_width="fill_parent"   
  7.  android:layout_height="wrap_content"   
  8.  android:id="@+id/htmlsource"  
  9.  />  
  10. </ScrollView>  

界面如下:

示例

  1.   @Override  
  2.   public void onCreate(Bundle savedInstanceState) {  
  3.       super.onCreate(savedInstanceState);  
  4.       setContentView(R.layout.main);  
  5.       pathText = (EditText) this.findViewById(R.id.path);  
  6.       htmlsource = (TextView) this.findViewById(R.id.htmlsource);  
  7.         
  8.       Button button = (Button) this.findViewById(R.id.button);  
  9.       button.setOnClickListener(new View.OnClickListener() {  
  10.       
  11.     public void onClick(View v) {  
  12.         String path = pathText.getText().toString();  
  13.         try{  
  14.             //获取源码  
  15.             String html = PageService.getHtml(path);  
  16.             htmlsource.setText(html);  
  17.         }catch (Exception e) {  
  18.             Toast.makeText(getApplicationContext(), R.string.error, 1).show();  
  19.         }  
  20.     }  
  21. });  
  22.   }  

 
  1. public class PageService {  
  2.     /**  
  3.      * 获取网页源代码  
  4.      * @param path 网页路径  
  5.      * @return  
  6.      */  
  7.     public static String getHtml(String path) throws Exception{  
  8.         URL url = new URL(path);  
  9.         HttpURLConnection conn = (HttpURLConnection) url.openConnection();  
  10.         conn.setConnectTimeout(5000);  
  11.         conn.setRequestMethod("GET");  
  12.         if(conn.getResponseCode() == 200){  
  13.             byte[] data = StreamTool.read(conn.getInputStream());  
  14.             return new String(data, "UTF-8");  
  15.         }  
  16.         return null;  
  17.     }  
  18. }  
linux
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款