okhttp请求远程数据并在ListView显示

1.我在本地搭建的服务端,基于springmvc,hibernate和注解,写了一个简单查询方法:

    @RequestMapping(value="/topicjson.do",method=RequestMethod.GET)
    @ResponseBody   
    public void topicjson(HttpSession session,HttpServletRequest request,HttpServletResponse response){
        List<TBaseTopic>topiclist=this.topicservice.getAllTopic();
        for (TBaseTopic tBaseTopic : topiclist) {
            tBaseTopic.setPublishdatestr(DateUtil.formatDate(tBaseTopic.getPublishdate(), "yyyy-MM-dd HH:mm"));
        }
        JSONArray array=JSONArray.fromObject(topiclist);
        response.setContentType("text/json;charset=utf-8");//设置编码
        PrintWriter out=null;
        try {
            out = response.getWriter();
        } catch (Exception e) {
        }
        out.print(array.toString());
    }

将查询出来的数据集合转成jsonArray,然后out.print(array.toString());将结果打印出来,要注意的是需要设置下数据格式和编码,不然中文会出现乱码问题。
先通过浏览器访问这个方法:http://192.168.56.1/springtest/topicaction/topicjson.do
ip是我们手机端访问我们本地服务器的ip,cmd,ipconfig,以太网IPv4
然后可以看到页面上的json格式的数据:

[{"comment":0,"heart":3,"id":"1","image":"","nickname":"管理员","publishdate":{"date":8,"day":4,"hours":7,"minutes":0,"month":11,"nanos":0,"seconds":0,"time":1481151600000,"timezoneOffset":-480,"year":116},"publishdatestr":"2016-12-08 07:00","topic":"如果再给你一次回去的机会,你想回到什么时候?","userid":"1001"},{"comment":0,"heart":22,"id":"2","image":"","nickname":"管理员","publishdate":{"date":28,"day":2,"hours":0,"minutes":0,"month":1,"nanos":0,"seconds":0,"time":1488211200000,"timezoneOffset":-480,"year":117},"publishdatestr":"2017-02-28 00:00","topic":"薛之谦的歌总能莫名其妙的戳中泪点","userid":"1001"},{"comment":0,"heart":221,"id":"3","image":"","nickname":"管理员","publishdate":{"date":28,"day":2,"hours":0,"minutes":0,"month":1,"nanos":0,"seconds":0,"time":1488211200000,"timezoneOffset":-480,"year":117},"publishdatestr":"2017-02-28 00:00","topic":"致陪伴我们每天的网易云音乐","userid":"1001"},{"comment":0,"heart":1,"id":"4","image":"","nickname":"管理员","publishdate":{"date":28,"day":2,"hours":0,"minutes":0,"month":1,"
  • 4
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
要在 ListView显示多张图片,可以使用 OkHttp 进行网络请求获取图片,并使用 ViewHolder 模式对 ListView 进行优化,以提高列表的滑动流畅性。具体实现步骤如下: 1. 在 Adapter 中定义 ViewHolder,用于缓存 item 中的控件: ```java private static class ViewHolder { ImageView imageView; } ``` 2. 在 getView() 方法中获取 ViewHolder,如果 convertView 为 null,则新建一个 ViewHolder,并使用 LayoutInflater 加载布局文件: ```java @Override public View getView(int position, View convertView, ViewGroup parent) { ViewHolder holder; if (convertView == null) { convertView = LayoutInflater.from(mContext).inflate(R.layout.item_layout, parent, false); holder = new ViewHolder(); holder.imageView = convertView.findViewById(R.id.image_view); convertView.setTag(holder); } else { holder = (ViewHolder) convertView.getTag(); } // 加载图片并设置到 ImageView 中 loadImage(holder.imageView, imageUrlList.get(position)); return convertView; } ``` 3. 在 loadImage() 方法中使用 OkHttp 进行网络请求,获取图片并设置到 ImageView 中: ```java private void loadImage(final ImageView imageView, String imageUrl) { OkHttpClient client = new OkHttpClient(); Request request = new Request.Builder() .url(imageUrl) .build(); client.newCall(request).enqueue(new Callback() { @Override public void onFailure(Call call, IOException e) { e.printStackTrace(); } @Override public void onResponse(Call call, Response response) throws IOException { InputStream inputStream = response.body().byteStream(); final Bitmap bitmap = BitmapFactory.decodeStream(inputStream); mHandler.post(new Runnable() { @Override public void run() { imageView.setImageBitmap(bitmap); } }); } }); } ``` 其中,`imageUrlList` 是存储图片 URL 地址的列表,`mContext` 是 Context 对象,`mHandler` 是 Handler 对象,用于在子线程中更新 UI。首先创建一个 OkHttpClient 对象,然后使用 Request.Builder 构建一个请求,设置请求的 URL 地址。接着使用 `newCall(request).enqueue()` 方法异步执行请求,并在回调方法中获取响应的结果,将 InputStream 转换成 Bitmap,并使用 Handler 将 Bitmap 设置到 ImageView 中,以更新 UI。 需要注意的是,由于网络请求是异步执行的,因此需要使用 Handler 来更新 UI,否则会出现异常。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值