Asynctask异步处理类的使用

主类
package com.example.day08_asynctask;

import java.util.concurrent.ExecutionException;

import android.os.Bundle;
import android.R.integer;
import android.app.Activity;
import android.view.Menu;
import android.widget.ProgressBar;
import android.widget.TextView;

public class MainActivity extends Activity {
	TextView tv;
	TextView tv2;
	ProgressBar pb;
	String key = "3ac9f31ff66b9746539472887b3799c3";
	// 通过get请求时的接口地址
	String get_path = "http://web.juhe.cn:8080/constellation/getAll?consName=狮子座&type=today&key="
			+ key;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		tv = (TextView) findViewById(R.id.textView1);
		tv2 = (TextView) findViewById(R.id.textView2);
		pb = (ProgressBar) findViewById(R.id.progressBar1);
		// 主页提供控件,和联网地址
		System.out.println("new 出对象");
		MyAsynctask myAsynctask = new MyAsynctask(tv, pb);
		System.out.println("执行extcte方法");
		// 执行完,这里先得到执行的结果,类中在得到
		myAsynctask.execute(get_path);
		try {
			String string = myAsynctask.get();
			System.out.println("主线程得到线程结束的数据:" + string);
			tv2.setText("主线程的设置" + string);
		} catch (InterruptedException e) {
			e.printStackTrace();
		} catch (ExecutionException e) {
			e.printStackTrace();
		}
	}

}

线程类


package com.example.day08_asynctask;

import java.io.BufferedReader;
import java.io.IOException;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;

import android.os.AsyncTask;
import android.os.SystemClock;
import android.widget.ProgressBar;
import android.widget.TextView;
// 三个参数,1.线程执行前传入的数据类型 params 2.线程中更新的数据类型 3.线程执行后的结果类型
public class MyAsynctask extends AsyncTask<String, Integer, String> {

	TextView tv;
	ProgressBar pb;
	String path = "";
	private BufferedReader br;
	private StringBuffer sb;
	private String str;

	public MyAsynctask(TextView tv, ProgressBar pb) {
		super();
		this.tv = tv;
		this.pb = pb;
	}

	// 开启后台线程
	@Override
	protected String doInBackground(String... params) {
		System.out.println("正式开启线程,得到传来的数据" + params[0]);
		HttpClient httpClient = new DefaultHttpClient();
		HttpGet httpGet = new HttpGet(params[0]);
		try {
			HttpResponse httpResponse = httpClient.execute(httpGet);
			if (httpResponse.getStatusLine().getStatusCode() == 200) {
				HttpEntity entity = httpResponse.getEntity();
				str = EntityUtils.toString(entity);
				for (Integer i = 0; i < 100; i++) {
					SystemClock.sleep(200);
					publishProgress(i);
				}
			}
		} catch (ClientProtocolException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return str;
	}

	// 开始后台线程前执行
	@Override
	protected void onPreExecute() {

		System.out.println("准备开启线程");
		super.onPreExecute();
	}

	@Override
	protected void onPostExecute(String result) {
		System.out.println("子类线程得到结果数据:" + result);
		tv.setText("任务类设置的信息" + str);
		super.onPostExecute(result);
	}

	@Override
	protected void onProgressUpdate(Integer... values) {
		System.out.println("线程执行中得到传来的数据" + values[0]);
		tv.setText(values[0] + "%");
		super.onProgressUpdate(values);
	}

}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值