AsyncTask异步;ListView列表展示数据
AsyncTask异步ListView列表展示数据
libs文件夹下加入jar包(导包): gson
=======================================
添加权限:
网络请求权限:INTERNET
<uses-permission android:name="android.permission.INTERNET"/>
网络判断权限:ACCESS_NETWORK_STATE
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
===========================================================
布局:
<ListView
android:id="@+id/list_view"
android:layout_width="match_parent"
android:layout_height="match_parent" />
==========================================================================
json解析类:https://api.tianapi.com/wxnew/?key=32b83b189e5e5e520db1612a83d82033&num=10
============================================================
主要方法:
import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ListView;
import com.google.gson.Gson;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private ListView listview;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listview = findViewById(R.id.list_view);
MyTask myTask = new MyTask();
myTask.execute("https://api.tianapi.com/wxnew/?key=32b83b189e5e5e520db1612a83d82033&num=10");
}
class MyTask extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... strings) {
StringBuilder builder = new StringBuilder();
try {
URL url = new URL(strings[0]);
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setConnectTimeout(5000);
urlConnection.setReadTimeout(5000);
urlConnection.setRequestMethod("GET");
if (urlConnection.getResponseCode() == 200) {
InputStream inputStream = urlConnection.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
String str;
while ((str = reader.readLine()) != null) {
builder.append(str);
}
}
} catch (Exception e) {
e.printStackTrace();
}
return builder.toString();
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
Gson gson = new Gson();
Result result = gson.fromJson(s, Result.class);
List<Result.NewslistBean> data = result.getNewslist();
MyAdapter myAdapter = new MyAdapter(data, MainActivity.this);
listview.setAdapter(myAdapter);
}
}
}
=========================================================
MyAdapter 适配器方法:
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.List;
class MyAdapter extends BaseAdapter {
private List<Result.NewslistBean> data;
private Context context;
public MyAdapter(List<Result.NewslistBean> data, Context context) {
this.data = data;
this.context = context;
}
@Override
public int getCount() {
return data.size();
}
@Override
public Object getItem(int i) {
return data.get(i);
}
@Override
public long getItemId(int i) {
return i;
}
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
ViewHolder holder;
if (view == null) {
view= LayoutInflater.from(context).inflate(R.layout.item,null);
holder = new ViewHolder();
holder.name=view.findViewById(R.id.name);
holder.img=view.findViewById(R.id.img);
view.setTag(holder);
}else {
holder = (ViewHolder) view.getTag();
}
holder.name.setText(data.get(i).getDescription());
new MyTask(holder.img).execute(data.get(i).getPicUrl());
return view;
}
class ViewHolder {
public TextView name;
public ImageView img;
}
class MyTask extends AsyncTask<String, Void, Bitmap> {
private ImageView img;
public MyTask(ImageView img) {
this.img = img;
}
@Override
protected Bitmap doInBackground(String... strings) {
Bitmap bitmap = null;
try {
URL url = new URL(strings[0]);
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setConnectTimeout(5000);
urlConnection.setReadTimeout(5000);
urlConnection.setRequestMethod("GET");
if (urlConnection.getResponseCode() == 200) {
InputStream inputStream = urlConnection.getInputStream();
bitmap = BitmapFactory.decodeStream(inputStream);
}
} catch (Exception e) {
e.printStackTrace();
}
return bitmap;
}
@Override
protected void onPostExecute(Bitmap bitmap) {
super.onPostExecute(bitmap);
img.setImageBitmap(bitmap);
}
}
}
-------------------------------------------
R.layout.item
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/img"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_margin="10dp"
tools:layout_editor_absoluteX="16dp"
tools:layout_editor_absoluteY="16dp" />
<TextView
android:id="@+id/name"
android:layout_width="200dp"
android:layout_height="50dp"
android:layout_alignTop="@+id/img"
android:layout_toEndOf="@+id/img"
android:layout_toRightOf="@+id/img"
tools:layout_editor_absoluteX="80dp"
tools:layout_editor_absoluteY="16dp" />
</RelativeLayout>
==================================================================