MainActivity.class:解析xml,用pull解析,还有定位
package com.example.map_new_two; import java.io.IOException; import java.io.InputStream; import java.util.ArrayList; import java.util.List; import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.client.ClientProtocolException; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpGet; import org.apache.http.impl.client.DefaultHttpClient; import org.apache.http.util.EntityUtils; import org.xmlpull.v1.XmlPullParser; import org.xmlpull.v1.XmlPullParserException; import org.xmlpull.v1.XmlPullParserFactory; import com.baidu.location.BDLocation; import com.baidu.location.BDLocationListener; import com.baidu.location.LocationClient; import com.baidu.location.LocationClientOption; import com.baidu.location.LocationClientOption.LocationMode; import android.os.Bundle; import android.app.Activity; import android.content.Intent; import android.util.Log; import android.view.Menu; import android.view.View; import android.view.View.OnClickListener; import android.widget.AdapterView; import android.widget.AdapterView.OnItemClickListener; import android.widget.ListView; import android.widget.TextView; public class MainActivity extends Activity { public LocationClient mLocationClient = null; public BDLocationListener myListener = new MyLocationListener(); private String url="http://apis.juhe.cn/goodbook/catalog?key=9d6ef8c31647a206e05fcaff70527182&dtype=xml"; private ListView list_view; private TextView text_one; private List<Item> list=new ArrayList<Item>(); protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); mLocationClient = new LocationClient(getApplicationContext()); //声明LocationClient类 mLocationClient.registerLocationListener( myListener ); //注册监听函数 setContentView(R.layout.activity_main); list_view=(ListView) findViewById(R.id.list_view); text_one=(TextView) findViewById(R.id.text_one); new Thread(){ public void run() { init(); } }.start(); initLocation(); mLocationClient.start(); list_view.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) { // TODO Auto-generated method stub Intent it=new Intent(getApplicationContext(),HomeActivity.class); it.putExtra("id",list.get(arg2).getId()); startActivity(it); } }); } private void init() { // TODO Auto-generated method stub HttpClient httpClient=new DefaultHttpClient(); HttpGet httpGet=new HttpGet(url); try { HttpResponse httpResponse=httpClient.execute(httpGet); int len=httpResponse.getStatusLine().getStatusCode(); if(len==200){ HttpEntity httpEntity=httpResponse.getEntity(); InputStream input=httpEntity.getContent(); XmlPullParserFactory factory=XmlPullParserFactory.newInstance(); XmlPullParser parser=factory.newPullParser(); parser.setInput(input, "utf-8"); int exenttype=parser.getEventType(); String name=""; Item item = null; while(exenttype!=XmlPullParser.END_DOCUMENT){ switch (exenttype) { case XmlPullParser.START_DOCUMENT: break; case XmlPullParser.START_TAG: name = parser.getName(); if("item".equals(name)){ item=new Item(); } break; case XmlPullParser.TEXT: String text=parser.getText(); if("id".equals(name)){ item.setId(text); }else if("catalog".equals(name)){ item.setCatalog(text); Log.i("TAG","fffffffrrrrrrrrrrrrrrrrrrrrrrrffff"+text); } break; case XmlPullParser.END_TAG: name = parser.getName(); if("item".equals(name)){ list.add(item); item=null; } name=""; break; case XmlPullParser.END_DOCUMENT: break; default: break; } exenttype=parser.next(); } } runOnUiThread(new Runnable() { @Override public void run() { // TODO Auto-generated method stub Mybase base=new Mybase(list, getApplicationContext()); list_view.setAdapter(base); } }); } catch (ClientProtocolException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (XmlPullParserException e) { // TODO Auto-generated catch block e.printStackTrace(); } }; private void initLocation(){ LocationClientOption option = new LocationClientOption(); option.setLocationMode(LocationMode.Hight_Accuracy );//可选,默认高精度,设置定位模式,高精度,低功耗,仅设备 option.setCoorType("bd09ll");//可选,默认gcj02,设置返回的定位结果坐标系 int span=1000; option.setScanSpan(span);//可选,默认0,即仅定位一次,设置发起定位请求的间隔需要大于等于1000ms才是有效的 option.setIsNeedAddress(true);//可选,设置是否需要地址信息,默认不需要 option.setOpenGps(true);//可选,默认false,设置是否使用gps option.setLocationNotify(true);//可选,默认false,设置是否当gps有效时按照1S1次频率输出GPS结果 option.setIsNeedLocationDescribe(true);//可选,默认false,设置是否需要位置语义化结果,可以在BDLocation.getLocationDescribe里得到,结果类似于“在北京天安门附近” option.setIsNeedLocationPoiList(true);//可选,默认false,设置是否需要POI结果,可以在BDLocation.getPoiList里得到 option.setIgnoreKillProcess(false);//可选,默认true,定位SDK内部是一个SERVICE,并放到了独立进程,设置是否在stop的时候杀死这个进程,默认不杀死 option.SetIgnoreCacheException(false);//可选,默认false,设置是否收集CRASH信息,默认收集 option.setEnableSimulateGps(false);//可选,默认false,设置是否需要过滤gps仿真结果,默认需要 mLocationClient.setLocOption(option); } public class MyLocationListener implements BDLocationListener { @Override public void onReceiveLocation(BDLocation location) { //Receive Location text_one.setText(location.getCity()); } } }
HomeActivity.class:点击查看详情,解析gson,上拉加载,下拉刷新
package com.example.map_new_two; import java.io.IOException; import java.util.ArrayList; import java.util.List; import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.client.ClientProtocolException; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpGet; import org.apache.http.impl.client.DefaultHttpClient; import org.apache.http.util.EntityUtils; import com.example.vo.DataDemo; import com.example.vo.Demo; import com.google.gson.Gson; import com.handmark.pulltorefresh.library.PullToRefreshBase; import com.handmark.pulltorefresh.library.PullToRefreshBase.Mode; import com.handmark.pulltorefresh.library.PullToRefreshBase.OnRefreshListener; import com.handmark.pulltorefresh.library.PullToRefreshGridView; import com.handmark.pulltorefresh.library.PullToRefreshBase.OnRefreshListener2; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.os.Handler; import android.text.format.DateUtils; import android.util.Log; import android.widget.GridView; public class HomeActivity extends Activity{ private PullToRefreshGridView grid_view; String id,str; int j=10; private Mybasetwo base; private List<Demo> data=new ArrayList<Demo>(); private String url="http://apis.juhe.cn/goodbook/query?key=9d6ef8c31647a206e05fcaff70527182&catalog_id=+"+id+"&rn=10&pn="; Handler han=new Handler(){ public void handleMessage(android.os.Message msg) { switch (msg.what) { case 1: msg.obj=str; Gson g=new Gson(); DataDemo dd=g.fromJson(str, DataDemo.class); data=dd.getResult().getData(); base=new Mybasetwo(data, getApplicationContext()); grid_view.setAdapter(base); break; case 2: msg.obj=str; Gson g1=new Gson(); DataDemo d2=g1.fromJson(str, DataDemo.class); if(d2!=null){ data=d2.getResult().getData(); base=new Mybasetwo(data, getApplicationContext()); grid_view.setAdapter(base); base.notifyDataSetChanged(); grid_view.onRefreshComplete(); } break; case 3: msg.obj=str; Gson g2=new Gson(); DataDemo d23=g2.fromJson(str, DataDemo.class); if(d23!=null){ List<Demo> dea=d23.getResult().getData(); data.addAll(dea); base.notifyDataSetChanged(); grid_view.onRefreshComplete(); } break; default: break; } }; }; protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.grid); Intent it=getIntent(); id=it.getStringExtra("id"); grid_view=(PullToRefreshGridView) findViewById(R.id.grid_view); grid_view.setMode(Mode.BOTH); new Thread(){ public void run() { init(); } }.start(); grid_view.setOnRefreshListener(new OnRefreshListener2<GridView>() { @Override public void onPullDownToRefresh( PullToRefreshBase<GridView> refreshView) { Log.e("TAG", "onPullDownToRefresh"); // Do work to String label = DateUtils.formatDateTime( getApplicationContext(), System.currentTimeMillis(), DateUtils.FORMAT_SHOW_TIME | DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_ABBREV_ALL); // Update the LastUpdatedLabel refreshView.getLoadingLayoutProxy() .setLastUpdatedLabel(label); new Thread(){ @Override public void run() { try { j++; init2(url+j); han.sendMessage(han.obtainMessage(2, str)); } catch (Exception e) { e.printStackTrace(); } } }.start(); } @Override public void onPullUpToRefresh( PullToRefreshBase<GridView> refreshView) { // TODO Auto-generated method stub new Thread(){ @Override public void run() { try { j--; init2(url+j); han.sendMessage(han.obtainMessage(3, str)); // } catch (Exception e) { e.printStackTrace(); } } }.start(); } }); } private void init() { // TODO Auto-generated method stub HttpClient httpClient=new DefaultHttpClient(); HttpGet httpGet=new HttpGet("http://apis.juhe.cn/goodbook/query?key=9d6ef8c31647a206e05fcaff70527182&catalog_id=+"+id+"&rn=10&pn="); try { HttpResponse httpResponse=httpClient.execute(httpGet); int len=httpResponse.getStatusLine().getStatusCode(); if(len==200){ HttpEntity httpEntity=httpResponse.getEntity(); str=EntityUtils.toString(httpEntity, "gbk"); han.sendMessage(han.obtainMessage(1, str)); } } catch (ClientProtocolException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } }; private void init2(String url) { // TODO Auto-generated method stub HttpClient httpClient=new DefaultHttpClient(); HttpGet httpGet=new HttpGet("http://apis.juhe.cn/goodbook/query?key=9d6ef8c31647a206e05fcaff70527182&catalog_id=+"+id+"&rn=10&pn="+j); try { HttpResponse httpResponse=httpClient.execute(httpGet); int len=httpResponse.getStatusLine().getStatusCode(); if(len==200){ HttpEntity httpEntity=httpResponse.getEntity(); str=EntityUtils.toString(httpEntity, "gbk"); } } catch (ClientProtocolException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } }; }
适配器mybase.class:
package com.example.map_new_two; import java.util.List; import android.content.Context; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.webkit.WebView.FindListener; import android.widget.BaseAdapter; import android.widget.TextView; public class Mybase extends BaseAdapter{ private List<Item> base; private Context context; public Mybase(List<Item> base, Context context) { super(); this.base = base; this.context = context; } @Override public int getCount() { // TODO Auto-generated method stub return base.size(); } @Override public Object getItem(int arg0) { // TODO Auto-generated method stub return base.get(arg0); } @Override public long getItemId(int arg0) { // TODO Auto-generated method stub return arg0; } @Override public View getView(int arg0, View arg1, ViewGroup arg2) { // TODO Auto-generated method stub Viehoder vh; if(arg1==null){ vh=new Viehoder(); arg1=LayoutInflater.from(context).inflate(R.layout.item,null); vh.texView1=(TextView) arg1.findViewById(R.id.textView1); arg1.setTag(vh); }else{ vh=(Viehoder) arg1.getTag(); } vh.texView1.setText(base.get(arg0).getCatalog()); return arg1; } class Viehoder{ TextView texView1; } }
xml解析的vo类
package com.example.map_new_two; public class Item { private String id; private String catalog; public String getId() { return id; } public void setId(String id) { this.id = id; } public String getCatalog() { return catalog; } public void setCatalog(String catalog) { this.catalog = catalog; } @Override public String toString() { return "Item [id=" + id + ", catalog=" + catalog + "]"; } }