定位和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;
	}

}

  

时间: 2024-08-01 21:40:26

定位和xml解析和gson解析加上拉加载,下拉刷新的相关文章

XML解析及上拉加载下拉刷新

XML解析及上拉加载下拉刷新 1.XML格式 2.GData和XPath遍历 //配置XML库(配置完才能使用) //(1)添加头文件搜索路径 // Header Search Paths-> /usr/include/libxml2 //(2)添加二进制库 // Link library -> lixml2.dylib //(3)源文件添加编译选项 // -fno-objc-arc //(4)添加头文件 // #import "GDataXMLNode.h"*/ XPat

IOS开发之XML解析以及下拉刷新上拉加载更多的分享

IOS开发之XML解析 1.XML格式 <?xml version="1.0" encoding="utf-8" ?> 表示XML文件版本, 内部文本使用的编码 <root> 表示根节点 <CityName>北京</CityName>  一个结点, CityName是结点名, 北京结点值 <Item key="1" value="A"></Item>  

XML解析以及下拉刷新上拉加载的一些基本使用

---恢复内容开始--- IOS开发之XML解析 1.XML格式 <?xml version="1.0" encoding="utf-8" ?> 表示XML文件版本, 内部文本使用的编码 <root> 表示根节点 <CityName>北京</CityName>  一个结点, CityName是结点名, 北京结点值 <Item key="1" value="A"><

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

做之前需要导入XListview的文件,此是用第三方的xListview实现的,东西没写全.此是在Fragment中实现的 //--------------XListView的布局---------------- <me.XListView android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:id="@+id/lv&

Maxwin-z/XListView-Android(下拉刷新上拉加载)源码解析(一)

本次解析的内容,是github上一个用于下拉刷新上拉加载的控件xlistview,这个功能相信大家在开发的过程中会经常用到. 控件的源码地址是https://github.com/Maxwin-z/XListView-Android 在这个控件之前,我看过一些相同功能的控件,挑选后觉得XListView功能比较完善,而且易于理解.在android-open-project里面,有提到一个DropDownListView,个人使用过以后,觉得功能是具备了,但是操作体验不好,原因就是没有使用到Scr

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

转载请标明出处: http://blog.csdn.net/developer_jiangqq/article/details/49992269 本文出自:[江清清的博客] (一).前言: [好消息]个人网站已经上线运行,后面博客以及技术干货等精彩文章会同步更新,请大家关注收藏:http://www.lcode.org 话说RecyclerView已经面市很久,也在很多应用中得到广泛的使用,在整个开发者圈子里面也拥有很不错的口碑,那说明RecyclerView拥有比ListView,GridVi

解析OBJ模型并将其加载到Unity3D场景中

??各位朋友,大家好,欢迎大家关注我的博客,我是秦元培,我的博客地址是http://qinyuanpei.com.今天想和大家交流的是解析obj模型并将其加载到Unity3D场景中,虽然我们知道Unity3D是可以直接导入OBJ模型的,可是有时候我们并不能保证我们目标客户知道如何使用Unity3D的这套制作流程,可能对方最终提供给我们的就是一个模型文件而已,所以这个在这里做这个尝试想想还是蛮有趣的呢,既然如此,我们就选择在所有3D模型格式中最为简单的OBJ模型来一起探讨这个问题吧! 关于OBJ模

读取网上gson数据,展示到gridview上,然后上拉加载,下拉刷新

NetWorkUtil.class package com.example.liangminghui20160425; 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.HttpC

web.xml 之contextParam,listener,filter,servlet的加载顺序

先以加载spring为例子看看加载顺序的作用: Spring加载可以利用ServletContextListener 实现,也可以采用load-on-startup Servlet 实现,但比如filter 需要用到 bean ,但加载顺序是: 先加载filter 后加载spring,则filter中初始化操作中的bean为null:所以,如果过滤器中要使用到 bean,此时就可以根据加载顺序listener>filter>servlet,将spring 的加载 改成 Listener的方式.