网页代码查看器

                       网页代码查看器

1.用一个小小的demo介绍handle.首先制作布局layout.这里设置一个EdiText控件,button,ScollView,和TextView.上面的helloword是生成是自带的,没有删掉,xml代码如下。

<?xml version="1.0" encoding="utf-8"?>
<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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:orientation="vertical"
    tools:context="com.example.lenovo.MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!" />
    <EditText
        android:layout_width="match_parent"
        android:id="@+id/bianji"
        android:layout_height="wrap_content"
        android:text="http://www.baidu.com"/>
    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:onClick="view"
        android:text="确定"/>
    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/view"/>
    </ScrollView>
</LinearLayout>

2.MainActivity代码如下:

package com.example.lenovo;

import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
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.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.logging.Handler;
import java.util.logging.LogRecord;

public class MainActivity extends AppCompatActivity {
    private static final int SUCCESS = 0;
    private static final int ERROR = 1;
    private static final int NETWORK = -1;
    private EditText editText;
    private String path=null;
    private TextView textView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        editText=(EditText)findViewById(R.id.bianji);
        textView= (TextView) findViewById(R.id.view);
    }
   android.os.Handler handler=new android.os.Handler(){//是os包的,而不是untils包的。
       @Override
       public void handleMessage(Message msg) {
           switch (msg.what){
               case SUCCESS:
                  String value= (String) msg.obj;
                   textView.setText(value);
               break;
               case ERROR:
                  Toast.makeText(MainActivity.this,"错误",Toast.LENGTH_LONG).show();
                   break;
               default:
                   break;
           }
           super.handleMessage(msg);
       }
   };
    public  void view(View v){
     path=   editText.getText().toString().trim();
        if (TextUtils.isEmpty(path))
        {
            Toast.makeText(MainActivity.this, "hh", Toast.LENGTH_LONG).show();
            return;
        }
        new Thread(){
            @Override
            public void run() {
                try {
                    URL url=new URL(path);
                    try {
                        HttpURLConnection httpURLConnection=(HttpURLConnection)url.openConnection();
                        httpURLConnection.setConnectTimeout(5000);//设置连接超时时间
                        httpURLConnection.setRequestMethod("GET");
                        String content=httpURLConnection.getContentType();
                        int code=httpURLConnection.getResponseCode();
                        if (code==200){
                            //把流转换成字符串
                            InputStream inputStream= httpURLConnection.getInputStream();
                            Stream stream=new Stream();
                            String s=stream.streamchangechar(inputStream);
                            Message msg=Message.obtain();
                            msg.what=SUCCESS;//这个要是int型
                            msg.obj=s;
                            handler.sendMessage(msg);

                        }
                        else {
                            Message message=Message.obtain();
                            message.what=ERROR;
                            handler.sendMessage(message);
                        }
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                } catch (MalformedURLException e) {
                    e.printStackTrace();
                }
                super.run();
            }
        }.start();
    }
}

3.新建一个类,叫Stream,用途是把流转换成字符串。下面是一种方法,还可以用byteArray进行转换。

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

/**
 * Created by lenovo on 2020/1/22.
 */
public class Stream {
    public String streamchangechar(InputStream inputStream)
    {
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
        String str;
        StringBuilder stringBuilder = new StringBuilder();
        try {
            while ((str=bufferedReader.readLine())!=null){
                stringBuilder.append(str);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return  stringBuilder.toString();
    }
}

4.接下来就是给出联网权限。

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.lenovo">
    <uses-permission android:name="android.permission.INTERNET"/>
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

5.拜拜。

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值