android html编码转换,html查看器android

该博客介绍了在Android中获取网络HTML代码的方法。利用Android的HttpURLConnection类发送GET请求,获取服务器返回的HTML代码。详细展示了布局文件、清单文件、Java代码及工具类代码,最后可查看运行效果。

1.android的API提供了访问网络的一个类HttpURLConnection

2.通过发送GET请求获取服务器返回的html代码

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

copycode.gif<?xml  version="1.0" encoding="utf-8"?>

android:id="@+id/activity_main"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:orientation="vertical">

android:layout_width="match_parent"

android:layout_height="wrap_content" />

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="获取源码"/>

android:layout_height="match_parent">

android:layout_width="match_parent"

android:layout_height="match_parent"

android:textColor="#00f"

android:hint="源码显示"/>

copycode.gif

布局样子:

1069003-20170623005412945-1513325335.png

4.下来是清单文件,记得连接网络要添加权限

1069003-20170623005508695-609633670.png

5.1再看java代码,MainActivity的

copycode.gifpackage 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.设置请求参数

con.setConnectTimeout(5000);                    //5.获取服务器的响应

int code = con.getResponseCode();                    //6.根据响应吗,判断请求成功还是失败,200成功

if(code==200){                        //7.成功的话,服务器一流的形式返回数据

InputStream inputStream = con.getInputStream();                        //8.这个流是字节流,需要我们转换为字符流才可以认识

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();

}

}

copycode.gif

5.2工具类,用来将一个流转化为字符串

copycode.gifpublic class StreamTool {    public static String StreamtoString(InputStream inputStream) {        int len = -1;        byte[] buffer = new byte[1024];        //内存数组输出流

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;

}

}

copycode.gif

6.运行效果查看

1069003-20170623005620773-185025512.png

1069003-20170623005814429-239941036.png

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值