python爬虫+JavaWeb接口+Android完整的小项目开发案例(3)

上一篇的写了web端接口,本篇来讲解一下制作一个简单的糗事百科app,先上一张效果图


image.png

思路如下:
解析web端接口,获取需要的参数,然后将数据与listview条目绑定,图片请求框架用xutils

准备工作:下载好android studio 并配置好环境
导入xutils模块,gson模块

好,准备工作完毕,下面详述开发过程:

1.再manifest文件中添加网络请求权限

 <uses-permission android:name="android.permission.INTERNET"/>

2.创建糗事百科实体类,可以用android studio 的GsonFormat插件生成:

package com.example.qiushibaike.Bean;

import java.util.List;

/**
 * Created by lenovo on 2018/3/22.
 */

public class Datas {
    

    /**
     * currentPage : 1
     * totalPage : 38
     * count : 15
     * qiushibaikes : [{"id":1,"imgurl":"//pic.qiushibaike.com/system/avtnew/3298/32987623/thumb/20170531181603.JPEG?imageView2/1/w/90/h/90","username":"\n(糗名招著)~老鼠\u2026\n","vote":"1678","comments":"29","content":"儿子小学三年级了,过年放寒假,跟老婆商量好了,开学前寒假作业语文她检查,数学我检查。","imgpath":""},{"id":2,"imgurl":"//pic.qiushibaike.com/system/avtnew/3059/30591370/thumb/20171217185111.JPEG?imageView2/1/w/90/h/90","username":"\n睡觉撞墙卡跟头\n","vote":"2860","comments":"27","content":"我家汪居然学会装死,装的特别像,晚上我让它踏躺好了,冲爸妈大喊\u201c不好了,狗死了!\u201d给我妈吓坏了,怎么弄它都不动,在旁边一直沉默的老爹开口了\u201c嗯!看样子是死了,准备烧水剥皮,炖狗肉吧!\u201d傻汪嗷的一声跳起来,围着爸妈转圈,不停摆尾,吐舌卖萌!也是醉了。。。","imgpath":""},{"id":3,"imgurl":"//pic.qiushibaike.com/system/avtnew/2843/28437800/thumb/20180315173338.JPEG?imageView2/1/w/90/h/90","username":"\n桑榆非晚风未凉\n","vote":"166","comments":"8","content":"我现在知道跟我一样高个子的表姐,为嘛被表姐夫当萝莉一样宠了。","imgpath":""},{"id":4,"imgurl":"//pic.qiushibaike.com/system/avtnew/3727/37277250/thumb/2018031822214788.JPEG?imageView2/1/w/90/h/90","username":"\n一笔前缘一缕烟0\n","vote":"194","comments":"17","content":"百度里面老跳出来这个东西是什么情况还是关不掉的那种     有没有知道的糗友 给支个招什么的[幻想][幻想]","imgpath":"//pic.qiushibaike.com/system/pictures/12011/120110868/medium/app120110868.jpg"},{"id":5,"imgurl":"//pic.qiushibaike.com/system/avtnew/2722/27224008/thumb/20171228224921.JPEG?imageView2/1/w/90/h/90","username":"\n直::男\n","vote":"1065","comments":"33","content":"上级专家王主任来坐诊,今天病人特别多,性 病的有十几个。下午快下班时候不忙,谈论起找小 姐这个事儿,我似笑非笑的试探:\u201c王主任,作为男性 病方面的专家,您要是去找,肯定传 染不上什么病吧?!\u201d","imgpath":""},{"id":6,"imgurl":"//pic.qiushibaike.com/system/avtnew/3020/30201529/thumb/20180310222647.JPEG?imageView2/1/w/90/h/90","username":"\n道士下山会女神\n","vote":"7525","comments":"47","content":"老婆开车从小区倒出来,刚好小区门口有辆三轮与一辆大奔擦刮,几个交警正在现场处理,老婆一看一下倒不出,就摇下车窗冲几个交警喊道:让开,女司机。","imgpath":""},{"id":7,"imgurl":"//pic.qiushibaike.com/system/avtnew/3691/36912791/thumb/2018021823310272.JPEG?imageView2/1/w/90/h/90","username":"\n俺好莱芜的\n","vote":"2118","comments":"10","content":"\u201c老弟,你小时老漂亮了!好多邻居都抢着和咱们定娃娃亲!\u201d","imgpath":""},{"id":8,"imgurl":"//pic.qiushibaike.com/system/avtnew/2881/28814804/thumb/20150912193249.jpg?imageView2/1/w/90/h/90","username":"\n落跑的老男孩\n","vote":"422","comments":"1","content":"lz带96年小鲜肉同事一起出差,半夜门外有人按门铃(估计是折了翅的美女)被lz大声呵斥,早上醒来问他昨晚休息的怎么样,小伙子说昨晚被干(平声)醒了。。。,半夜起来关了空调。。。lz一听憋不住笑了,你说这么多年这坨都教会我什么了。。。","imgpath":""},{"id":9,"imgurl":"//pic.qiushibaike.com/system/avtnew/3374/33747823/thumb/2017050913112372.JPEG?imageView2/1/w/90/h/90","username":"\n梅朵0\n","vote":"190","comments":"45","content":"我们家对面住了一女的,二十多岁左右,3月2号左右搬过来的,她是租的房。我的天啊,今天我看见她晒了大概有19条内裤,4件内衣。现在的女孩也太懒了吧。屯到一块洗,不脏吗?","imgpath":"//pic.qiushibaike.com/system/pictures/12011/120111850/medium/app120111850.jpg"},{"id":10,"imgurl":"//pic.qiushibaike.com/system/avtnew/15/157166/thumb/20140512103831.jpg?imageView2/1/w/90/h/90","username":"\n很好的坏人℡\n","vote":"289","comments":"9","content":"我还用解释嘛???","imgpath":"//pic.qiushibaike.com/system/pictures/11986/119862939/medium/app119862939.jpg"},{"id":11,"imgurl":"//pic.qiushibaike.com/system/avtnew/3707/37076223/thumb/20180228125455.JPEG?imageView2/1/w/90/h/90","username":"\n姑苏云琪\n","vote":"1602","comments":"26","content":"半夜饿了,起床找吃的,老公迷迷糊糊睁开眼问我:这大半夜的,你想吃什么?","imgpath":""},{"id":12,"imgurl":"//pic.qiushibaike.com/system/avtnew/1183/11837809/thumb/2016120507194283.JPEG?imageView2/1/w/90/h/90","username":"\n帝国守卫\n","vote":"5585","comments":"73","content":"和女朋友出去玩,路边献血送洋娃娃。女朋友想要,我当然义不容辞,撸起袖子就上。","imgpath":""},{"id":13,"imgurl":"//pic.qiushibaike.com/system/avtnew/1297/12973477/thumb/20160601085737.jpg?imageView2/1/w/90/h/90","username":"\n0海之梦幻0\n","vote":"125","comments":"3","content":"买烤鸭碰到20年前的幼儿园老师,怕认错人,不敢上去打招呼,直到把这张照片给我妈确认后,我才相信自己没有错.....","imgpath":"//pic.qiushibaike.com/system/pictures/12011/120111975/medium/app120111975.jpg"},{"id":14,"imgurl":"//pic.qiushibaike.com/system/avtnew/3392/33921103/thumb/201706221753314.PNG?imageView2/1/w/90/h/90","username":"\n无数春闺梦里人\n","vote":"71","comments":"1","content":"高三的时候,学校查手机查的很紧,班主任经常来寝室突击检查。有一次,我一个人在寝室撸的正欢,班主任突然来检查,我吓得马上钻进被子里,结果班主任以为我在玩手机,一掀被子射了她一脸,后面一个学期上她的课都是尴尬的。","imgpath":""},{"id":15,"imgurl":"","username":"","vote":"461","comments":"7","content":"楼主是宾馆服务员,今天一个满头白发的男的来开房,看着要有七八十了,满脸的老年斑。他进房间后,房门不关严留一条缝,领班来查房时说楼主那间房房门咋不关?楼主说客人没关。一会儿和老员工说起此事,老员工说让我们别管,客人经常来开房,那是给一个女的留门的。又过一会儿我们下班那个客人也退房了。一个人走的,听老员工说那女的没来,客人走时看着有些失望,也有点失落!","imgpath":""}]
     * totalCount : 556
     */

