定位和xml解析和gson解析加上拉加载,下拉刷新
这里的上拉加载,下拉刷新用到是依赖包
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;
} }
定位和xml解析和gson解析加上拉加载,下拉刷新的更多相关文章
- iOS开发 XML解析和下拉刷新,上拉加载更多
iOS开发 XML解析和下拉刷新,上拉加载更多 1.XML格式 <?xml version="1.0" encoding="utf-8" ?> 表示 ...
- XListview的下拉刷新、上拉加载、用Pull解析XML
做之前需要导入XListview的文件,此是用第三方的xListview实现的,东西没写全.此是在Fragment中实现的 //--------------XListView的布局---------- ...
- 【FastDev4Android框架开发】RecyclerView完全解析之下拉刷新与上拉加载SwipeRefreshLayout(三十一)
转载请标明出处: http://blog.csdn.net/developer_jiangqq/article/details/49992269 本文出自:[江清清的博客] (一).前言: [好消息] ...
- springboot源码解析-管中窥豹系列之BeanDefine如何加载(十三)
一.前言 Springboot源码解析是一件大工程,逐行逐句的去研究代码,会很枯燥,也不容易坚持下去. 我们不追求大而全,而是试着每次去研究一个小知识点,最终聚沙成塔,这就是我们的springboot ...
- Android Volley和Gson实现网络数据加载
Android Volley和Gson实现网络数据加载 先看接口 1 升级接口 http://s.meibeike.com/mcloud/ota/cloudService POST请求 参数列表如下 ...
- 在element-ui的select下拉框加上滚动加载
在项目中,我们需要运用到很多来自后端返回的数据.有时是上百条,有时甚至上千条.如果加上后端的多表查询或者数据量过大,这就导致在前端的显示就会及其慢,特别是在网络不好的时候更是如此. 自然,后端就做了一 ...
- 第三方 XListview 上拉加载、下拉刷新、分页加载和Gson解析
注意:此Demo用的是第三方的Xlistview.jar,需要复制me文件夹到项目中,两个XML布局文件和一张图片 把下面的复制到String中 <string name="xlist ...
- 安卓数据解析之 fastjson 的解析以及Gson解析
在安卓开发过程中的.我们经常使用的数据传递是以json格式传递.安卓 亲爹提供了我们Gson解析工具.点击下载Gson.jar 阿里巴巴FastJson是一个Json处理工具包,包含"序列化 ...
- Json解析与Gson解析
本文主要介绍json最原始的解析与google提供的gson工具类解析 ①json解析 /** * 普通的json解析 * @param s * @throws JSONException */ pr ...
随机推荐
- WAF攻防研究之四个层次Bypass WAF
从架构.资源.协议和规则4个层次研究绕过WAF的技术,助于全方位提升WAF防御能力. 绕过WAF的相关技术研究是WAF攻防研究非常重要的一部分,也是最有趣的部分,所以我在写WAF攻防时先写攻击部分.还 ...
- UIPickerView理解
- Struts2中的ModelDriven机制及其运用
所谓ModelDriven,意思是直接把实体类当成页面数据的收集对象.比如,有实体类User如下: package cn.com.leadfar.struts2.actions; public cla ...
- Android-简单拨号器案例
Android [19]简单电话拨号器 @方法步骤 1.新建一个android程序,项目名设置为 phone ,然后打开 phone->res->layout->activity ...
- 总结-computer
常见问题 1.任务栏位置:%AppData%\Microsoft\Internet Explorer\Quick Launch\User Pinned\TaskBar 2.锁定到任务栏的图标不见了: ...
- Java-马士兵动态代理模式
Java-马士兵动态代理模式 模拟jdk的动态代理的实现原理, 这些东西没有必要写出来,写项目的时候一般用不上,主要是为了面试和理解原理: java动态代理有什么作用 作用非常大,在很多底层框架中都会 ...
- chm转换为html
在Windows下chm转换为html的超简单方法(反编译CHM文件的方法) 通过调用Windows命令,将chm 文件转换为html 文件. 方法: 命令行(cmd),输入hh -decompile ...
- IT行业常谈的优雅
起因 前几天在群里和以前一起在成都培训的同学谈论到了求职, 有一位朋友说他在某家外包公司试用失败了, 然后我说了句:不要去外包公司.即使工资高一点. 其实说的时候也没考虑到他本人的处境, 毕竟还房贷资 ...
- tomcat404报错解决
本地启动tomcat,不报错.也可正常打开http://localhost:8080 但是eclipse中启动tomcat不报错.但是打开http://localhost:8080时报404 思考,原 ...
- C#_基本类型
1.C#中的值类型包括:简单类型.枚举类型和结构类型. 2.C#中的引用类型包括:类(class).接口(interface).数组.委托(delegate).object和string. 3.调试时 ...