- AsynctTask内部封装了线程池和Handler
- 可以再主线程更改UI
- AsynctTask用于初始化操作的方法是___onPreExcute____
- doInBackground————运行在子线程的方法,返回一个值
- onPostExecute————AsynctTask任务完成后执行的方法
定义方法创建AsynctTask在onPostExecute更改UI
private String path = "https://suggest.taobao.com/sug?code=utf-8&q=%E6%89%8B%E6%9C%BA";
private void getInfo(){
@SuppressLint("StaticFieldLeak") AsyncTask<String, Void, List<List<String>>> stringVoidListAsyncTask = new AsyncTask<String, Void, List<List<String>>>() {
@Override
protected List<List<String>> doInBackground(String... strings) {
//打开URL地址
try {
URL url = new URL(path);
//打开链接
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setReadTimeout(5000);
connection.setConnectTimeout(5000);
int responseCode = connection.getResponseCode();
if(responseCode == 200){
String stream = stream(connection.getInputStream());
Gson gson = new Gson();
PhoneBean phoneBean = gson.fromJson(stream, PhoneBean.class);
return phoneBean.getResult();
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(List<List<String>> lists) {
super.onPostExecute(lists);
list = lists;
adapter.setList(lists);
}
}.execute(path,"");
}
private String stream(InputStream is) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(is));
StringBuilder builder = new StringBuilder();
for (String tmp = br.readLine();tmp != null;tmp = br.readLine()){
builder.append(tmp);
}
return builder.toString();
}
Adapter适配器
private List<List<String>> list;
private Context context;
public PhoneAdapter(Context context) {
this.context = context;
list = new ArrayList<>();
}
public void setList(List<List<String>> list) {
this.list = list;
notifyDataSetChanged();
}
@Override
public int getCount() {
return list.size();
}
@Override
public Object getItem(int position) {
return list.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if(convertView == null){
convertView=LayoutInflater.from(context).inflate(R.layout.item,parent,false);
holder=new ViewHolder();
holder.text=convertView.findViewById(R.id.item_textview);
convertView.setTag(holder);
}else {
holder = (ViewHolder) convertView.getTag();
}
holder.text.setText(list.get(position).get(0));
return convertView;
}
class ViewHolder{
TextView text;
}
点击按钮后判断是否有网络
adapter = new PhoneAdapter(this);
listView.setAdapter(adapter);
but.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(!panduan()){
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setIcon(R.drawable.ic_launcher_background);
builder.setTitle("没有网络请链接网络");
builder.setPositiveButton("确认", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
builder.show();
}else{
getInfo();
}
}
});
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(MainActivity.this,list.get(position).get(0),Toast.LENGTH_SHORT).show();
}
});
}
private boolean panduan(){
ConnectivityManager manager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetworkInfo = manager.getActiveNetworkInfo();
return activeNetworkInfo != null &&activeNetworkInfo.isAvailable();
}