定位和xml解析和gson解析加上拉加载,下拉刷新

时间:2023-12-30 15:46:50

这里的上拉加载,下拉刷新用到是依赖包

Mainactivity,xml解析和定位

package com.exmple.autolayout;

import java.util.List;

import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
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; 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 com.exmple.base.BaseApater;
import com.exmple.netUtil.NetUtil;
import com.exmple.vo.Myitem;
import com.exmple.vo.Super;
import com.thoughtworks.xstream.XStream;
import com.zhy.autolayout.AutoLayoutActivity; public class MainActivity extends AutoLayoutActivity {
public LocationClient mLocationClient = null;
public BDLocationListener myListener = new MyLocationListener();
private TextView city;
private ListView listview;
private List<Myitem> list; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
city = (TextView) findViewById(R.id.city);
listview = (ListView) findViewById(R.id.listview);
mLocationClient = new LocationClient(getApplicationContext()); // 声明LocationClient类
mLocationClient.registerLocationListener(myListener);
initLocation();
mLocationClient.start();
new My().execute("http://apis.juhe.cn/goodbook/catalog?key=9d6ef8c31647a206e05fcaff70527182&dtype=xml");
listview.setOnItemClickListener(new OnItemClickListener() { @Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Intent ints = new Intent(MainActivity.this, NewActivity.class);
ints.putExtra("id", list.get(position).getId());
startActivity(ints); }
});
} 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
city.setText(location.getCity()); } } public class My extends AsyncTask<String, Integer, String> { @Override
protected String doInBackground(String... params) {
String url = NetUtil.getXMl(params[0]);
return url;
} @Override
protected void onPostExecute(String result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
XStream xs = new XStream();
xs.processAnnotations(Super.class);
Super xml = (Super) xs.fromXML(result);
list = xml.getResult().getItem();
if (list != null) {
BaseApater ba = new BaseApater(list, MainActivity.this);
listview.setAdapter(ba);
} }
}
}

  NewActivitygson解析,handler发送消息,网络请求将数据拼接

package com.exmple.autolayout;

import java.util.ArrayList;
import java.util.List; 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;
import android.widget.Toast; import com.exmple.base.BaseApater1;
import com.exmple.datevo.DateSuper;
import com.exmple.datevo.MyData;
import com.exmple.netUtil.NetUtil;
import com.google.gson.Gson;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshBase.OnRefreshListener2;
import com.handmark.pulltorefresh.library.PullToRefreshGridView;
import com.zhy.autolayout.AutoLayoutActivity; public class NewActivity extends AutoLayoutActivity {
String url = "http://apis.juhe.cn/goodbook/query?key=9d6ef8c31647a206e05fcaff70527182";
private PullToRefreshGridView grd;
int page = 1;
Handler ha = new Handler() {
public void handleMessage(android.os.Message msg) { String ma = msg.obj.toString();
Gson gs = new Gson();
DateSuper fromJson = gs.fromJson(ma, DateSuper.class);
if (fromJson != null) {
List<MyData> list = fromJson.getResult().getData();
lists.addAll(list);
BaseApater1 ba = new BaseApater1(lists, NewActivity.this);
grd.setAdapter(ba);
}
};
}; List<MyData> lists = new ArrayList<MyData>(); @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_new);
Intent ints = getIntent();
final String id = ints.getStringExtra("id");
Toast.makeText(NewActivity.this, id, 0).show();
grd = (PullToRefreshGridView) findViewById(R.id.pull_refresh_grid); getData(url, id, page); grd.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);
page=1;
System.out.println(page+"=========");
getData(url, id, page);
grd.onRefreshComplete(); } @Override
public void onPullUpToRefresh(
PullToRefreshBase<GridView> refreshView) {
Log.e("TAG", "onPullUpToRefresh"); // Do work to refresh
page++;
System.out.println(page+"++++++++++");
getData(url, id, page);
grd.onRefreshComplete();
// the list here. }
});
} private void getData(final String url, final String id, final int page) {
new Thread() {
public void run() { String data = NetUtil.getXMl(url, id, page);
ha.sendMessage(ha.obtainMessage(1, data)); };
}.start(); } }

  NetUtil网络请求

package com.exmple.netUtil;

import java.io.IOException;

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.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import org.apache.http.util.EntityUtils; public class NetUtil {
public static String getXMl(String url)
{
String str="";
HttpGet get=new HttpGet(url);
HttpParams parmas=new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(parmas, 50000);
HttpConnectionParams.setSoTimeout(parmas, 50000);
HttpClient client=new DefaultHttpClient();
try {
HttpResponse execute = client.execute(get);
if(execute.getStatusLine().getStatusCode()==200)
{
HttpEntity entity=execute.getEntity();
str=EntityUtils.toString(entity, "UTF-8");
}
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return str;
}
public static String getXMl(String url,String id,int page)
{
String str="";
StringBuffer sb=new StringBuffer(url);
sb.append("&catalog_id="+id);
sb.append("&rn=10&rn=10&pn="+page); HttpGet get=new HttpGet(sb.toString());
HttpParams parmas=new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(parmas, 50000);
HttpConnectionParams.setSoTimeout(parmas, 50000);
HttpClient client=new DefaultHttpClient();
try {
HttpResponse execute = client.execute(get);
if(execute.getStatusLine().getStatusCode()==200)
{
HttpEntity entity=execute.getEntity();
str=EntityUtils.toString(entity, "UTF-8");
}
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return str;
} }