带参数路径的刷新和加载
package com.baidu.zongxml; import java.util.ArrayList; import java.util.List; import com.baidu.myutils.MyUtils; import com.baway.adapter.MyAdapter; import com.baway.vo.MyBooks; import com.baway.vo.MyData; 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 android.os.AsyncTask; import android.os.Bundle; import android.app.Activity; import android.view.Menu; import android.widget.GridView; public class MainActivity extends Activity { private List<MyData> list_push; private List<MyData> list; private PullToRefreshGridView gv; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); list_push = new ArrayList<MyData>(); init(); } private void init() { // 找到相应的控件 gv = (PullToRefreshGridView) findViewById(R.id.gv); String yy="http://apis.juhe.cn/goodbook/query?key=9d6ef8c31647a206e05fcaff70527182&catalog_id=242&rn=10&rn=10"; new MyTask().execute(0); gv.setOnRefreshListener(new OnRefreshListener2<GridView>() { // 下拉刷新加载 public void onPullDownToRefresh( PullToRefreshBase<GridView> refreshView) { new MyTask().execute(20); new MyAdapter(MainActivity.this, list_push).notifyDataSetChanged(); gv.onRefreshComplete(); } // 上拉加载跟多 public void onPullUpToRefresh( PullToRefreshBase<GridView> refreshView) { new MyTask2().execute(30); new MyAdapter(MainActivity.this, list_push).notifyDataSetChanged(); gv.onRefreshComplete(); } }); } class MyTask extends AsyncTask<Integer, Integer, String> { @Override protected String doInBackground(Integer... params) { int str=params[0]; String jstr=MyUtils.Check_j(str); return jstr; } @Override protected void onPostExecute(String result) { System.out.println(result+"========"); Gson g=new Gson(); MyBooks book=g.fromJson(result, MyBooks.class); List<MyData> list=book.getResult().getData(); gv.setAdapter(new MyAdapter(MainActivity.this, list)); super.onPostExecute(result); } } class MyTask2 extends AsyncTask<Integer, Integer, String> { @Override protected String doInBackground(Integer... params) { int str=params[0]; String jstr=MyUtils.Check_j(str); return jstr; } @Override protected void onPostExecute(String result) { System.out.println(result+"========"); Gson g=new Gson(); MyBooks book=g.fromJson(result, MyBooks.class); list = book.getResult().getData(); list_push.addAll(list); gv.setAdapter(new MyAdapter(MainActivity.this, list_push)); super.onPostExecute(result); } } }
//请求数据
package com.baidu.myutils; import java.io.BufferedReader; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.URL; public class MyUtils { //设置请求的数据路径 public static String J_Url="http://apis.juhe.cn/goodbook/query?key=9d6ef8c31647a206e05fcaff70527182"; public static String Check_j(int pn){ String str=""; StringBuffer sb=new StringBuffer(J_Url+"&catalog_id=242&pn="+pn+"&rn=20"); try { URL j_url=new URL(sb.toString()); HttpURLConnection connection=(HttpURLConnection) j_url.openConnection(); connection.setConnectTimeout(5000); connection.setReadTimeout(5000); if(connection.getResponseCode()==200){ BufferedReader br=new BufferedReader(new InputStreamReader(connection.getInputStream(),"utf-8") ); str=br.readLine(); } } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } return str; } }