1. 编写如下项目:

2 编写Android清单文件

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

<manifest xmlns:android="http://schemas.android.com/apk/res/android"

package="com.itheima28.htmldemo"

android:versionCode="1"

android:versionName="1.0" >

<uses-sdk

android:minSdkVersion="8"

android:targetSdkVersion="19" />

<uses-permission android:name="android.permission.INTERNET"/>

<application

android:allowBackup="true"

android:icon="@drawable/ic_launcher"

android:label="@string/app_name"

android:theme="@style/AppTheme" >

<activity

android:name="com.itheima28.htmldemo.MainActivity"

android:label="@string/app_name" >

<intent-filter>

<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />

</intent-filter>

</activity>

</application>

</manifest>

3 编写布局文件activity_main.xml

<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:orientation="vertical"

tools:context=".MainActivity" >

<LinearLayout

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:orientation="horizontal">

<EditText

android:id="@+id/et_url"

android:layout_width="0dip"

android:text="http://www.baidu.com"

android:layout_height="wrap_content"

android:singleLine="true"

android:layout_weight="1"/>

<Button

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:onClick="getHtml"

android:text="GO"/>

</LinearLayout>

<ScrollView

android:layout_width="fill_parent"

android:layout_height="fill_parent">

<TextView

android:id="@+id/tv_html"

android:layout_width="fill_parent"

android:layout_height="fill_parent"/>

</ScrollView>

</LinearLayout>

4 编写Activity的类MainActivity如下:

package com.itheima28.htmldemo;

import java.io.ByteArrayOutputStream;

import java.io.IOException;

import java.io.InputStream;

import java.net.HttpURLConnection;

import java.net.URL;

import android.app.Activity;

import android.os.Bundle;

import android.os.Handler;

import android.os.Message;

import android.text.TextUtils;

import android.util.Log;

import android.view.View;

import android.widget.EditText;

import android.widget.TextView;

import android.widget.Toast;

public class MainActivity extends Activity {

private static final String TAG = "MainActivity";

private static final int SUCCESS = 0;

protected static final int ERROR = 1;

private EditText etUrl;

private TextView tvHtml;

private Handler handler = new Handler() {

@Override

public void handleMessage(Message msg) {

super.handleMessage(msg);

switch (msg.what) {

case SUCCESS:

tvHtml.setText((String) msg.obj);

break;

case ERROR:

Toast.makeText(MainActivity.this, "访问失败", 0).show();

break;

default:

break;

}

}

};

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

etUrl = (EditText) findViewById(R.id.et_url);

tvHtml = (TextView) findViewById(R.id.tv_html);

}

public void getHtml(View v) {

final String url = etUrl.getText().toString();

new Thread(new Runnable() {

@Override

public void run() {

// 请求网络

String html = getHtmlFromInternet(url);

if(!TextUtils.isEmpty(html)) {

// 更新textview的显示了

Message msg = new Message();

msg.what = SUCCESS;

msg.obj = html;

handler.sendMessage(msg);

} else {

Message msg = new Message();

msg.what = ERROR;

handler.sendMessage(msg);

}

}

}).start();

}

/**

* 根据给定的url访问网络, 抓去html代码

* @param url

* @return

*/

protected String getHtmlFromInternet(String url) {

try {

URL mURL = new URL(url);

HttpURLConnection conn = (HttpURLConnection) mURL.openConnection();

conn.setRequestMethod("GET");

conn.setConnectTimeout(10000);

conn.setReadTimeout(5000);

//         conn.connect();

int responseCode = conn.getResponseCode();

if(responseCode == 200) {

InputStream is = conn.getInputStream();

String html = getStringFromInputStream(is);

return html;

} else {

Log.i(TAG, "访问失败: " + responseCode);

}

} catch (Exception e) {

e.printStackTrace();

}

return null;

}

/**

* 根据流返回一个字符串信息

* @param is

* @return

* @throws IOException

*/

private String getStringFromInputStream(InputStream is) throws IOException {

ByteArrayOutputStream baos = new ByteArrayOutputStream();

byte[] buffer = new byte[1024];

int len = -1;

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

baos.write(buffer, 0, len);

}

is.close();

String html = baos.toString();  // 把流中的数据转换成字符串, 采用的编码是: utf-8

String charset = "utf-8";

if(html.contains("gbk") || html.contains("gb2312")

|| html.contains("GBK") || html.contains("GB2312")) {       // 如果包含gbk, gb2312编码, 就采用gbk编码进行对字符串编码

charset = "gbk";

}

html = new String(baos.toByteArray(), charset);  // 对原有的字节数组进行使用处理后的编码名称进行编码

baos.close();

return html;

}

}

