本例要实现:以前的例子先锋系统加入数据到数据库,再取出来显示在android应用程序中。
1.添加产品信息,包括文字和图片。
通过链接 http://192.168.0.101:8080/xianfengProject/servlet/JsonAction 就可以访问到Json数据。
2. 通过异步任务和Handler取出数据
1)定义数据的链接,以后如果要改只要改一个地方就可以了
1.添加产品信息,包括文字和图片。
通过链接 http://192.168.0.101:8080/xianfengProject/servlet/JsonAction 就可以访问到Json数据。
2. 通过异步任务和Handler取出数据
1)定义数据的链接,以后如果要改只要改一个地方就可以了
public class CommonUrl{
//访问服务器产品表的链接
public static final String PRODUCT_URL=" http://192.168.0.101:8080/xianfengProject/servlet/JsonAction?action_flag=more";
//产品图片的链接
public static final String PRODUCT_IMG="http://192.168.0.101:8080/xianfengProject/update/";
}
2)MainActivitypublic class MainActivity extends Activity{
private ListView listView;
private ProgressDialog dialog;
private MyAdapter adapter;
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView=(ListView)this.findViewById(R.id.listView);
dialog=new ProgressDialog(this);
adapter=new MyAdapter(this);//构建适配器
dialog.setTitle("提示");
dialog.setMessage("正在下载,请稍后...");
new MyTask().execute(CommonUrl.PRODUCT_URL);
}
public class MyTask extends AsyncTask<String,Void,List<Map<String,Object>>>{
protected List<Map<String,Object>> doInBackground(String ... params){
List<Map<String,Object>> list=new Array List<Map<String,Object>>();
//连接网络,获取json数据并且进行解析
try{
HttpClient httpClient=new DefaultHttpClient();
HttpPost httpPost=new HttpPost(params[0]);
HttpResponse httpResponse=httpClient.execute(httpPost);
if(httpResponse.getStatusLine().getStatusCode()==200){
String jsonString=EntityUtils.toString(httpResponse.getEntity(),"utf-8");
JSONObject jsonObject=new JSONObject(jsonString);
JSONArray jsonArray=jsonObject.getJSONArray("product");
for(int i=0;i<jsonArray.length();i++){
JSONObject jsonObject2=jsonArray.getJSONObject(i);
Map<String,Object> map=new HashMap<String,Object>();
iterator<String> iterator=jsonObject2.keys();
while(iterator.hasNext()){
String key=iterator.next();
Object value=jsonObject2.get(key);
map.put(key,value);
}
list.add(map);
}
}
}catch(Exception e){
e.printStackTrace();
}
}
protected void onPreExecute(){
super.onPreExecute();
dialog.show();
}
protected void onPostExcute(List<Map<String,Object>> result){
super.onPostExcute(result);
adapter.setData(result);
listView.setAdapter(adapter);
adapter.notifyDataSetChanged();
dialog.dismiss();
}
}
public class MyAdapter extends BaseAdapter{
private content content;
private LayoutInflater layoutInflater;
private List<Map<String,Object>> list=null;
public MyAdapter(Content content){
this.content=content;
layoutInflater=layoutInflater.from(content);
}
public void setData(List<Map<String,Object>> list){
this.list=list;
}
public int getCount(){
return list.size();
}
public Object getItem(int position){
return list.get(position);
}
public long getItemId(int position){
return position;
}
public View getView(int position,View convertView,ViewGroup parent){
View view=null;
if(convertView==null){
view=layoutInflater.inflate(R.layout.item,null);
}else{
view=convertView;
}
TextView name=(TextView)view.findViewById(R.id.textview1);
TextView address=(TextView)view.findViewById(R.id.textview2);
TextView price=(TextView)view.findViewById(R.id.textview3);
name.setText(list.get(position).get("proname").toString);
address.setText(list.get(position).get("address").toString);
price.setText(list.get(position).get("price").toString);
return view;
}
}
}