Chinaunix首页 | 论坛 | 博客
  • 博客访问: 418423
  • 博文数量: 121
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 1393
  • 用 户 组: 普通用户
  • 注册时间: 2014-03-11 12:17
个人简介

www.vibexie.com vibexie@qq.com

文章分类

全部博文(121)

文章存档

2015年(55)

2014年(66)

我的朋友

分类: Android平台

2015-03-23 13:46:21

客户端:

MainActivity.java

  1. package cn.com.xiebiao.listview;
  2. import android.app.Activity;
  3. import android.app.ProgressDialog;
  4. import android.os.AsyncTask;
  5. import android.os.Bundle;
  6. import android.view.View;
  7. import android.view.ViewGroup;
  8. import android.widget.AbsListView;
  9. import android.widget.BaseAdapter;
  10. import android.widget.ListView;
  11. import android.widget.TextView;
  12. import android.widget.Toast;

  13. import java.util.ArrayList;
  14. import java.util.List;

  15. public class MainActivity extends Activity {
  16.     private String serverAddr="";
  17.     private ListView listView;
  18.     //是否分页
  19.     private boolean is_divpage;
  20.     //loading dialog
  21.     private ProgressDialog dialog;
  22.     //用于保存接收的数据
  23.     List<String> data=new ArrayList<String>();
  24.     //页数
  25.     private static int pageNo=1;

  26.     MyAdapter myAdapter=new MyAdapter();

  27.     @Override
  28.     protected void onCreate(Bundle savedInstanceState) {
  29.         super.onCreate(savedInstanceState);
  30.         setContentView(R.layout.activity_main);
  31.         listView = (ListView) this.findViewById(R.id.listView);
  32.         //设置ProgressDialog属性
  33.         dialog=new ProgressDialog(this);
  34.         dialog.setTitle("提示");
  35.         dialog.setMessage("loading...");
  36.         //初始的时候加载第一页
  37.         new MyTask().execute(serverAddr+pageNo);

  38.         listView.setOnScrollListener(new AbsListView.OnScrollListener() {
  39.             @Override
  40.             public void onScrollStateChanged(AbsListView absListView, int i) {
  41.                 System.out.println("----onScrollStateChanged>>"+i);
  42.                 //onScrollStateChanged再手指离开屏幕的瞬间调用,所以晚于onScroll的最后一次调用
  43.                 if(is_divpage&& i==0){
  44.                     //手指划到最底部并松开,即表示请求下次数据
  45.                     is_divpage=true;
  46.                     Toast.makeText(MainActivity.this,"下一页",Toast.LENGTH_SHORT).show();
  47.                     new MyTask().execute(serverAddr+pageNo);
  48.                 }
  49.             }

  50.             @Override
  51.             public void onScroll(AbsListView absListView, int i, int i2, int i3) {
  52.                 if((i+i2)==i3){
  53.                     //滑到最底部
  54.                     is_divpage=true;
  55.                 }else {
  56.                     is_divpage=false;
  57.                 }
  58.             }
  59.         });
  60.     }

  61.     //异步任务,再网络上读取数据
  62.     class MyTask extends AsyncTask<String,Void,List<String>>{
  63.         @Override
  64.         protected void onPreExecute() {
  65.             super.onPreExecute();
  66.             dialog.show();
  67.         }

  68.         @Override
  69.         protected List<String> doInBackground(String... strings) {
  70.             String jsonString=HttpUtils.sendPostMethod(strings[0],"utf-8");
  71.             List<String> list=JsonTools.parseJsonList(jsonString);
  72.             return list;
  73.         }

  74.         @Override
  75.         protected void onPostExecute(List<String> strings) {
  76.             super.onPostExecute(strings);
  77.             data.addAll(strings);

  78.             //更新data
  79.             myAdapter.bindDate(data);

  80.             if(pageNo==1){
  81.                 listView.setAdapter(myAdapter);
  82.             }
  83.             myAdapter.notifyDataSetChanged();
  84.             pageNo++;
  85.             //关闭progressDialog
  86.             dialog.dismiss();
  87.         }
  88.     }

  89.     class MyAdapter extends BaseAdapter {
  90.         private List<String> list;
  91.         private TextView textView;

  92.         MyAdapter() {
  93.         }

  94.         public void bindDate(List<String> list){
  95.             this.list = list;
  96.         }

  97.         @Override
  98.         public int getCount() {
  99.             System.out.println("--->>getCount中size=" + list.size());
  100.             return list.size();
  101.         }

  102.         @Override
  103.         public Object getItem(int i) {
  104.             System.out.println("--->>getItem中i=" + i);
  105.             return i;
  106.         }

  107.         @Override
  108.         public long getItemId(int i) {
  109.             System.out.println("--->>getItemID中i=" + i);
  110.             return 5;
  111.         }

  112.         @Override
  113.         public View getView(int i, View view, ViewGroup viewGroup) {
  114.             System.out.println("--->>getView中i=" + i);
  115.             if (view == null) {
  116.                 textView=new TextView(MainActivity.this);
  117.             } else {
  118.                 textView=(TextView)view;
  119.             }
  120.             textView.setText(list.get(i));
  121.             textView.setTextSize(30);
  122.             return textView;
  123.         }
  124.     }
  125. }

