zoukankan      html  css  js  c++  java
  • Android 制作一个网页源代码浏览器(HttpURLConnection)

    package com.wuyou.htmlcodeviewer;
    
    import android.os.Bundle;
    import android.os.Handler;
    import android.os.Message;
    import android.support.v7.app.ActionBarActivity;
    import android.text.TextUtils;
    import android.view.View;
    import android.widget.EditText;
    import android.widget.TextView;
    import android.widget.Toast;
    
    import java.io.BufferedReader;
    import java.io.InputStream;
    import java.io.InputStreamReader;
    import java.net.HttpURLConnection;
    import java.net.URL;
    
    public class MainActivity extends ActionBarActivity {
        private static final int ERROR = 0;
        private static final int CHANGE_TEXT = 1;
    
        private EditText editText;
        private TextView textView;
    
        private Handler handler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                if (msg.what == CHANGE_TEXT) {
                    textView.setText((String) msg.obj);
                } else if (msg.what == ERROR) {
                    Toast.makeText(MainActivity.this, (String) msg.obj, Toast.LENGTH_LONG).show();
                }
            }
        };
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            editText = (EditText) findViewById(R.id.et);
            textView = (TextView) findViewById(R.id.tv);
        }
    
        public void click(View view) {
            new Thread(new Runnable() {
                @Override
                public void run() {
                    try {
                        String path = editText.getText().toString().trim();
                        if (TextUtils.isEmpty(path)) {
                            Message message = new Message();
                            message.what = ERROR;
                            message.obj = "地址不能为空!";
                            handler.sendMessage(message);
                        } else {
                            URL url = new URL(path);
                            HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
                            httpURLConnection.setRequestMethod("GET");
                            httpURLConnection.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:22.0) Gecko/20100101 Firefox/22.0");
                            httpURLConnection.setConnectTimeout(5000);//设置连接服务器如果没有在5秒钟运行则抛出错误
                            if (httpURLConnection.getResponseCode() == 200) {
                                InputStream is = httpURLConnection.getInputStream();
                                BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
                                String line = null;
                                StringBuilder sb = new StringBuilder();
                                while ((line = bufferedReader.readLine()) != null) {
                                    sb.append(line);
                                }
                                Message message = new Message();
                                message.what = CHANGE_TEXT;
                                message.obj = sb.toString();
                                handler.sendMessage(message);
                            }
                        }
    
                    } catch (Exception e) {
                        e.printStackTrace();
                        Message message = new Message();
                        message.what = ERROR;
                        message.obj = "出错了!";
                        handler.sendMessage(message);
                    }
                }
            }).start();
        }
    
    }
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        tools:context=".MainActivity"
        tools:ignore="MergeRootFrame">
    
        <EditText
            android:id="@+id/et"
            android:text="http://www.baidu.com/"
            android:layout_height="wrap_content"
            android:layout_width="fill_parent" />
    
        <Button
            android:text="查看源代码"
            android:onClick="click"
            android:layout_height="wrap_content"
            android:layout_width="wrap_content" />
    
    
        <ScrollView
            android:id="@+id/scroll"
            android:layout_height="fill_parent"
            android:layout_width="fill_parent">
    
            <TextView
                android:id="@+id/tv"
                android:layout_height="fill_parent"
                android:layout_width="fill_parent" />
        </ScrollView>
    
    
    </LinearLayout>

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

  • 相关阅读:
    flash/flex builder在IE中stage.stageWidth始终为0的解决办法
    “AS3.0高级动画编程”学习:第一章高级碰撞检测
    Flash/Flex学习笔记(57):实用技巧
    Flash/Flex学习笔记(56):矩阵变换
    flash开发中如何实现界面代码分离
    [转]Flash开发技能树
    flash builder代码格式化以及其它快捷键
    中小型商城系统中的分类/产品属性/扩展属性的数据库设计
    “AS3.0高级动画编程”学习:第四章 寻路(AStar/A星/A*)算法 (下)
    晒晒这两天做的播放器
  • 原文地址:https://www.cnblogs.com/wuyou/p/3426830.html
Copyright © 2011-2022 走看看