threed+handler解析gson文件,并且上拉加载,下拉刷新

ThreeActivity.class:

package com.example.yuekao_one_try;

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.StatusLine;
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 android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.widget.GridView;

public class ThreeActivity extends Activity{
    private PullToRefreshGridView grid_view;
    private List<Demo> data=new ArrayList<Demo>();
    private int p=10;
    private int z=10;
    private Mybasetwo base;
    private String str;
    private String url="http://apis.juhe.cn/goodbook/query?key=9d6ef8c31647a206e05fcaff70527182&catalog_id=246&rn=50&rn=";
     private int i=1;
     private Handler handler=new Handler(){
         public void handleMessage(android.os.Message msg) {
             switch (msg.what) {
            case 0:
                base=new Mybasetwo(data, getApplicationContext());
                grid_view.setAdapter(base);
                break;
            case 3:

                  base=new Mybasetwo(data, getApplicationContext());
                    grid_view.setAdapter(base);
                base.notifyDataSetChanged();
                grid_view.onRefreshComplete();
                break;
            case 4:

                  base=new Mybasetwo(data, getApplicationContext());
                    grid_view.setAdapter(base);
                    grid_view.onRefreshComplete();
                break;
            default:
                break;
            }
         };
     };
    protected void onCreate(Bundle savedInstanceState) {
            // TODO Auto-generated method stub
            super.onCreate(savedInstanceState);
        setContentView(R.layout.grid);
        grid_view=(PullToRefreshGridView) findViewById(R.id.grid_view);
        grid_view.setMode(Mode.BOTH);
             init();
            grid_view.setOnRefreshListener(new OnRefreshListener<GridView>() {

                @Override
                public void onRefresh(PullToRefreshBase<GridView> refreshView) {
                    // TODO Auto-generated method stub
                       i++;
                          new Thread(){
                               public void run() {
                                   url="http://apis.juhe.cn/goodbook/query?key=9d6ef8c31647a206e05fcaff70527182&catalog_id=246&rn=50&rn="+i;

                                   getClient();
                                   Gson g=new Gson();
                                DataDemo dd=g.fromJson(str, DataDemo.class);
                                data=dd.getResult().getData();
                                   handler.sendEmptyMessage(4);
                               };
                           }.start();
                }

                    public void onPullUpToRefresh(PullToRefreshBase<GridView> refreshView) {
                        // TODO Auto-generated method stub
                        i++;
                          new Thread(){
                               public void run() {
                                   url="http://apis.juhe.cn/goodbook/query?key=9d6ef8c31647a206e05fcaff70527182&catalog_id=246&rn=50&rn="+i;
                                   getClient();
                                   Gson g=new Gson();
                                DataDemo dd=g.fromJson(str, DataDemo.class);
                                List<Demo> list=dd.getResult().getData();
                                data.addAll(list);

                                   handler.sendEmptyMessage(3);

                               };
                           }.start();
                    }
                });

        }
    private void init() {
        // TODO Auto-generated method stub
           new Thread(){
               public void run() {
                   url="http://apis.juhe.cn/goodbook/query?key=9d6ef8c31647a206e05fcaff70527182&catalog_id=246&rn=50&rn=10";
                   getClient();
                   Gson g=new Gson();
                DataDemo dd=g.fromJson(str, DataDemo.class);
                data=dd.getResult().getData();
                  handler.sendEmptyMessage(0);

               };
           }.start();
    }
      private void getClient() {
            // TODO Auto-generated method stub
            HttpClient  client=new DefaultHttpClient();
            HttpGet get=new HttpGet(url);
            try {
                HttpResponse response=client.execute(get);
                 StatusLine  statusLine=response.getStatusLine();
                 int statusCode=statusLine.getStatusCode();
                 if(statusCode==200){
                     HttpEntity  entity=response.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();
            }

        };
}

再加上适配器,这些就可以刷新出新的数据了

时间: 2024-10-10 22:46:20

threed+handler解析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

ListView上拉加载下拉刷新

主要用到了这个几个文件,MainActivity是界面的Activity,MyAdapter是ListView的自定义适配,MyListView是自定义带头部LIistView,如果只需要上拉加载就不需要:activity_main.xml是住界面,item.xml是ListView的子布局里面只有一个TextView,listview_footer.xml是listview的加载更多的底部布局,listview_header.xml是listview的头部布局. MainActivity.ja

使用dragloader.js插件实现上拉加载/下拉刷新..

在写代码时候有个需求是,在触屏页面,为了加快页面加载速度,案件列表每页展示5条数据: 然后点击更多,展示下一页数据: 但是为了触屏更好的体验,改为往上滑动案件列表,加载下一页数据:就是要实现上拉加载/下拉刷新的效果: 我只用到了 上拉加载: 参考资料:http://blog.csdn.net/xb12369/article/details/39202711 下面是写的demo: html代码: <!DOCTYPE html> <html lang="en"> &

Mint-ui中loadmore(上拉加载下拉刷新)组件在ios中滑动会触发点击事件的解决方法

bug说明: Mint-ui中loadmore(上拉加载下拉刷新)组件 在 使用fastclick的情况下 ,在ios设备中滑动会触发点击事件: 解决方法: 我是按需引入,去项目中找到loadmore下的index.js,全部引入的要找mint下面mint-ui.common.js 路径如下:你的项目名/node_modules\mint-ui\lib\loadmore\index.js 搜索 handleTouchEnd ,记得写event进去 handleTouchEnd: function

微信小程序上拉加载下拉刷新

微信小程序实现上拉加载下拉刷新 使用小程序默认提供方法. (1). 在xxx.json 中开启下拉刷新,需要设置backgroundColor,或者是backgroundTextStyle ,因为加载的动画可能会是白色背景,会看不清. { "usingComponents": { "annicate": "/components/annicate/index" }, "navigationBarTitleText": &quo

移动端上拉加载下拉刷新

<template> <div class="wrapper" ref="wrapper"> <div class="content" > <div class="refresh" :class="{ativeRefresh:refresh}">刷新</div> <div class="ct-row" v-for=&quo

xlistview上拉加载 下拉刷新

<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent" >

【PullToRefresh 系列一】 Android上拉加载下拉刷新控件详解

转载请注明:http://blog.csdn.net/duguang77/article/details/40921601 作者信息: PullToRefresh控件下载地址:https://github.com/chrisbanes/Android-PullToRefresh 作者:https://github.com/chrisbanes 百度网盘下载地址:http://pan.baidu.com/s/1o6umifw (一)导入方法: 1.下载后将extras,library,sample

XListView实现上拉加载下拉刷新

package com.loaderman.androiddemo; import android.content.Context; import android.util.AttributeSet; import android.view.MotionEvent; import android.view.View; import android.view.ViewTreeObserver.OnGlobalLayoutListener; import android.view.animation

上拉加载下拉刷新

老方法,先上效果图: 1.加入下拉刷新 上拉加载的依赖 1 //下拉刷新 上拉加载 2 implementation 'com.scwang.smartrefresh:SmartRefreshLayout:1.0.4-7' 3 implementation 'com.scwang.smartrefresh:SmartRefreshHeader:1.0.4-7' 2.控件的布局文件代码,以下的LinearLayout是内容部分,直接替换即可. 1 <com.scwang.smartrefresh.