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

2.通过发送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.下来是清单文件,记得连接网络要添加权限

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.设置请求参数
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();
} }

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

public 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;
} }

6.运行效果查看

html查看器android的更多相关文章

  1. Android 网络HTML查看器

    本文实现一个基于Android的网络HTML查看器 新建项目,项目布局文件如下: <LinearLayout xmlns:android="http://schemas.android ...

  2. Android 网络图片查看器

    今天来实现一下android下的一款简单的网络图片查看器 界面如下: 代码如下: <LinearLayout xmlns:android="http://schemas.android ...

  3. 无废话Android之内容观察者ContentObserver、获取和保存系统的联系人信息、网络图片查看器、网络html查看器、使用异步框架Android-Async-Http(4)

    1.内容观察者ContentObserver 如果ContentProvider的访问者需要知道ContentProvider中的数据发生了变化,可以在ContentProvider 发生数据变化时调 ...

  4. android 网络_网络图片查看器

    xml <?xml version="1.0"?> -<LinearLayout tools:context=".MainActivity" ...

  5. android 网络_网络源码查看器

    xml设计 <?xml version="1.0"?> -<LinearLayout tools:context=".MainActivity" ...

  6. Android -- 网络图片查看器,网络html查看器, 消息机制, 消息队列,线程间通讯

    1. 原理图 2. 示例代码 (网络图片查看器) (1)  HttpURLConnection (2) SmartImageView (开源框架:https://github.com/loopj/an ...

  7. Android中的图片查看器

    本案例,使用Eclipse来开发Android2.1版本号的图片查看器. 1)首先,打开Eclipse.新建一个Android2.1版本号的项目ShowTu,打开res/values中文件夹下的str ...

  8. Android仿微信朋友圈图片查看器

    转载请注明出处:http://blog.csdn.net/allen315410/article/details/40264551 看博文之前,希望大家先打开自己的微信点到朋友圈中去,细致观察是不是发 ...

  9. Android 网络图片查看器与网页源码查看器

    在AndroidManifest.xml里面先添加访问网络的权限: <uses-permission android:name="android.permission.INTERNET ...

随机推荐

  1. Android’s HTTP Clients (httpClient 和 httpURLConnect 区别)

    来源自:http://android-developers.blogspot.jp/2011/09/androids-http-clients.html Most network-connected ...

  2. 二、Hadoop学习笔记————架构学习

    1.成百上千台服务器组成集群,需要时刻检测服务器是否故障 2.用流读取数据更加高效快速 3.存储节点具有运算功能,省略了服务器之间来回传数据的网络带宽限制 4.一次写入,多次访问,不修改数据 5.多平 ...

  3. 2718:晶晶赴约会-poj

    总时间限制:  1000ms 内存限制:  65536kB 描述 晶晶的朋友贝贝约晶晶下周一起去看展览,但晶晶每周的1.3.5有课必须上课,请帮晶晶判断她能否接受贝贝的邀请,如果能输出YES:如果不能 ...

  4. 迷宫问题-POJ 3984

    迷宫问题 Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 24348   Accepted: 14206 Descriptio ...

  5. jstl--->Core 核心标签库->流程控制

    jstl--->Core 核心标签库->流程控制 -->if.choose.when.otherwise  <c:if>条件判断 语法1:没有本体内容  <c:if ...

  6. JavaScript 性能优化技巧分享

    JavaScript 作为当前最为常见的直译式脚本语言,已经广泛应用于 Web 应用开发中.为了提高Web应用的性能,从 JavaScript 的性能优化方向入手,会是一个很好的选择. 本文从加载.上 ...

  7. zzuli 1817: match number 模拟

    1817: match number Time Limit: 1 Sec  Memory Limit: 128 MB Submit: 199  Solved: 72 SubmitStatusWeb B ...

  8. c语言的发展历程

    C语言的发展颇为有趣.它的原型ALGOL 60语言. 1963年,剑桥大学将ALGOL 60语言发展成为CPL(Combined Programming Language)语言. 1967年,剑桥大学 ...

  9. dom4j详解

    Dom4j下载及使用Dom4j读写XML简介要使用dom4j读写XML文档,需要先下载dom4j包,dom4j官方网站在 http://www.dom4j.org/目前最新dom4j包下载地址:htt ...

  10. 【NOIP2014提高组】联合权值

    https://www.luogu.org/problem/show?pid=1351 既然是一棵树,就先转化成有根树.有根树上距离为2的点对,路径可能长下面这样: 枚举路径上的中间点X. 第一种情况 ...