欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

AsyncTask_初级应用

程序员文章站 2022-04-25 15:53:55
...

package com.example.day3;

import android.graphics.Bitmap;
import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;

public class AsyncTaskActivity extends AppCompatActivity {

private TextView info;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_async_task);

	
    info = findViewById(R.id.info);
    //
    //子线程处理数据   主线程处理ui
    //Thread + Handler
    //AsyncTask   -> 线程池(2)
    requestData();
}

private String apiUrl = "https://tcc.taobao.com/cc/json/mobile_tel_segment.htm?tel=18679906337";

private void requestData() {
    //创建AcyncTask,实现doInBackground方法
    //调execute执行 -> 会导致doInBackground执行
    //doInBackground执行完成,返回结果  -> 会导致onPostExecute执行
    //execute()  ->   doInBackground()子线程中  ->  onPostExecute()主线程中
    //三个泛型
    //Params   执行doInBackground的方法参数类型,调用execute()时传递过去
    //Progress
    //Result  执行doInBackground的返回类型
    new AsyncTask<String, Void, String>() {//void
        //实现doInBackground方法
        @Override
        protected String doInBackground(String[] objects) {//在子线程(后台)执行
            String result = "";

            try {
                URL url = new URL(objects[0]);
                HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
                urlConnection.setRequestMethod("GET");
                urlConnection.setConnectTimeout(5000);
                urlConnection.setReadTimeout(5000);
                int responseCode = urlConnection.getResponseCode();
                if(responseCode == 200) {
                    result = stream2String(urlConnection.getInputStream(), "gbk");
                }
            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }

            return result;
        }

        //当后台处理完成返回执行    在主线程中回调
        @Override
        protected void onPostExecute(String s) {
            info.setText(s);
        }
    }.execute(apiUrl);
}

private String stream2String(InputStream inputStream, String charsetName) throws IOException {
    StringBuilder stringBuilder = new StringBuilder();

    InputStreamReader isr = new InputStreamReader(inputStream, charsetName);

    BufferedReader br = new BufferedReader(isr);

    //读取一行
    //一行文本   null
    for (String tmp = br.readLine(); tmp != null; tmp = br.readLine()) {
        stringBuilder.append(tmp);
    }

    /*String tmp = br.readLine();
    while (tmp != null) {
        stringBuilder.append(tmp);
        tmp = br.readLine();
    }*/

    return stringBuilder.toString();
}

}

相关标签: AsyncTask