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

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解析加上拉加载,下拉刷新的更多相关文章

  1. iOS开发 XML解析和下拉刷新,上拉加载更多

    iOS开发 XML解析和下拉刷新,上拉加载更多 1.XML格式 <?xml version="1.0" encoding="utf-8" ?> 表示 ...

  2. XListview的下拉刷新、上拉加载、用Pull解析XML

    做之前需要导入XListview的文件,此是用第三方的xListview实现的,东西没写全.此是在Fragment中实现的 //--------------XListView的布局---------- ...

  3. 【FastDev4Android框架开发】RecyclerView完全解析之下拉刷新与上拉加载SwipeRefreshLayout(三十一)

    转载请标明出处: http://blog.csdn.net/developer_jiangqq/article/details/49992269 本文出自:[江清清的博客] (一).前言: [好消息] ...

  4. springboot源码解析-管中窥豹系列之BeanDefine如何加载(十三)

    一.前言 Springboot源码解析是一件大工程,逐行逐句的去研究代码,会很枯燥,也不容易坚持下去. 我们不追求大而全,而是试着每次去研究一个小知识点,最终聚沙成塔,这就是我们的springboot ...

  5. Android Volley和Gson实现网络数据加载

    Android Volley和Gson实现网络数据加载 先看接口 1 升级接口 http://s.meibeike.com/mcloud/ota/cloudService POST请求 参数列表如下 ...

  6. 在element-ui的select下拉框加上滚动加载

    在项目中,我们需要运用到很多来自后端返回的数据.有时是上百条,有时甚至上千条.如果加上后端的多表查询或者数据量过大,这就导致在前端的显示就会及其慢,特别是在网络不好的时候更是如此. 自然,后端就做了一 ...

  7. 第三方 XListview 上拉加载、下拉刷新、分页加载和Gson解析

    注意:此Demo用的是第三方的Xlistview.jar,需要复制me文件夹到项目中,两个XML布局文件和一张图片 把下面的复制到String中 <string name="xlist ...

  8. 安卓数据解析之 fastjson 的解析以及Gson解析

    在安卓开发过程中的.我们经常使用的数据传递是以json格式传递.安卓 亲爹提供了我们Gson解析工具.点击下载Gson.jar 阿里巴巴FastJson是一个Json处理工具包,包含"序列化 ...

  9. Json解析与Gson解析

    本文主要介绍json最原始的解析与google提供的gson工具类解析 ①json解析 /** * 普通的json解析 * @param s * @throws JSONException */ pr ...

随机推荐

  1. PHP 模拟 HTTP 基本认证(Basic Authentication)

    当某个页面需要认证才能进行访问时,接到请求后服务器端会在响应头中发送一个 WWW-Authenticate 首部(用来标识认证安全域),语法为 WWW-Authenticate:Basic relam ...

  2. 【偶像大师 白金星光】的【Variable Tone】技术大公开!偶像从哪里看都那么可爱,VA小组谈制作方针

    http://game.watch.impress.co.jp/docs/news/1016369.html         自从街机版的运营依赖,今年迎来了[偶像大师]系列的11周年.在CEDEC ...

  3. 关于Fekit的安装与测试

    一. Fekit的安装 1.cmd输入指令 $ npm install fekit -g 2.初始化方法 $ fekit init 3.创建本地服务 $ fekit server 4.创建同时改端口( ...

  4. [软件测试基础3]基于Jemter的压力测试

    一.整体目标 安装LAMP待测系统,推荐ECShop,基于此进行Jmeter压力测试,并在测试后得出Jmeter测试报告,并根据sysstat得出Linux服务器的CIMN(CPU,IO,Memory ...

  5. LINUX端口查看

    e切依燃   LINUX端口查看 查看文件数 ls | wc -w lsof -i:80

  6. web前端开发培训和自学 哪种选择更适合你

    web前端相对于其他软件开发是比较容易入门的,但是如果深入学习就比较困难了,这门技能需要从业人员掌握一定的设计.代码.交互技能和一些SEO技能,容易入门还涉及这么多知识和技能,那学习web前端开发到底 ...

  7. C++学习笔记 构造&析构 友元 new&delete

    构造&析构函数 构造函数 定义:与类同名,可以有参可以无参,主要功能用于在类的对象创建时定义初始化的状态,无返回值,也不能用void修饰,构造函数不能被直接调用,必须通过new运算符在创建对象 ...

  8. SecureCRT连接VirtualBox下的ubuntu以及NMP的搭建与通过宿主机访问该NGINX服务器过程

    一直使用wamp作为开发环境,只是工作中时常会遇到在wamp下没问题但是提交到服务器生产环境会出现一些意想不到的情况,由此萌生了在本机搭建LNMP环境的想法,我个人不知道其他人的虚拟机搭建服务器环境是 ...

  9. WIN8,开机启动 需要管理员权限的程序

    1. 用WPF开发,需要管理员权限并开机启动,以写注册表的方式实现.仅写注册表只能启动一般权限的程序. 2. 考虑这样实现: 程序以一般权限启动,启动以后申请管理员权限. 实现: App类中重写OnS ...

  10. IIS发布网站

    IIS发布网站几个问题 1.部署步骤:http://jingyan.baidu.com/article/3065b3b6e5becdbecff8a4d5.html Win7下IIS报503Servic ...