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

public class MainActivity extends Activity {
ListView listview;
List<News> 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<News>();
}
/**
* 设置数据
*/
private void setData() {
MyAsyncTask asyncTask = new MyAsyncTask();
asyncTask.execute();
}
/**
* 异步任务
* @author user
*负责请求网络资源数据和解析数据,请求图片
*/
class MyAsyncTask extends AsyncTask<String, String, List<News>>{
@Override
protected void onPreExecute() {
// TODO Auto-generated method stub
super.onPreExecute();
}
@Override
protected List<News> 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<News> 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<News> parseJsonArray(String jsonStr) {
try {
//定义一个List集合用来存放news
List<News> list = new ArrayList<News>();
//把字符串转换成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<News> list;



public MyListAdapter(Context context, List<News> 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;
}


}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值