日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 综合教程 >内容正文

综合教程

html查看器android

發(fā)布時間:2024/1/4 综合教程 33 生活家
生活随笔 收集整理的這篇文章主要介紹了 html查看器android 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

1.android的API提供了訪問網(wǎng)絡的一個類HttpURLConnection

2.通過發(fā)送GET請求獲取服務器返回的html代碼

3.先看看布局文件,如下所示,

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

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

    <Button
        android:onClick="get"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="獲取源碼"/>
    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <TextView
            android:id="@+id/tv"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
           android:textColor="#00f"
            android:hint="源碼顯示"/>
    </ScrollView>

</LinearLayout>

布局樣子:

4.下來是清單文件,記得連接網(wǎng)絡要添加權限

5.1再看java代碼,MainActivity的

package com.market.source;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.ProtocolException;
import java.net.URL;
import java.net.URLConnection;

import butterknife.BindView;
import butterknife.ButterKnife;

public class MainActivity extends Activity {

    @BindView(R.id.et)
     EditText et;

    @BindView(R.id.tv)
     TextView tv;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
    }


    public void get(View vew) throws IOException {
        final String str = et.getText().toString().trim();


        new Thread(){

            @Override
            public void run() {
                //1.請求地址url
                URL url = null;
                try {
                    url = new URL(str);
                    //2.獲取對這個地址的連接
                    HttpURLConnection con = (HttpURLConnection) url.openConnection();

                    //3.設置對這個地址的請求,GET請求
                    con.setRequestMethod("GET");
                    //4.設置請求參數(shù)
                    con.setConnectTimeout(5000);
                    //5.獲取服務器的響應
                    int code = con.getResponseCode();
                    //6.根據(jù)響應嗎,判斷請求成功還是失敗,200成功
                    if(code==200){
                        //7.成功的話,服務器一流的形式返回數(shù)據(jù)
                        InputStream inputStream = con.getInputStream();

                        //8.這個流是字節(jié)流,需要我們轉換為字符流才可以認識
                        final String info = StreamTool.StreamtoString(inputStream);
                        Log.e("MainActivity",info);
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                tv.setText(info);
                            }
                        });

                    }
                } catch (MalformedURLException e) {
                    e.printStackTrace();
                } catch (ProtocolException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }


            }




        }.start();
    }


}

5.2工具類,用來將一個流轉化為字符串

public class StreamTool {

    public static String StreamtoString(InputStream inputStream) {

        int len = -1;
        byte[] buffer = new byte[1024];
        //內(nèi)存數(shù)組輸出流
        ByteArrayOutputStream baos = new ByteArrayOutputStream();

        try {
            while((len=inputStream.read(buffer)) != -1){

               baos.write(buffer,0,len);
            }
            String str = new String(baos.toByteArray());
            return str;

        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                inputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        return null;
    }

}

6.運行效果查看

需要程序源碼的可以加我微信x241602私聊。

總結

以上是生活随笔為你收集整理的html查看器android的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。