package com.testasyntextview;
/**
* 把获取的线程写到方法中(比较好)
*/
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.Html;
import android.text.Spanned;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class MethodTestAsynTextViewActivity extends Activity {
private TextView textView1;
private Button button1;
private Context context;
private ProgressDialog progressDialog;
private Spanned html;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
context = this;
textView1 = (TextView) findViewById(R.id.textView1);
button1 = (Button) findViewById(R.id.button1);
button1.setOnClickListener(l);
}
private OnClickListener l = new OnClickListener() {
@Override
public void onClick(View v) {
progressDialog = ProgressDialog.show(context, "获取数据中", "等待");
getHtmlDate();
}
};
private void getHtmlDate() {// 获取数据,把线程写入了其中
new Thread() {
public void run() {
Message msg = myHandler.obtainMessage();
try {
html = HttpUtil.fromHtml(HttpUtil
.getHtml("http://wap.sina.com"));
msg.what = 0;
} catch (Exception e) {
e.printStackTrace();
msg.what = 1;
}
myHandler.sendMessage(msg);
}
}.start();
}
Handler myHandler = new Handler() {
public void handleMessage(Message msg) {
switch (msg.what) {
case 0:
textView1.setText(html);
progressDialog.dismiss();
break;
case 1:
textView1.setText("当前无数据");
progressDialog.dismiss();
break;
}
super.handleMessage(msg);
}
};
}
package com.testasyntextview;
/**
* 使用AsyncTask类
*/
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.Html;
import android.text.Spanned;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class TestAsynTextViewActivity extends Activity {
private TextView textView1;
private Button button1;
private Context context;
private ProgressDialog progressDialog;
private Spanned html;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
context = this;
progressDialog = new ProgressDialog(context);
progressDialog.setTitle("进度条");
progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
textView1 = (TextView) findViewById(R.id.textView1);
button1 = (Button) findViewById(R.id.button1);
button1.setOnClickListener(l);
}
private OnClickListener l = new OnClickListener() {
@Override
public void onClick(View v) {
new InitTask().execute("http://wap.sina.com",
"http://wap.baidu.com");
}
};
private void getHtmlDate(String url) {// 获取数据,把线程写入了其中
try {
html = HttpUtil.fromHtml(HttpUtil.getHtml(url));
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* When an asynchronous task is executed, the task goes through 4 steps:
*
* onPreExecute(),
* invoked on the UI thread immediately after the task is
* executed. This step is normally used to setup the task, for instance by
* showing a progress bar in the user interface.
*
* doInBackground(Params...),
* invoked on the background thread immediately after onPreExecute()
* finishes executing. This step is used to perform background computation
* that can take a long time. The parameters of the asynchronous task are
* passed to this step. The result of the computation must be returned by
* this step and will be passed back to the last step. This step can also
* use
*
* publishProgress(Progress...) to publish one or more units of
* progress. These values are published on the UI thread, in the
*
* onProgressUpdate(Progress...) step. onProgressUpdate(Progress...),
* invoked on the UI thread after a call to publishProgress(Progress...).
* The timing of the execution is undefined. This method is used to display
* any form of progress in the user interface while the background
* computation is still executing. For instance, it can be used to animate a
* progress bar or show logs in a text field. onPostExecute(Result), invoked
* on the UI thread after the background computation finishes. The result of
* the background computation is passed to this step as a parameter.
*/
class InitTask extends AsyncTask<String, Integer, Long> {
protected void onPreExecute() {
progressDialog.show();
super.onPreExecute();
}
protected Long doInBackground(String... params) {// Long是结果 String 是入口参数
getHtmlDate(params[0]);// 可以运行两个任务
publishProgress(50);// 发送进度50%
getHtmlDate(params[1]);
publishProgress(100);// 发送进度100%
return (long) 1;
}
@Override
protected void onProgressUpdate(Integer... progress) {
progressDialog.setProgress(progress[0]);// 设置进度
super.onProgressUpdate(progress);
Log.e("测试", progress[0] + "");
}
@Override
protected void onPostExecute(Long result) {
setTitle(result + "测试");
textView1.setText(html);
progressDialog.dismiss();
super.onPostExecute(result);
}
}
}
package com.testasyntextview;
/**
* 使用Runable
*/
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.Html;
import android.text.Spanned;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class RunableTestAsynTextViewActivity extends Activity {
private TextView textView1;
private Button button1;
private Context context;
private ProgressDialog progressDialog;
private Spanned html;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
context = this;
textView1 = (TextView) findViewById(R.id.textView1);
button1 = (Button) findViewById(R.id.button1);
button1.setOnClickListener(l);
}
private OnClickListener l = new OnClickListener() {
@Override
public void onClick(View v) {
progressDialog = ProgressDialog.show(context, "获取数据中", "等待");
new Thread(new ThreadDemo()).start();
}
};
private void getHtmlDate() {
try {
html = HttpUtil.fromHtml(HttpUtil.getHtml("http://wap.sina.com"));
} catch (Exception e) {
e.printStackTrace();
}
}
class ThreadDemo implements Runnable {//一个runable
public void run() {
getHtmlDate();
myHandler.sendEmptyMessage(0);
}
}
Handler myHandler = new Handler() {
public void handleMessage(Message msg) {
switch (msg.what) {
case 0:
textView1.setText(html);
progressDialog.dismiss();
break;
case 1:
textView1.setText("当前无数据");
progressDialog.dismiss();
break;
}
super.handleMessage(msg);
}
};
}
package com.testasyntextview;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import android.graphics.drawable.Drawable;
import android.text.Html;
import android.text.Spanned;
public class HttpUtil {
public static String getHtml(String path) throws Exception {
URL url = new URL(path);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
conn.setConnectTimeout(5 * 1000);
InputStream inStream = conn.getInputStream();// 通过输入流获取html数据
byte[] data = readInputStream(inStream);// 得到html的二进制数据
String html = new String(data, "utf-8");
return html;
}
public static byte[] readInputStream(InputStream inStream) throws Exception {
ByteArrayOutputStream outStream = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int len = 0;
while ((len = inStream.read(buffer)) != -1) {
outStream.write(buffer, 0, len);
}
inStream.close();
return outStream.toByteArray();
}
public static Spanned fromHtml(String html) {
Spanned sp = Html.fromHtml(html, new Html.ImageGetter() {
@Override
public Drawable getDrawable(String source) {
InputStream is = null;
try {
is = (InputStream) new URL(source).getContent();
Drawable d = Drawable.createFromStream(is, "src");
d.setBounds(0, 0, d.getIntrinsicWidth(),
d.getIntrinsicHeight());
is.close();
return d;
} catch (Exception e) {
return null;
}
}
}, null);
return sp;
}
}
分享到:
相关推荐
Echarts基础折线图源代码,对于数据源的读取有两种方式:直接定义数据源和异步加载数据,异步加载数据以读取json/js数据为主。
Echarts高级进阶教程(2):appendData异步加载大数据量分片加载数据和增量渲染的解决方案
最近项目中有一个比较大型的树节点加载,网上面也看过一些解决方案,感觉都不是很好,也有很多误区,比如单击节点时加载子...ztree fileter方法是在每次展开时都会执行,所以根据不同的请求达到异步加载子节点的需求。
ztree实现异步加载,使用java语言
unity通过协程实现异步下载数据并加载
ECharts地图主要用于地理区域数据的可视化,展示不同区域的数据分布信息,通过本文给大家介绍如何使用PHP+jQuery+MySQL实现异步加载ECharts地图数据,需要的朋友参考下吧
该程序使用VS.net C#开发环境中自带的webbrowser控件的方法,实现了抓取新闻评论页面网民跟帖这类异步加载数据(在浏览器的“查看源码”打开的网页源码中找不到的动态加载的数据)的功能。
异步分页数据,tomcat ,jquery实现,无刷新加载数据
ListView的异步加载图片并缓存,可以实现异步加载数据,异步加载图片,缓存图片,并且实现退出删除图片缓存
Flutter通过FutureBuilder实现异步请求网络数据并显示加载中Demo,详情请看博客Flutter中的异步(Future、async、await、FutureBuilder)和 网络请求:https://yuzhiqiang.blog.csdn.net/article/details/89155870
vue写的后台管理,需要将表格数据绘制成图表(折线图,柱状图),图表数据都是通过接口请求回来的。这篇文章主要介绍了vue+echarts 动态绘制图表及异步加载数据的相关知识,需要的朋友可以参考下
android平台实现异步加载网络数据的实例,供学习
实现非主线程异步请求数据, 只需要传入参数值即可!
本文实例讲述了Android开发实现ListView异步加载数据的方法。分享给大家供大家参考,具体如下: 1.主Activity public class MainActivity extends Activity { private ListView listView; private ArrayList...
找了一下官方文档,看了EASYUI的异步加载,弄了我两三个小时,死活都不出数据
listview异步加载图片,滚动条滚动到地步自动调用后台异步加载数据
本文给大家分享的是Jquery+dataTable插件来实现异步加载数据的示例代码,非常实用,有需要的小伙伴可以参考下
采用Jquery 及 Javascript自定义了一个TabMenuComponent对象, 能够点击左右按钮实现平滑左右移动1个或多个Tab菜单,一次移动标签个数、移动速度、正反方向移动可以定制,点击Tab异步加载内容。