10_Android中通过HttpUrlConnection访问网络,Handler和多线程使用,读取网络html代码并显示在界面上,ScrollView组件的使用的更多相关文章

  1. android中利用HttpURLConnection进行Get、Post和Session读取页面。

    直接上代码,调用的时候要放在线程中. package slj.getsms; import java.io.BufferedReader; import java.io.InputStreamRead ...

  2. [Android基础]Android中使用HttpURLConnection

    HttpURLConnection继承了URLConnection,因此也能够向指定站点发送GET请求.POST请求.它在URLConnetion的基础上提供了例如以下便捷的方法. int getRe ...

  3. 客户机中PLSQL DEV访问虚拟机中的ORCLE11g,错误百出!

    客户机中PLSQL DEV访问虚拟机中的ORCLE11g,错误百出! 创建时间: 2017/10/14 18:44 作者: CNSIMO 标签: ORACLE 忙了一下午,只有两个字形容:麻烦!   ...

  4. 安卓中的消息循环机制Handler及Looper详解

    我们知道安卓中的UI线程不是线程安全的,我们不能在UI线程中进行耗时操作,通常我们的做法是开启一个子线程在子线程中处理耗时操作,但是安卓规定不允许在子线程中进行UI的更新操作,通常我们会通过Handl ...

  5. Android中判断网络连接是否可用及监控网络状态

    Android中判断网络连接是否可用及监控网络状态 作者: 字体:[增加 减小] 类型:转载 获取网络信息需要在AndroidManifest.xml文件中加入相应的权限,接下来详细介绍Android ...

  6. Android中使用HttpURLConnection实现GET POST JSON数据与下载图片

    Android中使用HttpURLConnection实现GET POST JSON数据与下载图片 Android6.0中把Apache HTTP Client全部的包与类都标记为deprecated ...

  7. delphi 中OutputDebugString 函数的妙用(使用DebugView或者Pascal Analyzer软件,在运行过程中就能监视和捕捉日志,而且通过网络就能监视)

    原文地址 https://www.peganza.com/delphi-and-outputdebugstring.html 曾经想要实时监控您的Delphi应用程序,并能够查看日志消息吗?当然,您始 ...

  8. 【Azure 应用服务】Azure Function集成虚拟网络,设置被同在虚拟网络中的Storage Account触发,遇见Function无法触发的问题

    一切为了安全,所有的云上资源如支持内网资源访问,则都可以加入虚拟网络 问题描述 使用Azure Function处理Storage Account中Blob 新增,更新,删除等情况.Storage A ...

  9. Docker 外部访问容器Pp、数据管理volume、网络network 介绍

    Docker 外部访问容器Pp.数据管理volume.网络network 介绍 外部访问容器 容器中可以运行一些网络应用,要让外部也可以访问这些应用,可以通过 -P 或 -p 参数来 指定端口映射. ...

随机推荐

  1. Git提交代码到远程服务器

    1.下载Git 不用说了,这个是必须的,也是最简单的步骤,地址如下: http://git-scm.com/download 这里会提供三个版本的下载地址,读者可以自行查找. 2.创建代码库 远程的代 ...

  2. Dynamics CRM2016 WebApi查询之alternate key

    本篇继续来分享web api查询中的一条,利用alternate key进行查询. alternate key是个什么东西这里就不解释了如果有不知道的可以去看sdk.这里以房号信息实体为例,新建一个键 ...

  3. log file sync 因为数据线有问题而造成高等侍的表现

    这是3月份某客户的情况,原因是服务器硬件故障后进行更换之后,业务翻译偶尔出现提交缓慢的情况.我们先来看下awr的情况. 我们可以看到,该系统的load profile信息其实并不高,每秒才21个tra ...

  4. 合成/聚合复用原则(CARP)

    组合/聚合复用原则(Composite/Aggregate Reuse Principle或CARP),就是在一个新的对象里面使用一些已有的对象,使之成为新对象的一部分,新对象通过向这些对象的委派达到 ...

  5. Apache commons email 使用过程中遇到的问题

    apache-commons-email是对mail的一个封装,所以使用起来确实是很方便.特别的,官网上的tutorial也是极其的简单.但是我也仍然是遇到了没有解决的问题. jar包的添加 mail ...

  6. Shell脚本生成网页版相册浏览器

    今天学到了一招,那就是使用脚本制作一款网页版相册浏览器.先上图吧. 必备基础 操作系统: 以linux为内核的操作系统都行 编程语言:Shell(bash)脚本,相关基础知识即可 下载工具:wget ...

  7. 19 Handler 总结

    Handler 一, 回顾异步任务 AsyncTask 二, android 使用线程的规则 1,在主线程 不能做阻塞操作 2,在主线程之外的线程不能更新Ui 三, Handler的作用 1,在子线程 ...

  8. iOS常见控件的基本使用

    UI相关类继承关系 UIView 常见属性和方法 UIView属性 UIView方法 UIControl 常用控件 UIImageView 图片显示控件android ImageView UISlid ...

  9. Hibernate系列学习之(二) 多对一、一对一、一对多、多对多的配置方法

    这是近期做案件录入.java项目中体会的几点:项目理解很是深刻,和大家共勉! hihernate一对多关联映射(单向Classes----->Student) 一对多关联映射利用了多对一关联映射 ...

  10. 发现----Android Demo

    时间悄悄的走,转眼来实习已经三个月了,三个月的时间,小编慢慢的成长着,从刚开始的电商项目到现在的车段子项目,小编在走过一个又一个项目的同时,走过了一个又一个战胜自己的奇迹,每次遇到一个新的技术点,小编 ...