    private int currentPage;
    private int totalPage;
    private int count;
    private int totalCount;
    private List<QiushibaikesBean> qiushibaikes;

    public int getCurrentPage() {
        return currentPage;
    }

    public void setCurrentPage(int currentPage) {
        this.currentPage = currentPage;
    }

    public int getTotalPage() {
        return totalPage;
    }

    public void setTotalPage(int totalPage) {
        this.totalPage = totalPage;
    }

    public int getCount() {
        return count;
    }

    public void setCount(int count) {
        this.count = count;
    }

    public int getTotalCount() {
        return totalCount;
    }

    public void setTotalCount(int totalCount) {
        this.totalCount = totalCount;
    }

    public List<QiushibaikesBean> getQiushibaikes() {
        return qiushibaikes;
    }

    public void setQiushibaikes(List<QiushibaikesBean> qiushibaikes) {
        this.qiushibaikes = qiushibaikes;
    }

    public static class QiushibaikesBean {
        /**
         * id : 1
         * imgurl : //pic.qiushibaike.com/system/avtnew/3298/32987623/thumb/20170531181603.JPEG?imageView2/1/w/90/h/90
         * username :
         (糗名招著)~老鼠…

         * vote : 1678
         * comments : 29
         * content : 儿子小学三年级了,过年放寒假,跟老婆商量好了,开学前寒假作业语文她检查,数学我检查。
         * imgpath :
         */

