Android之分类显示

首先要连接jdbc,这里有个莫名其妙的错误一直解决不了,但是先跳过了,重要的是思路和流程。

连接jdbc,将内容转换成json字符串。

先写连接数据库获取数据的方法,当调用Serlvet的时候调用方法连接数据库进行操作

public class CategoryDaoImple implements CategoryDao {


    @Override
    public List<Category> getCategoryList()  {
        Connection con = null;
        Statement s = null;
        ResultSet rs = null;
        List<Category> result = null;

        try {
            con = JdbcUtil.conn();
            Statement statement = con.createStatement();
            rs = s.executeQuery("SELECT category.category_id FROM category");
            result = new ArrayList<Category>();
            while (rs.next()) {
                Category category = new Category();
                category.setCategoryId(rs.getString("category_id"));
                result.add(category);
            }
            return null;
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            JdbcUtil.close(con, null, rs);

        }
        return result;
    }


    /*获取总数*/
    @Override
    public long getCategoryTotal() {
        Connection con = null;
      Statement s = null;
        ResultSet rs = null;
        long sum = 0;


        try {
            con = JdbcUtil.conn();
            s = con.createStatement();
            rs = s.executeQuery("SELECT  category_id sum FROM category");
            while (rs.next()) {
                sum = rs.getLong("sum");
            }

        } catch (Exception e) {
            e.printStackTrace();
            return -1;
        }finally {
            JdbcUtil.close(con, null, rs);
        }


        return sum;
    }
}

连接数据库,获取数据,转换成json字符串对象。

public class CategoryServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html");
        resp.setCharacterEncoding("UTF-8");
        PrintWriter out = resp.getWriter();
        CategoryDaoImple imple = new CategoryDaoImple();
        List<Category> list = imple.getCategoryList();
        long total = imple.getCategoryTotal();
        Category first = list.get(0);
        first.setCategoryId(1 + "");
        first.setCategoryNUmber(total);
        ResponseObject ressult = null;
        if (list != null && list.size() > 0) {
            ressult = new ResponseObject(1, list);
        } else {
            ressult = new ResponseObject(0, "没有数据");
        }
        out.println(new GsonBuilder().create().toJson(ressult));
        out.flush();
        out.close();



        super.doGet(req, resp);


    }
}

 后面的和上次一样,执行异步任务,获取连接头并请求,获取字符串,去掉json串的头部,再在适配器里面进行渲染,需要特殊处理一下id,因为从1开始,而我们编程中是从0开始,其他列如开启网络权限,特殊xml配置语句,开头记得把异步任务new一下并执行execute(),就完成了。

 public class CagoryDataTask extends AsyncTask<Void, Void, Void> {


        @Override
        protected void onPostExecute(Void aVoid) {
            super.onPostExecute(aVoid);
            list.setAdapter(new MyAdapter());
        }

        @Override
        protected Void doInBackground(Void... voids) {
            HttpClient httpClient = new DefaultHttpClient();
            HttpPost post = new HttpPost(Coasts.City_data_URI);
            try {
                HttpResponse response = httpClient.execute(post);
                String jsonString = EntityUtils.toString(response.getEntity());
                paseCategroyJson(jsonString);
            } catch (IOException e) {
                e.printStackTrace();
            }
            return null;
        }
    }

    private void paseCategroyJson(String jsonString) {
        Gson gson = new Gson();
        ResponseObject<List<Category>> responseObject = gson.fromJson(jsonString, new TypeToken<ResponseObject<List<Category>>>() {
        }.getType());
        List<Category> datas = responseObject.getDates();
        for (Category category : datas) {
            int postion = Integer.parseInt(category.getCategoryId());
            MyUtils.allCategoryNumber[postion - 1] = category.getCategoryNUmber();

        }

    }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值