zoukankan      html  css  js  c++  java
  • 网页源代码查看器

    1、编写activity_main.xml

    <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"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"/>
    
        <Button
            android:onClick="click"
            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:id="@+id/tv"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"            />
        </ScrollView>
    </LinearLayout>
    View Code

    2、编写MainActivity.java

    package com.hyzhou.htmlviver;
    
    import java.io.InputStream;
    import java.net.HttpURLConnection;
    import java.net.URL;
    
    
    import android.os.Bundle;
    import android.os.Handler;
    import android.os.Message;
    import android.app.Activity;
    import android.text.TextUtils;
    import android.view.View;
    import android.widget.EditText;
    import android.widget.TextView;
    import android.widget.Toast;
    
    public class MainActivity extends Activity {
    
        private EditText edt;
        private TextView tView;
        
        private Handler handler=new Handler()
        {
            public void handleMessage(android.os.Message msg) {
                if (msg.what==1) {
                    String content=(String) msg.obj;
                    tView.setText(content);
                }
                
            };
        };
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            edt=(EditText) findViewById(R.id.et);
            tView=(TextView) findViewById(R.id.tv);
        }
        public void click(View view)
        {
            final String path=edt.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; Trident/5.0)");
                            
                            int code=conn.getResponseCode();
                            if (code==200) {
                                InputStream in=conn.getInputStream();
                                String str=StreamRead.readData(in);
                                Message msg=new Message();
                                msg.what=1;
                                msg.obj=str;
                                handler.sendMessage(msg);
                            }else {
                                Toast.makeText(MainActivity.this, "显示异常", 0).show();
                            }
                        } catch (Exception e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
                        
                    }
                }.start();
            }
        }
    
    
    }
    View Code

    3、编写读取html数据类StreamRead.java

    /**
     * 
     */
    package com.hyzhou.htmlviver;
    
    import java.io.ByteArrayOutputStream;
    import java.io.InputStream;
    
    /**
     * @author hyzhou
     *
     * 2013-12-18
     */
    public class StreamRead {
        public static String readData(InputStream inputStream) throws Exception
        {
            ByteArrayOutputStream outputStream=new ByteArrayOutputStream();
            byte[] buffer=new byte[1024];
            int len=-1;
            while ((len=inputStream.read(buffer)) !=-1 ){
                outputStream.write(buffer,0,len);
                
            }
            byte[] data=outputStream.toByteArray();
            outputStream.close();
            inputStream.close();
            return new String(data);
            
        }
    }
    View Code

    4、添加权限AndroidManifest.xml

    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="com.hyzhou.htmlviver"
        android:versionCode="1"
        android:versionName="1.0" >
    
        <uses-sdk
            android:minSdkVersion="8"
            android:targetSdkVersion="17" />
        <uses-permission android:name="android.permission.INTERNET"/>
    
        <application
            android:allowBackup="true"
            android:icon="@drawable/ic_launcher"
            android:label="@string/app_name"
            android:theme="@style/AppTheme" >
            <activity
                android:name="com.hyzhou.htmlviver.MainActivity"
                android:label="@string/app_name" >
                <intent-filter>
                    <action android:name="android.intent.action.MAIN" />
    
                    <category android:name="android.intent.category.LAUNCHER" />
                </intent-filter>
            </activity>
        </application>
    
    </manifest>
    View Code

    demo下载

  • 相关阅读:
    VUE前端项目配置代理解决跨域问题
    面试题:无序数组排序后的最大相邻差
    Vue项目中更改Vux组件中的样式
    iOS13适配 UITableView 种Cell出现带方框的小箭头
    JavaScript 中的require,import,export
    前端框架 Less 学习与实践
    Vue textarea 高度自适应
    Vue项目中添加手势实现左滑右滑操作
    day24 多态--后续
    day24 继承、封装和多态
  • 原文地址:https://www.cnblogs.com/hyzhou/p/3481598.html
Copyright © 2011-2022 走看看