从服务端加载图片(Volley框架+Gson框架+PullToRefresh框架)

先看服务端的代码(对象封装类和servlet类)

ShopInfo.java(get、set、构造器、toString方法省略)

private String name;private String img;ShopListServlet.javapackage com.atguigu.dianpin_server.servlet;import java.io.File;import java.io.IOException;import java.util.ArrayList;import java.util.List;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import com.google.gson.Gson;/** * 获取分页ShopList的json字符串 */public class ShopListServlet extends HttpServlet {private List<ShopInfo> infos;protected void doGet(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {init();// 得到start和count的请求参数int start = Integer.parseInt(request.getParameter("start"));int count = Integer.parseInt(request.getParameter("count"));// 如果start太大, 就返回一个空串// 15 start=15if (start >= infos.size()) {response.getWriter().write("");return;}// 从集合中取当前请求页的数据集合List<ShopInfo> data = new ArrayList<ShopInfo>();// 11 start=10&count=5if (start + count > infos.size()) {count = infos.size() – start;}for (int i = 0; i < count; i++) {data.add(infos.get(start + i));}// 转换为json字符串String json = new Gson().toJson(data);// 写到客户端response.setContentType("text/json;charset=utf-8");response.getWriter().write(json);// [{"name":"商铺名称1", "img":"f1.jpg"},{"name":"商铺名称2", "img":"f12.jpg"}]}public void init() {if (infos == null) {infos = new ArrayList<ShopInfo>();// 得到/image的真实路径String imagesPath = getServletContext().getRealPath("/image");// 得到路径对象File dirFile = new File(imagesPath);// 得到所有图片file对象File[] files = dirFile.listFiles();// 遍历for (int i = 0; i < files.length; i++) {// 将图片信息封装为一个shopinfo对象, 并保存到集合中String imageName = files[i].getName();String name = "商铺名称 " + (i + 1);infos.add(new ShopInfo(name, imageName));}}}}——————————————–分割线——————————————————————-看android代码

先贴出布局来

activity_main.xml

<FrameLayout xmlns:android=""android:layout_width="fill_parent"android:layout_height="fill_parent" ><com.handmark.pulltorefresh.library.PullToRefreshListViewandroid:id="@+id/lv_main"android:layout_width="match_parent"android:layout_height="match_parent" /><ProgressBarandroid:id="@+id/pb_main"style="?android:attr/progressBarStyleLarge"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_gravity="center"/></FrameLayout>list_item.xml<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android=""android:layout_width="fill_parent"android:layout_height="100dp"android:orientation="horizontal"android:gravity="center_vertical"><com.android.volley.toolbox.NetworkImageViewandroid:id="@+id/iv_img"android:layout_width="90dp"android:layout_height="90dp"/><TextViewandroid:id="@+id/tv_name"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="描述文本"android:layout_marginLeft="20dp"android:textSize="20sp"/></LinearLayout>listview_foot.xml<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android=""android:layout_width="match_parent"android:layout_height="wrap_content"android:orientation="horizontal"android:gravity="center"android:clickable="false"android:focusable="false"><ProgressBarandroid:id="@+id/pb_foot"style="?android:attr/progressBarStyle"android:layout_width="wrap_content"android:layout_height="wrap_content"/><TextViewandroid:id="@+id/tv_foot"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="正在加载中…" /></LinearLayout><!– 1. 如果还有更多数据, 它就会显示2. 如果没有更多数据: 隐藏ProgressBar, 更新TextView的文本 –>

ShopInfo.java(get、set、构造器、toString方法省略)

private String name;private String img;让所有的愁向后飞去。请不要回头去追你因该向前奔跑,因为快乐在前方!

从服务端加载图片(Volley框架+Gson框架+PullToRefresh框架)

相关文章:

你感兴趣的文章:

标签云: