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

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


python爬虫+JavaWeb接口+Android完整的小项目开发案例(3)_第1张图片
image.png

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

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

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

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

 

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 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 getQiushibaikes() {
        return qiushibaikes;
    }

    public void setQiushibaikes(List 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 extends BaseAdapter {

    private Context context;
    private List datas;

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

    public MyBaseAdapter(Context context, List 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 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{
     private Context context;
     private HashMap hm = new HashMap();
     public QiushiAdapter(Context context, List 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 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() {
            @Override
            public void onSuccess(ResponseInfo 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代码如下



    
    

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




    
        

        

    



    

    


你可能感兴趣的:(python爬虫+JavaWeb接口+Android完整的小项目开发案例(3))