Android开发笔记之接口数据通过listView显示

public class MainActivity extends Activity {
ListView listview;
List list;
MyListAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
setData();
}
/**
* 初始化
*/
private void initView() {
listview = (ListView) findViewById(R.id.main_list_view);
list= new ArrayList();
}
/**
* 设置数据
*/
private void setData() {
MyAsyncTask asyncTask = new MyAsyncTask();
asyncTask.execute();
}
/**
* 异步任务
* @author user
*负责请求网络资源数据和解析数据,请求图片
*/
class MyAsyncTask extends AsyncTask>{
@Override
protected void onPreExecute() {
// TODO Auto-generated method stub
super.onPreExecute();
}
@Override
protected List doInBackground(String... params) {
String result = getDataByGet("输入你自己找到的接口,通过次接口获得数据");
//把json解析成list集合的形式
list= parseJsonArray(result);
for (int i = 0; i < list.size(); i++) {
list.get(i).bm=getImageByGet("输入你找到接口中的图片地址" + list.get(i).image + ".jpg");
}
return list;
}
@Override
protected void onPostExecute(List result) {
if(result!=null){
adapter = new MyListAdapter(MainActivity.this,list);
listview.setAdapter(adapter);
}
}
}
/**
* 1.从网络接口获得数据
* 2.通过get的方式从网络接口上请求数据
* @return 返回一个包含数据的result
* @param path 网络地址,需要传入的参数
*/
private String getDataByGet(String path) {
try {
//先创建一个URL的对象
URL url = new URL(path);
//创建一个HttpURLConnection对象
//通过url.openConnection打开链接,网络请求耗时链接需要放到线程里面来处理
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.setConnectTimeout(8000);
if (connection.getResponseCode() == 200) {
InputStream inputStream = connection.getInputStream();
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int len = 0;
// inputStream.read(buffer)返回值为int类型
// 如果返回-1,代表输入流数据已经全部读取完毕
// 如果没读取完毕,返回的是 读取的内容长度
while ((len = inputStream.read(buffer)) != -1) {
outputStream.write(buffer, 0, len);
}
String result = new String(outputStream.toByteArray());
return result;
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
/**
* 通过get的方式从网路上请求图片数据
* @param path 包含数据的网络地址
* @return 返回包含图片的bitmap值
*/
private Bitmap getImageByGet(String path){
try {
URL url = new URL(path);
HttpURLConnection connection= (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
if(connection.getResponseCode()==200){
InputStream inputStream=connection.getInputStream();
Bitmap bitmap=BitmapFactory.decodeStream(inputStream);
return bitmap;
}else{
return null;
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/**
* 解析获得到的数据

* @param jsonStr需要解析的数据(从网络得到的数据)
* @return
*/
private List parseJsonArray(String jsonStr) {
try {
//定义一个List集合用来存放news
List list = new ArrayList();
//把字符串转换成jsonObject对象,通过new JSONObject(jsonStr);
JSONObject jsonObject = new JSONObject(jsonStr);
//判断返回码是否是200 如果是的话就解析,不是的话,就不解析,返回空
if (jsonObject.getInt("resultcode") != 200) {
return null;
}
//从jsonObject对象中拿到一个数组-----通过jsonObject.getJSONArray("data");此处data为数据的标签值
JSONArray jsonArray = jsonObject.getJSONArray("data");
//通过循环来拿到每个jsonArray中的数据
for (int i = 0; i < jsonArray.length(); i++) {
News news = new News();
JSONObject object = jsonArray.getJSONObject(i);
news.title = object.getString("title");
news.author = object.getString("author");
news.content = object.getString("content");
news.date = object.getString("date");
news.image=object.getString("image");
list.add(news);
}
return list;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}


}

-------------------------------------------------------------------

适配器的配置如下

public class MyListAdapter extends BaseAdapter {
Context context;
List list;



public MyListAdapter(Context context, List list) {
super();
this.context = context;
this.list = list;
}


@Override
public int getCount() {
// TODO Auto-generated method stub
return list.size();
}


@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return null;
}


@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}


@Override
public View getView(int position, View convertView, ViewGroup parent) {
convertView = View.inflate(context, R.layout.list_item, null);
TextView tvTitle = (TextView) convertView.findViewById(R.id.list_item_title);
TextView tvAuthor = (TextView) convertView.findViewById(R.id.list_item_author);
TextView tvData = (TextView) convertView.findViewById(R.id.list_item_data);
TextView tvcontent = (TextView) convertView.findViewById(R.id.list_item_content);
ImageView iv = (ImageView) convertView.findViewById(R.id.list_item_ImageView);
tvTitle.setText(list.get(position).title);
tvAuthor.setText(list.get(position).author);
tvData.setText(list.get(position).date);
tvcontent.setText(list.get(position).content);
iv.setImageBitmap(list.get(position).bm);
return convertView;
}


}

你可能感兴趣的:(Android)