查看htmlView

Posted zhongyinghe

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了查看htmlView相关的知识,希望对你有一定的参考价值。

1、视图

 1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 2     xmlns:tools="http://schemas.android.com/tools"
 3     android:layout_width="match_parent"
 4     android:layout_height="match_parent"
 5     android:orientation="vertical"
 6     tools:context=".MainActivity" >
 7 
 8     <EditText
 9         android:id="@+id/et_path"
10         android:layout_width="fill_parent"
11         android:layout_height="wrap_content"
12         android:hint="请输入内容" />
13     <Button 
14         android:onClick="viewSource"
15         android:layout_width="fill_parent"
16         android:layout_height="wrap_content"
17         android:text="查看内容"
18         />
19     <ScrollView 
20         android:layout_width="fill_parent"
21         android:layout_height="fill_parent"
22         >
23         <TextView 
24             android:layout_width="fill_parent"
25             android:layout_height="fill_parent"
26             android:id="@+id/tv_content"
27             />
28     </ScrollView>
29 
30 </LinearLayout>

2、权限

<uses-permission android:name="android.permission.INTERNET"/>

3、MainActivity

 1 package com.example.htmlview;
 2 
 3 import java.io.InputStream;
 4 import java.net.HttpURLConnection;
 5 import java.net.MalformedURLException;
 6 import java.net.URL;
 7 
 8 import com.example.htmlview.utils.StreamTools;
 9 
10 import android.os.Bundle;
11 import android.os.Handler;
12 import android.os.Message;
13 import android.app.Activity;
14 import android.text.TextUtils;
15 import android.view.Menu;
16 import android.view.View;
17 import android.widget.EditText;
18 import android.widget.TextView;
19 import android.widget.Toast;
20 
21 public class MainActivity extends Activity {
22 
23     private static final int VIEW_UI = 1;
24     private static final int ERROR = 2;
25     private TextView tv_content;
26     private EditText et_path;
27     private Handler handler = new Handler(){
28 
29         @Override
30         public void handleMessage(android.os.Message msg) {
31             // TODO Auto-generated method stub
32             switch(msg.what){
33             case ERROR:
34                 Toast.makeText(MainActivity.this, "抛出异常", 0).show();
35                 break;
36             case VIEW_UI:
37                 String str = (String)msg.obj;
38                 tv_content.setText(str);
39                 break;
40             }
41         }
42         
43     };
44     @Override
45     protected void onCreate(Bundle savedInstanceState) {
46         super.onCreate(savedInstanceState);
47         setContentView(R.layout.activity_main);
48         
49         tv_content = (TextView)findViewById(R.id.tv_content);
50         et_path = (EditText)findViewById(R.id.et_path);
51     }
52 
53 
54     public void viewSource(View view){
55         final String path = et_path.getText().toString().trim();
56         if(TextUtils.isEmpty(path)){
57             Toast.makeText(this, "路径不能为空", 0).show();
58         }else{
59             new Thread(){
60                 public void run(){
61                     try {
62                         URL url = new URL(path);
63                         HttpURLConnection conn=(HttpURLConnection) url.openConnection();
64                         conn.setRequestMethod("GET");
65                         conn.setConnectTimeout(5000);
66                         conn.setRequestProperty("User-Agent", "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0)");
67                         int code = conn.getResponseCode();
68                         if(code == 200){
69                             InputStream is = conn.getInputStream();
70                             String result = StreamTools.readInputStream(is);
71                             if(result != null){
72                                 Message msg = new Message();
73                                 msg.what = VIEW_UI;
74                                 msg.obj = result;
75                                 handler.sendMessage(msg);
76                             }
77                         }
78                     } catch (Exception e) {
79                         // TODO Auto-generated catch block
80                         e.printStackTrace();
81                         Message msg = new Message();
82                         msg.what = ERROR;
83                         handler.sendMessage(msg);
84                         
85                     }
86                 };
87             }.start();
88         }
89     }
90 }

4、辅助类

 1 package com.example.htmlview.utils;
 2 
 3 import java.io.ByteArrayOutputStream;
 4 import java.io.IOException;
 5 import java.io.InputStream;
 6 
 7 public class StreamTools {
 8 
 9     public static String readInputStream(InputStream is){
10         try {
11             ByteArrayOutputStream baos = new ByteArrayOutputStream();
12             int len=0;
13             byte[] buffer = new byte[1024];
14             while((len = is.read(buffer)) != -1){
15                 baos.write(buffer, 0, len);
16             }
17             baos.close();
18             is.close();
19             byte[] result = baos.toByteArray();
20             return new String(result);
21         } catch (Exception e) {
22             // TODO Auto-generated catch block
23             e.printStackTrace();
24             return null;
25         }
26         
27     }
28 }

 

以上是关于查看htmlView的主要内容,如果未能解决你的问题,请参考以下文章

如何在 Nativescript HtmlView 标签中设置链接颜色?

为啥我在flutter_web中更新HtmlView时得到_state!= _PlatformViewState.disposed不是真正的异常?

持久片段和查看器

postman 自动生成 curl 代码片段

postman 自动生成 curl 代码片段

无法在片段内查看接收短信