网页源码查看器

UI文件

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    tools:layout_editor_absoluteY="81dp">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <EditText
            android:id="@+id/et_url"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

        <Button
            android:id="@+id/bt_look"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:onClick="click"
            android:text="查看源代码" />

        <ScrollView
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <TextView
                android:id="@+id/tv_src"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" />
        </ScrollView>

    </LinearLayout>
</android.support.constraint.ConstraintLayout>

逻辑代码

package com.example.administrator.logincase;

import android.Manifest;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;

import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static android.content.pm.PackageManager.PERMISSION_DENIED;
import static android.content.pm.PackageManager.PERMISSION_GRANTED;

public class MainActivity extends AppCompatActivity {
    /*
    * 定义一个handler用于接收线程消息
    * */
    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            TextView srcView = findViewById(R.id.tv_src);
            String strSrc = (String) msg.obj;
            srcView.setText(strSrc);
            super.handleMessage(msg);
        }
    };

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        switch (requestCode) {
            case 0x11: {
                if (grantResults[0] == PERMISSION_GRANTED) {
                    Toast.makeText(this, "网络权限请求成功",
                            Toast.LENGTH_SHORT).show();
                    return;
                } else {
                    Toast.makeText(this, "网络权限请求失败",
                            Toast.LENGTH_SHORT).show();
                    return;
                }
            }
            default:
                break;
        }
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
//        ListView lv = findViewById(R.id.lv);
//        lv.setAdapter(new myAdapter(this));
        // simpleAdapterTest();
        /*
        * 请求网络使用权限
        * */
        if (checkSelfPermission(Manifest.permission.INTERNET) == PERMISSION_DENIED) {
            requestPermissions(new String[]{Manifest.permission.INTERNET}, 0x11);
        }
    }

    public void click(View v) {
        /*
         * 网络的访问必须在线程里面实现
         * */
        new Thread() {
            @Override
            public void run() {
                EditText editText = findViewById(R.id.et_url);
                String strUrl = editText.getText().toString().trim();

                try {
                    URL url = new URL(strUrl);
                    HttpURLConnection conn = (HttpURLConnection) url.openConnection();
                    conn.setRequestMethod("GET");
                    conn.setReadTimeout(5000);
                    if (conn.getResponseCode() != 200) {
                        /*
                        * 这里代码是错误的 不能再线程里面调用Toast
                        * */
                        Toast.makeText(MainActivity.this, "http请求错误",
                                Toast.LENGTH_SHORT).show();
                        return;
                    }
                    InputStream in = new BufferedInputStream(conn.getInputStream());
                    /*
                     * 读出数据
                     * */
                    ByteArrayOutputStream baos = new ByteArrayOutputStream();
                    int nLen = -1;
                    byte[] buffers = new byte[1024];
                    while ((nLen = in.read(buffers)) != -1) {
                        baos.write(buffers, 0, nLen);
                    }

                    Message msg = new Message();
                    msg.obj = new String(baos.toByteArray());
                    handler.sendMessage(msg);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }.start();
    }




}

效果

这里写图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值