AndroidMainfest.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <manifest xmlns:android=""
  3.     package="cn.com.xiebiao.listview" >
  4. <uses-permission android:name="android.permission.INTERNET"></uses-permission>
  5.     <application
  6.         android:allowBackup="true"
  7.         android:icon="@mipmap/ic_launcher"
  8.         android:label="@string/app_name"
  9.         android:theme="@style/AppTheme" >
  10.         <activity
  11.             android:name=".MainActivity"
  12.             android:label="@string/app_name" >
  13.             <intent-filter>
  14.                 <action android:name="android.intent.action.MAIN" />

  15.                 <category android:name="android.intent.category.LAUNCHER" />
  16.             </intent-filter>
  17.         </activity>
  18.     </application>

  19. </manifest>


HttpUtil.java

  1. package cn.com.xiebiao.listview;

  2. import org.apache.http.HttpResponse;
  3. import org.apache.http.client.HttpClient;
  4. import org.apache.http.client.methods.HttpPost;
  5. import org.apache.http.impl.client.DefaultHttpClient;
  6. import org.apache.http.util.EntityUtils;

  7. import java.io.IOException;

  8. /**
  9.  * Created by vibexie on 3/23/15.
  10.  */
  11. public class HttpUtils {

  12.     public static String sendPostMethod(String path,String encoding){
  13.         String result=null;
  14.         HttpClient httpClient=new DefaultHttpClient();
  15.         HttpPost httpPost=new HttpPost(path);
  16.         try {
  17.             HttpResponse httpResponse=httpClient.execute(httpPost);
  18.             if(httpResponse.getStatusLine().getStatusCode()==200){
  19.                 result= EntityUtils.toString(httpResponse.getEntity(),encoding);
  20.             }
  21.         } catch (IOException e) {
  22.             e.printStackTrace();
  23.         }finally {
  24.             httpClient.getConnectionManager().shutdown();
  25.         }

  26.         return result;
  27.     }
  28. }

JsonTool.java

  1. package cn.com.xiebiao.listview;

  2. import org.json.JSONArray;
  3. import org.json.JSONException;
  4. import org.json.JSONObject;

  5. import java.util.ArrayList;
  6. import java.util.List;

  7. /**
  8.  * Created by vibexie on 3/23/15.
  9.  */
  10. public class JsonTools {
  11.     public static List<String> parseJsonList(String jsonString){
  12.         List<String> list=new ArrayList<String>();
  13.         try {
  14.             JSONObject jsonObject=new JSONObject(jsonString);
  15.             JSONArray jsonArray=jsonObject.getJSONArray("data");
  16.             for(int i=0;i<jsonArray.length();i++){
  17.                 list.add(jsonArray.getString(i));
  18.             }
  19.         } catch (JSONException e) {
  20.             e.printStackTrace();
  21.         }
  22.         return list;
  23.     }
  24. }

服务器端:

