使用AsyncTask从网上获取一个网页,在一个TextView中将其源代码显示出来

本文主要是介绍使用AsyncTask从网上获取一个网页,在一个TextView中将其源代码显示出来,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

使用AsyncTask从网上获取一个网页,在一个TextView中将其源代码显示出来
package test.list;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.util.ArrayList;import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;public class NetworkActivity extends Activity{private TextView message;private Button open;private EditText url;@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.network);message= (TextView) findViewById(R.id.message);url= (EditText) findViewById(R.id.url);open= (Button) findViewById(R.id.open);open.setOnClickListener(new View.OnClickListener() {public void onClick(View arg0) {connect();}});}private void connect() {PageTask task = new PageTask(this);task.execute(url.getText().toString());}class PageTask extends AsyncTask<String, Integer, String> {// 可变长的输入参数,与AsyncTask.exucute()对应ProgressDialog pdialog;public PageTask(Context context){pdialog = new ProgressDialog(context, 0);   pdialog.setButton("cancel", new DialogInterface.OnClickListener() {public void onClick(DialogInterface dialog, int i) {dialog.cancel();}});pdialog.setOnCancelListener(new DialogInterface.OnCancelListener() {public void onCancel(DialogInterface dialog) {finish();}});pdialog.setCancelable(true);pdialog.setMax(100);pdialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);pdialog.show();}@Overrideprotected String doInBackground(String... params) {try{HttpClient client = new DefaultHttpClient();// params[0]代表连接的urlHttpGet get = new HttpGet(params[0]);HttpResponse response = client.execute(get);HttpEntity entity = response.getEntity();long length = entity.getContentLength();InputStream is = entity.getContent();String s = null;if(is != null) {ByteArrayOutputStream baos = new ByteArrayOutputStream();byte[] buf = new byte[128];int ch = -1;int count = 0;while((ch = is.read(buf)) != -1) {baos.write(buf, 0, ch);count += ch;if(length > 0) {// 如果知道响应的长度,调用publishProgress()更新进度publishProgress((int) ((count / (float) length) * 100));}// 让线程休眠100msThread.sleep(100);}s = new String(baos.toByteArray());              }// 返回结果return s;} catch(Exception e) {e.printStackTrace();}return null;}@Overrideprotected void onCancelled() {super.onCancelled();}@Overrideprotected void onPostExecute(String result) {// 返回HTML页面的内容message.setText(result);pdialog.dismiss(); }@Overrideprotected void onPreExecute() {// 任务启动,可以在这里显示一个对话框,这里简单处理message.setText(R.string.task_started);}@Overrideprotected void onProgressUpdate(Integer... values) {// 更新进度System.out.println(""+values[0]);message.setText(""+values[0]);pdialog.setProgress(values[0]);}}}

这篇关于使用AsyncTask从网上获取一个网页,在一个TextView中将其源代码显示出来的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/996308

相关文章

使用Dify访问mysql数据库详细代码示例

《使用Dify访问mysql数据库详细代码示例》:本文主要介绍使用Dify访问mysql数据库的相关资料,并详细讲解了如何在本地搭建数据库访问服务,使用ngrok暴露到公网,并创建知识库、数据库访... 1、在本地搭建数据库访问的服务,并使用ngrok暴露到公网。#sql_tools.pyfrom

使用mvn deploy命令上传jar包的实现

《使用mvndeploy命令上传jar包的实现》本文介绍了使用mvndeploy:deploy-file命令将本地仓库中的JAR包重新发布到Maven私服,文中通过示例代码介绍的非常详细,对大家的学... 目录一、背景二、环境三、配置nexus上传账号四、执行deploy命令上传包1. 首先需要把本地仓中要

Spring Cloud之注册中心Nacos的使用详解

《SpringCloud之注册中心Nacos的使用详解》本文介绍SpringCloudAlibaba中的Nacos组件,对比了Nacos与Eureka的区别,展示了如何在项目中引入SpringClo... 目录Naacos服务注册/服务发现引⼊Spring Cloud Alibaba依赖引入Naco编程s依

Java springBoot初步使用websocket的代码示例

《JavaspringBoot初步使用websocket的代码示例》:本文主要介绍JavaspringBoot初步使用websocket的相关资料,WebSocket是一种实现实时双向通信的协... 目录一、什么是websocket二、依赖坐标地址1.springBoot父级依赖2.springBoot依赖

Java使用Mail构建邮件功能的完整指南

《Java使用Mail构建邮件功能的完整指南》JavaMailAPI是一个功能强大的工具,它可以帮助开发者轻松实现邮件的发送与接收功能,本文将介绍如何使用JavaMail发送和接收邮件,希望对大家有所... 目录1、简述2、主要特点3、发送样例3.1 发送纯文本邮件3.2 发送 html 邮件3.3 发送带

Linux虚拟机不显示IP地址的解决方法(亲测有效)

《Linux虚拟机不显示IP地址的解决方法(亲测有效)》本文主要介绍了通过VMware新装的Linux系统没有IP地址的解决方法,主要步骤包括:关闭虚拟机、打开VM虚拟网络编辑器、还原VMnet8或修... 目录前言步骤0.问题情况1.关闭虚拟机2.China编程打开VM虚拟网络编辑器3.1 方法一:点击还原VM

Win32下C++实现快速获取硬盘分区信息

《Win32下C++实现快速获取硬盘分区信息》这篇文章主要为大家详细介绍了Win32下C++如何实现快速获取硬盘分区信息,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 实现代码CDiskDriveUtils.h#pragma once #include <wtypesbase

使用DeepSeek搭建个人知识库(在笔记本电脑上)

《使用DeepSeek搭建个人知识库(在笔记本电脑上)》本文介绍了如何在笔记本电脑上使用DeepSeek和开源工具搭建个人知识库,通过安装DeepSeek和RAGFlow,并使用CherryStudi... 目录部署环境软件清单安装DeepSeek安装Cherry Studio安装RAGFlow设置知识库总

Python FastAPI入门安装使用

《PythonFastAPI入门安装使用》FastAPI是一个现代、快速的PythonWeb框架,用于构建API,它基于Python3.6+的类型提示特性,使得代码更加简洁且易于绶护,这篇文章主要介... 目录第一节:FastAPI入门一、FastAPI框架介绍什么是ASGI服务(WSGI)二、FastAP

Spring-AOP-ProceedingJoinPoint的使用详解

《Spring-AOP-ProceedingJoinPoint的使用详解》:本文主要介绍Spring-AOP-ProceedingJoinPoint的使用方式,具有很好的参考价值,希望对大家有所帮... 目录ProceedingJoinPoijsnt简介获取环绕通知方法的相关信息1.proceed()2.g