        private int id;
        private String imgurl;
        private String username;
        private String vote;
        private String comments;
        private String content;
        private String imgpath;

        public int getId() {
            return id;
        }

        public void setId(int id) {
            this.id = id;
        }

        public String getImgurl() {
            return imgurl;
        }

        public void setImgurl(String imgurl) {
            this.imgurl = imgurl;
        }

        public String getUsername() {
            return username;
        }

        public void setUsername(String username) {
            this.username = username;
        }

        public String getVote() {
            return vote;
        }

        public void setVote(String vote) {
            this.vote = vote;
        }

        public String getComments() {
            return comments;
        }

        public void setComments(String comments) {
            this.comments = comments;
        }

        public String getContent() {
            return content;
        }

        public void setContent(String content) {
            this.content = content;
        }

        public String getImgpath() {
            return imgpath;
        }

        public void setImgpath(String imgpath) {
            this.imgpath = imgpath;
        }
    }
}

3.创建存放url的类UriPath

package com.example.qiushibaike.utils;

/**
 * ip地址为服务器地址
 */
public class UriPath {
   public static final String huodongPath="http://192.168.3.142:8080/QiuShiSample/QiushiServlet?page=";
}

4.创建自定义adapter基类

package com.example.qiushibaike.utils;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

import java.util.List;

public abstract class MyBaseAdapter<T> extends BaseAdapter {

    private Context context;
    private List<T> datas;

    //提供给子类的布局构造器
    private LayoutInflater layoutInflater;

    public MyBaseAdapter(Context context, List<T> datas){
        this.context = context;
        this.datas = datas;
        layoutInflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return datas.size();
    }



    @Override
    public Object getItem(int position) {
        return datas.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        return getItemView(position,convertView,parent);
    }

    //填充数据
    public void addDatas(List<T> list){
        datas.addAll(list);

        notifyDataSetChanged();
    }
    //清除数据
    public void clearDatas(){
        datas.clear();
        notifyDataSetChanged();
    }

    /**
     * 提供布局填充器的
     * @return
     */
    public LayoutInflater getLayoutInflater(){
        return layoutInflater;
    }


    //给子类用
    public abstract View getItemView(int position, View convertView, ViewGroup parent);
}

5.创建糗事百科adapter类QiushiAdapter

package com.example.qiushibaike.utils;

import android.content.Context;

import android.graphics.Color;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import com.example.qiushibaike.Bean.Datas;
import com.example.qiushibaike.R;
import com.lidroid.xutils.BitmapUtils;
import java.util.HashMap;
import java.util.List;



/**
 * Created by Administrator on 2016/6/22 0022.
 */
public class QiushiAdapter extends MyBaseAdapter<Datas.QiushibaikesBean>{
     private Context context;
     private HashMap<Integer, View> hm = new HashMap<Integer, View>();
     public QiushiAdapter(Context context, List<Datas.QiushibaikesBean> datas) {
        super(context, datas);
        this.context = context;

    }

    @Override
    public View getItemView(int position, View convertView, ViewGroup parent) {
      ViewHolder viewHolder=null;

      if(convertView==null){
            convertView=getLayoutInflater().inflate(R.layout.item11,null);
            viewHolder=new ViewHolder(convertView);
            convertView.setTag(viewHolder);
        }

        else {
            viewHolder= (ViewHolder) convertView.getTag();
        }

        Datas.QiushibaikesBean datasBean= (Datas.QiushibaikesBean) getItem(position);
        viewHolder.textView_name.setText(datasBean.getUsername());
        viewHolder.textView_name.setTextColor(Color.RED);
        viewHolder.textView_title.setText( datasBean.getContent());


        //加载网络图片并在imageview展示
        String url=String.format("http:"+datasBean.getImgurl());
        BitmapUtils bitmapUtils = new BitmapUtils(context);
        bitmapUtils.display(viewHolder.imageView_pic, url);

        String imgpath=String.format("http:"+datasBean.getImgpath());
        BitmapUtils bitmapUtils1 = new BitmapUtils(context);
        bitmapUtils1.display(viewHolder.imageView2, imgpath);


        return convertView;
    }


//条目复用