DividePageServlet.java

  1. package cn.com.xiebiao.dividePage;

  2. import java.io.IOException;
  3. import java.io.PrintWriter;
  4. import java.util.HashMap;
  5. import java.util.List;
  6. import java.util.Map;

  7. import javax.servlet.ServletException;
  8. import javax.servlet.annotation.WebServlet;
  9. import javax.servlet.http.HttpServlet;
  10. import javax.servlet.http.HttpServletRequest;
  11. import javax.servlet.http.HttpServletResponse;

  12. import net.sf.json.JSONSerializer;


  13. /**
  14.  *
  15.  * Title : DividePageServlet.java
  16.  * Company: ZhenBot
  17.  * Author : Vibe Xie @
  18.  * Time : Mar 23, 2015 10:30:03 AM
  19.  * Copyright: Copyright (c) 2015
  20.  * Description: 服务器端将数据进行分页,客户端发送页码请求,服务器返回对应页的json数据
  21.  */
  22. @WebServlet("/dividePageServlet")
  23. public class DividePageServlet extends HttpServlet {
  24.     private static final long serialVersionUID = 1L;
  25.     //private static int currentPage;//当前页
  26.     /**
  27.      * @see HttpServlet#HttpServlet()
  28.      */
  29.     public DividePageServlet() {
  30.         super();
  31.         // TODO Auto-generated constructor stub
  32.     }

  33.     /**
  34.      * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
  35.      */
  36.     protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  37.         // TODO Auto-generated method stub
  38.         doPost(request, response);
  39.     }

  40.     /**
  41.      * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
  42.      */
  43.     protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  44.         // TODO Auto-generated method stub
  45.         request.setCharacterEncoding("utf-8");
  46.         response.setCharacterEncoding("utf-8");
  47.         response.setContentType("text/html;charset=utf-8");
  48.         PrintWriter printWriter=response.getWriter();
  49.         
  50.         //获取资源
  51.         DataSource dataSource=new DataSource();
  52.         List<String> date=dataSource.getDataSource();
  53.         
  54.         //获取客户端的请求页码
  55.         String pageNo=request.getParameter("pageNo");
  56.         
  57.         int currentPage=1;//当前页
  58.         //请求页码非空的时候设定当前页为请求页
  59.         if(pageNo!=null){
  60.             currentPage=Integer.parseInt(pageNo);
  61.         }
  62.         
  63.         DividePageUtil dividePageUtil=new DividePageUtil(25, date.size(), currentPage);
  64.         int fromIndex=dividePageUtil.getFromIndex();
  65.         int toIndex=dividePageUtil.getToIndex();
  66.         
  67.         //当前页的数据
  68.         List<String> subData=date.subList(fromIndex, toIndex);
  69.         //将当前页的数据包装成json数据
  70.         Map<String, Object> map=new HashMap<String, Object>();
  71.         map.put("data", subData);
  72.         String jsonString=JSONSerializer.toJSON(map).toString();

  73.         //返回给客户端
  74.         printWriter.print(jsonString);
  75.         printWriter.flush();
  76.         printWriter.close();
  77.     }

  78. }

DataSource.java

  1. package cn.com.xiebiao.dividePage;

  2. import java.util.ArrayList;
  3. import java.util.List;

  4. public class DataSource {
  5.     private List<String> list;
  6.     
  7.     public DataSource() {
  8.         // TODO Auto-generated constructor stub
  9.     }
  10.     
  11.     public List<String> getDataSource(){
  12.         list=new ArrayList<String>();
  13.         for(int i=1;i<=500;i++){
  14.             list.add("data"+i);
  15.         }
  16.         return list;
  17.     }
  18. }

DividePageUtil.java

  1. package cn.com.xiebiao.dividePage;

  2. /**
  3.  *
  4.  * Title : DividePageUtil.java
  5.  * Company: ZhenBot
  6.  * Author : Vibe Xie @
  7.  * Time : Mar 23, 2015 10:52:38 AM
  8.  * Copyright: Copyright (c) 2015
  9.  * Description:分页工具类
  10.  */
  11. public class DividePageUtil {
  12.     private int pageSize;//每页条数
  13.     private int dataSize;//数据的总条数
  14.     private int currentPage;//当前页
  15.     private int pageCount;//总页数
  16.     
  17.     public DividePageUtil(int pageSize,int dateSize,int currentPage) {
  18.         // TODO Auto-generated constructor stub
  19.         this.pageSize=pageSize;
  20.         this.dataSize=dateSize;
  21.         this.setCurrentPage(currentPage);
  22.     }
  23.     
  24.     public int getPageSize() {
  25.         return pageSize;
  26.     }
  27.     public void setPageSize(int pageSize) {
  28.         this.pageSize = pageSize;
  29.     }
  30.     public int getDataSize() {
  31.         return dataSize;
  32.     }
  33.     public void setDataSize(int dataSize) {
  34.         this.dataSize = dataSize;
  35.     }
  36.     public int getCurrentPage() {
  37.         return currentPage;
  38.     }
  39.     
  40.     //设置当前页
  41.     public void setCurrentPage(int currentPage){
  42.         int activePage= currentPage<=0 ? 1 :currentPage;
  43.         activePage= currentPage>getPageCount() ? getPageCount() : activePage;
  44.         this.currentPage=activePage;
  45.     }
  46.     
  47.     //获取总页数
  48.     public int getPageCount() {
  49.         pageCount=dataSize/pageSize;
  50.         if((dataSize%pageSize)!=0){
  51.             pageCount++;
  52.         }
  53.         return dataSize==0 ? 1 : pageCount;
  54.     }
  55.     public void setPageCount(int pageCount) {
  56.         this.pageCount = pageCount;
  57.     }
  58.     
  59.     //获取指定页的起始索引
  60.     public int getFromIndex(){
  61.         return (currentPage-1)*pageSize;
  62.     }
  63.     //获取指定页的结束索引
  64.     public int getToIndex(){
  65.         return (currentPage*pageSize);
  66.     }
  67. }



阅读(2053) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~