查看htmlView
2016-05-23 23:42
477 查看
1、视图
2、权限
<uses-permission android:name="android.permission.INTERNET"/>
3、MainActivity
4、辅助类
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context=".MainActivity" > <EditText android:id="@+id/et_path" android:layout_width="fill_parent" android:layout_height="wrap_content" android:hint="请输入内容" /> <Button android:onClick="viewSource" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="查看内容" /> <ScrollView android:layout_width="fill_parent" android:layout_height="fill_parent" > <TextView android:layout_width="fill_parent" android:layout_height="fill_parent" android:id="@+id/tv_content" /> </ScrollView> </LinearLayout>
2、权限
<uses-permission android:name="android.permission.INTERNET"/>
3、MainActivity
package com.example.htmlview; import java.io.InputStream; import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.URL; import com.example.htmlview.utils.StreamTools; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.app.Activity; import android.text.TextUtils; import android.view.Menu; import android.view.View; import android.widget.EditText; import android.widget.TextView; import android.widget.Toast; public class MainActivity extends Activity { private static final int VIEW_UI = 1; private static final int ERROR = 2; private TextView tv_content; private EditText et_path; private Handler handler = new Handler(){ @Override public void handleMessage(android.os.Message msg) { // TODO Auto-generated method stub switch(msg.what){ case ERROR: Toast.makeText(MainActivity.this, "抛出异常", 0).show(); break; case VIEW_UI: String str = (String)msg.obj; tv_content.setText(str); break; } } }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); tv_content = (TextView)findViewById(R.id.tv_content); et_path = (EditText)findViewById(R.id.et_path); } public void viewSource(View view){ final String path = et_path.getText().toString().trim(); if(TextUtils.isEmpty(path)){ Toast.makeText(this, "路径不能为空", 0).show(); }else{ new Thread(){ public void run(){ try { URL url = new URL(path); HttpURLConnection conn=(HttpURLConnection) url.openConnection(); conn.setRequestMethod("GET"); conn.setConnectTimeout(5000); conn.setRequestProperty("User-Agent", "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0)"); int code = conn.getResponseCode(); if(code == 200){ InputStream is = conn.getInputStream(); String result = StreamTools.readInputStream(is); if(result != null){ Message msg = new Message(); msg.what = VIEW_UI; msg.obj = result; handler.sendMessage(msg); } } } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); Message msg = new Message(); msg.what = ERROR; handler.sendMessage(msg); } }; }.start(); } } }
4、辅助类
package com.example.htmlview.utils; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; public class StreamTools { public static String readInputStream(InputStream is){ try { ByteArrayOutputStream baos = new ByteArrayOutputStream(); int len=0; byte[] buffer = new byte[1024]; while((len = is.read(buffer)) != -1){ baos.write(buffer, 0, len); } baos.close(); is.close(); byte[] result = baos.toByteArray(); return new String(result); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); return null; } } }
相关文章推荐
- HTML之表单(笔记)
- HTML标签学习总结(1)
- HTML 幻灯片预览效果
- html转pdf 下载操作
- selenium webdriver+testng自定义html测试报告
- Html中截取url参数 实现HTML间的url传值
- 黄聪:wkhtmtopdf--高分辨率HTML转PDF
- HTML常用标签及其全称
- HTML::LinkExtor, links(), parse_file()
- HTML 5 Canvas
- HTML入门
- 导出html表格到excel
- html文本属性
- HTML设置字体
- HTML盒子模型
- htmlunit模拟浏览器实现163邮箱自动登录
- Smarty实现页面静态化(生成HTML)的方法
- html表单
- html固定宽度下拉框内容显示不全问题解决方法
- Smarty实现页面静态化(生成HTML)的方法