     public class ViewHolder{
        ImageView imageView_pic,imageView2;
        TextView textView_title,textView_name;

        public ViewHolder(View convertView) {
            imageView_pic=  convertView.findViewById(R.id.imageView1);
            textView_title= convertView.findViewById(R.id.content);
            textView_name=convertView.findViewById(R.id.text_name);
            imageView2=convertView.findViewById(R.id.imageView2);
        }
    }

}

6.MainActivity代码如下

package com.example.qiushibaike;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.LayoutInflater;
import android.widget.AbsListView;
import android.widget.ListView;
import com.example.qiushibaike.Bean.Datas;
import com.example.qiushibaike.utils.QiushiAdapter;
import com.example.qiushibaike.utils.UriPath;
import com.google.gson.Gson;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;
import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    private ListView listView;
    Boolean flag;
    public LayoutInflater inflater;
    public int last_index;
    public int total_index;
    public boolean isLoading = false;//表示是否正处于加载状态
    private List<Datas.QiushibaikesBean> beans;
    private QiushiAdapter qiushiAdapter;
    private int page=1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        listView= (ListView) findViewById(R.id.listView);
        loadData(page);
        System.out.println("ttttttttttttttttttttttttttttttt");
        beans=new ArrayList<>();
        qiushiAdapter=new QiushiAdapter(this,beans);
        listView.setAdapter(qiushiAdapter);
        initListRefresh();

    }


    private void initListRefresh() {
        this.listView.setOnScrollListener(new AbsListView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(AbsListView absListView, int i) {
                isLoading=false;
                if(last_index == total_index && (i == AbsListView.OnScrollListener.SCROLL_STATE_IDLE))
                {
                    //表示此时需要显示刷新视图界面进行新数据的加载(要等滑动停止)
                    if(!isLoading)
                    {

                        //上拉刷新
                        isLoading = true;
                        loadData(++page);
                        Log.i("===page11===",page+"");
                    }
                }
            }

            @Override
            public void onScroll(AbsListView absListView, int i, int i1, int i2) {

                last_index = i+i1;
                total_index = i2;
                flag = ((i+i1) == i2);
                Log.i("===page111===",last_index+"========="+total_index);
            }
        });

    }

//xutils请求数据
    private void loadData(int page) {
        com.lidroid.xutils.HttpUtils httpUtils = new com.lidroid.xutils.HttpUtils();
        Log.i("pagepath===",String.format(UriPath.huodongPath+page));
        httpUtils.send(HttpRequest.HttpMethod.GET, String.format(UriPath.huodongPath+page), new RequestCallBack<String>() {
            @Override
            public void onSuccess(ResponseInfo<String> responseInfo) {
                System.out.println("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa");
                Gson gson = new Gson();
                Datas huodongBean = gson.fromJson(responseInfo.result, Datas.class);
                Log.i("========dddddddd=====",huodongBean+"");
                qiushiAdapter.addDatas(huodongBean.getQiushibaikes());

            }

            @Override
            public void onFailure(HttpException e, String s) {

            }
        });

    }
}

7.mainactivity布局activity_main.xml代码如下

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
    <ListView
        android:id="@+id/listView"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    </ListView>
</LinearLayout>

8.listview条目布局item11.xml代码如下

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:padding="10dp">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:orientation="horizontal">
        <ImageView
            android:id="@+id/imageView1"
            android:layout_width="50dp"
            android:layout_height="50dp" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/text_name"/>

    </LinearLayout>



    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/content"
        />

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:id="@+id/imageView2"/>

</LinearLayout>
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 203,271评论 5 476
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 85,275评论 2 380
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 150,151评论 0 336
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 54,550评论 1 273
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 63,553评论 5 365
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,559评论 1 281
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,924评论 3 395
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,580评论 0 257
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,826评论 1 297
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,578评论 2 320
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,661评论 1 329
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,363评论 4 318
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,940评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,926评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,156评论 1 259
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 42,872评论 2 349
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,391评论 2 342