AsyncTask
程序员文章站
2022-05-04 18:08:06
...
AsyncTask
package com.example.asyncdemo2;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import com.example.bean.MyAdapter;
import com.google.gson.Gson;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private Button btn;
private TextView tv;
private List<List<String>> data=new ArrayList<>();
private String murl="https://suggest.taobao.com/sug?code=utf-8&q=%E6%89%8B%E6%9C%BA";
private ListView lv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
}
private void initView() {
btn = (Button) findViewById(R.id.btn);
tv = (TextView) findViewById(R.id.tv);
lv = (ListView)findViewById(R.id.lv);
btn.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btn:
new MyTask().execute(murl);
break;
}
}
class MyTask extends AsyncTask<String,Void,String>{
@Override
protected String doInBackground(String... strings) {
try {
String json = HttpUtils.get(murl);
return json;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
tv.setText(s);
Gson gson=new Gson();
result result = gson.fromJson(s, result.class);
data = result.getResult();
Log.d("onPostExecute: zzz",data+"");
MyAdapter adapter=new MyAdapter(MainActivity.this,data);
lv.setAdapter(adapter);
}
}
}
上一篇: Quick_Python_1
下一篇: 个人记录之8266数据处理
推荐阅读
-
基于AsyncTask进行异步下载(service后台服务执行并可查看下载进度)
-
Android的线程通信:消息机制原理(Message,Handler,MessageQueue,Looper),异步任务AsyncTask,使用JSON
-
Android中异步类AsyncTask用法总结
-
android使用AsyncTask实现多线程下载实例
-
Android中使用AsyncTask做下载进度条实例代码
-
详解Android中AsyncTask机制
-
android使用AsyncTask实现多线程下载实例
-
Android中异步类AsyncTask用法总结
-
Android使用AsyncTask下载图片并显示进度条功能
-
Android AsyncTask的优缺点详解