提示:因为服务器端和Android端项目都是用Java语言实现的,所以一些组件可以共用,JavaBean便是其中之一。此时我们在搭建Android端项目的时候,完全可以将服务器端项目中JavaBean拿来用。
package com.jph.gj.activity;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.jph.gj.R;
import com.jph.gj.service.GetAndParseJson;
import com.jph.model.News;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.app.Activity;
import android.widget.ListView;
import android.widget.SimpleAdapter;
/**
* 获取网络上的Json
* @author jph
* Date:2014.09.26
*/
public class MainActivity extends Activity {
private List<News>listNews;
private ListView list;
Handler mHandler=new Handler(){
@Override
public void handleMessage(Message msg) {
// TODO Auto-generated method stub
switch (msg.what) {
case GetAndParseJson.PARSESUCCWSS:
listNews=(List<News>) msg.obj;
initData();
break;
}
super.handleMessage(msg);
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
list=(ListView)findViewById(R.id.list);
GetAndParseJson getAndParseJson=new GetAndParseJson(mHandler);
getAndParseJson.getJsonFromInternet();
}
/**
* 将解析后的xml填充到ListView
*/
protected void initData() {
// TODO Auto-generated method stub
List<Map<String, Object>>items=new ArrayList<Map<String,Object>>();
for (News
news:listNews) {
Map<String, Object>item=new HashMap<String, Object>();
item.put("id", news.getId());
item.put("title", news.getTitle());
item.put("time", convertDate(news.getPublishDate()));
items.add(item);
}
SimpleAdapter adapter=new SimpleAdapter(this, items, R.layout.line, new String[]
{"id","title","time"}, new int[]{R.id.tvId,R.id.tvTitle,R.id.tvTime});
list.setAdapter(adapter);
}
private String convertDate(Date publishDate) {
// TODO Auto-generated method stub
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH-mm-ss");
return sdf.format(publishDate);
}
}