Okhttp MVC

本文详细介绍了如何在MVC架构中使用Okhttp,涵盖了布局设计、监听器以实现进度条显示、Model层的接口定义及其实现、JavaBean的创建、工具列的重点应用、适配器的使用,以及登录和展示界面的构建过程。

布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    android:orientation="vertical">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="登录注册界面"
        android:layout_gravity="center"
        android:textSize="20sp"
        android:textColor="#000"
        android:layout_marginTop="10dp"/>

    <EditText
        android:id="@+id/name"
        android:hint="用户名"
        android:layout_width="300dp"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_marginTop="100dp"/>

    <EditText
        android:id="@+id/pwd"
        android:hint="密码"
        android:layout_width="300dp"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_marginTop="20dp"/>

    <Button
        android:background="#03A9F4"
        android:text="登录"
        android:textSize="20sp"
        android:id="@+id/login"
        android:layout_gravity="center"
        android:layout_width="300dp"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"/>
    <Button
        android:background="#FF5722"
        android:text="注册"
        android:textSize="20sp"
        android:id="@+id/register"
        android:layout_gravity="center"
        android:layout_width="300dp"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"/>

</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".Main2Activity">
                <ListView
                    android:id="@+id/lv"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"></ListView>
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
    android:layout_height="match_parent">
                <ImageView
                    android:id="@+id/iv"
                    android:layout_width="100dp"
                    android:layout_height="100dp"
                    android:layout_marginTop="10dp"
                    android:layout_marginLeft="10dp"
                    android:src="@mipmap/ic_launcher_round"/>

                <TextView
                    android:id="@+id/tv"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:textSize="20sp"
                    android:text="这是标题"
                    android:layout_marginTop="45dp"
                    android:layout_toRightOf="@+id/iv"
                    android:layout_marginLeft="20dp"
                    android:textColor="#000"
                    />
</RelativeLayout>

LIstener

package com.example.day1102homework.listener;

public interface MyListener {
    void onOk(String ok);
    void onError(String message);
}

可以设置进度条的

package com.example.day1102homework.listener;

public interface MyProgressListener {
    void onError(String message);
    void oonprogress(int progress);
    void finish();
}

Model

一个接口

package com.example.day1102homework.model;

import com.example.day1102homework.listener.MyListener;
import com.example.day1102homework.listener.MyProgressListener;

import java.util.HashMap;

public interface Mymodel {
        void get(String url, MyListener myListener);
        void post(String url, HashMap<String,String> map, final MyListener myListener);
        void download(String url,  String path,  MyProgressListener myProgressListener);
        void upload(String url, String path, String servername,String type, final MyListener myListener);
}

一个类实现这个接口

package com.example.day1102homework.model;

        import com.example.day1102homework.Myutils;
        import com.example.day1102homework.listener.MyListener;
        import com.example.day1102homework.listener.MyProgressListener;

        import java.util.HashMap;

public class Model implements Mymodel {
    @Override
    public void get(String url, MyListener myListener) {
        Myutils.getInstance().doget(url,myListener);
    }

    @Override
    public void post(String url, HashMap<String, String> map, MyListener myListener) {
        Myutils.getInstance().dopost(url,map,myListener);
    }

    @Override
    public void download(String url, String path, MyProgressListener myProgressListener) {
        Myutils.getInstance().dodowuload(url,path,myProgressListener);
    }

    @Override
    public void upload(String url, String path, String servername, String type, MyListener myListener) {
        Myutils.getInstance().upload(url,path,servername,type,myListener);
    }
}

创建一个JavaBean

package com.example.day1102homework;

import java.util.List;

public class JavaBean {

    /**
     * code : 200
     * msg : 成功!
     * data : [{"type":"10","text":"穿袜子总是破洞,有木有跟我一样的脚","user_id":"23176996","name":"百思用户23176996","screen_name":"百思用户23176996","profile_image":"","created_at":"2019-11-03 15:32:02","create_time":null,"passtime":"2019-11-03 15:32:02","love":"73","hate":"10","comment":"7","repost":"0","bookmark":"0","bimageuri":"","voiceuri":null,"voicetime":null,"voicelength":null,"status":"4","theme_id":"55163","theme_name":"主版块","theme_type":"1","videouri":"","videotime":0,"original_pid":"0","cache_version":2,"playcount":null,"playfcount":null,"cai":"10","weixin_url":null,"image1":"http://wimg.spriteapp.cn/ugc/2019/11/02/5dbda482c7ae7_1.jpg","image2":"http://wimg.spriteapp.cn/ugc/2019/11/02/5dbda482c7ae7_1.jpg","is_gif":false,"image0":"http://wimg.spriteapp.cn/ugc/2019/11/02/5dbda482c7ae7_1.jpg","image_small":null,"cdn_img":"http://wimg.spriteapp.cn/ugc/2019/11/02/5dbda482c7ae7_1.jpg","width":"640","height":"852","tag":"","t":1572766322,"ding":"73","favourite":"0","top_cmt":null,"themes":null},{"type":"41","text":"你这特么是谋杀啊\u2026\u2026\u200b","user_id":"22711216","name":"丰恺KING","screen_name":"丰恺KING","profile_image":"http://thirdwx.qlogo.cn/mmopen/vi_32/zHBbcruvQgD2hNv0UgK4luy95ibTKjT6uibibBEoXnxBrOwbVX1Jgxmr5iaZ8GOcSX7oFLtzafiaMN7wGDuQvjP1jbw/132","created_at":"2019-11-03 15:29:02","create_time":null,"passtime":"2019-11-03 15:29:02","love":"95","hate":"7","comment":"11","repost":"0","bookmark":"0","bimageuri":"http://wimg.spriteapp.cn/picture/2019/1103/5dbe03bf76ed6_wpd.jpg","voiceuri":null,"voicetime":null,"voicelength":null,"status":"4","theme_id":"55163","theme_name":"主版块","theme_type":"1","videouri":"http://uvideo.spriteapp.cn/video/2019/1103/5dbe03bf76ed6_wpd.mp4","videotime":24,"original_pid":"0","cache_version":2,"playcount":"1390","playfcount":"367","cai":"7","weixin_url":null,"image1":"http://wimg.spriteapp.cn/picture/2019/1103/5dbe03bf76ed6_wpd.jpg","image2":"http://wimg.spriteapp.cn/picture/2019/1103/5dbe03bf76ed6_wpd.jpg","is_gif":false,"image0":"http://wimg.spriteapp.cn/picture/2019/1103/5dbe03bf76ed6_wpd.jpg","image_small":"http://wimg.spriteapp.cn/picture/2019/1103/5dbe03bf76ed6_wpd.jpg","cdn_img":"http://wimg.spriteapp.cn/picture/2019/1103/5dbe03bf76ed6_wpd.jpg","width":"400","height":"640","tag":"","t":1572766142,"ding":"95","favourite":"0","top_cmt":null,"themes":null},{"type":"41","text":"狗狗的一生其实听短暂,它是人类的过客,可人类是它的一生","user_id":"23129253","name":"木金车","screen_name":"木金车","profile_image":"http://wimg.spriteapp.cn/profile/large/2019/07/04/5d1d74b855eee_mini.jpg","created_at":"2019-11-03 14:59:02","create_time":null,"passtime":"2019-11-03 14:59:02","love":"251","hate":"6","comment":"25","repost":"6","bookmark":"0","bimageuri":"http://wimg.spriteapp.cn/picture/2019/1101/5dbc2af27e1a6_wpd.jpg","voiceuri":null,"voicetime":null,"voicelength":null,"status":"4","theme_id":"56781","theme_name":"情感社区","theme_type":"1","videouri":"http://uvideo.spriteapp.cn/video/2019/1101/5dbc2af27e1a6_wpd.mp4","videotime":27,"original_pid":"0","cache_version":2,"playcount":"3808","playfcount":"236","cai":"6","weixin_url":null,"image1":"http://wimg.spriteapp.cn/picture/2019/1101/5dbc2af27e1a6_wpd.jpg","image2":"http://wimg.spriteapp.cn/picture/2019/1101/5dbc2af27e1a6_wpd.jpg","is_gif":false,"image0":"http://wimg.spriteapp.cn/picture/2019/1101/5dbc2af27e1a6_wpd.jpg","image_small":"http://wimg.spriteapp.cn/picture/2019/1101/5dbc2af27e1a6_wpd.jpg","cdn_img":"http://wimg.spriteapp.cn/picture/2019/1101/5dbc2af27e1a6_wpd.jpg","width":"540","height":"960","tag":"","t":1572764342,"ding":"251","favourite":"0","top_cmt":null,"themes":null},{"type":"10","text":"新娘子:批瓜婆娘","user_id":"15704485","name":"成都李扯火","screen_name":"成都李扯火","profile_image":"http://wimg.spriteapp.cn/profile/large/2019/07/31/5d41627a28fc3_mini.jpg","created_at":"2019-11-03 14:42:02","create_time":null,"passtime":"2019-11-03 14:42:02","love":"62","hate":"1","comment":"6","repost":"0","bookmark":"2","bimageuri":"","voiceuri":null,"voicetime":null,"voicelength":null,"status":"4","theme_id":"55163","theme_name":"主版块","theme_type":"1","videouri":"","videotime":0,"original_pid":"0","cache_version":2,"playcount":null,"playfcount":null,"cai":"1","weixin_url":null,"image1":"http://wimg.spriteapp.cn/ugc/2019/11/03/5dbe4a4691ae1.gif","image2":"http://wimg.spriteapp.cn/ugc/2019/11/03/5dbe4a4691ae1.gif","is_gif":false,"image0":"http://wimg.spriteapp.cn/ugc/2019/11/03/5dbe4a4691ae1.gif","image_small":null,"cdn_img":"http://wimg.spriteapp.cn/ugc/2019/11/03/5dbe4a4691ae1.gif","width":"400","height":"213","tag":"","t":1572763322,"ding":"62","favourite":"2","top_cmt":null,"themes":null},{"type":"41","text":"结伴同行,不限男女","user_id":"23159255","name":"百思用户23159255","screen_name":"百思用户23159255","profile_image":"http://wimg.spriteapp.cn/profile/large/2019/09/12/5d792c8122321_mini.jpg","created_at":"2019-11-03 14:29:02","create_time":null,"passtime":"2019-11-03 14:29:02","love":"118","hate":"9","comment":"21","repost":"2","bookmark":"4","bimageuri":"http://wimg.spriteapp.cn/picture/2019/1102/5dbd71c2bbb87_wpd.jpg","voiceuri":null,"voicetime":null,"voicelength":null,"status":"4","theme_id":"55163","theme_name":"主版块","theme_type":"1","videouri":"http://uvideo.spriteapp.cn/video/2019/1102/5dbd71c2bbb87_wpd.mp4","videotime":15,"original_pid":"0","cache_version":2,"playcount":"2882","playfcount":"687","cai":"9","weixin_url":null,"image1":"http://wimg.spriteapp.cn/picture/2019/1102/5dbd71c2bbb87_wpd.jpg","image2":"http://wimg.spriteapp.cn/picture/2019/1102/5dbd71c2bbb87_wpd.jpg","is_gif":false,"image0":"http://wimg.spriteapp.cn/picture/2019/1102/5dbd71c2bbb87_wpd.jpg","image_small":"http://wimg.spriteapp.cn/picture/2019/1102/5dbd71c2bbb87_wpd.jpg","cdn_img":"http://wimg.spriteapp.cn/picture/2019/1102/5dbd71c2bbb87_wpd.jpg","width":"608","height":"544","tag":"","t":1572762542,"ding":"118","favourite":"4","top_cmt":null,"themes":null},{"type":"41","text":"姐夫们 那个火猛","user_id":"23130735","name":"散落的雪人","screen_name":"散落的雪人","profile_image":"http://wimg.spriteapp.cn/profile/large/2019/07/04/5d1d8a4be0889_mini.jpg","created_at":"2019-11-03 13:58:02","create_time":null,"passtime":"2019-11-03 13:58:02","love":"222","hate":"12","comment":"30","repost":"6","bookmark":"2","bimageuri":"http://wimg.spriteapp.cn/picture/2019/1102/5dbc9acd279d3_wpd.jpg","voiceuri":null,"voicetime":null,"voicelength":null,"status":"4","theme_id":"58191","theme_name":"搞笑视频","theme_type":"1","videouri":"http://uvideo.spriteapp.cn/video/2019/1102/5dbc9acd279d3_wpd.mp4","videotime":19,"original_pid":"0","cache_version":2,"playcount":"10006","playfcount":"910","cai":"12","weixin_url":null,"image1":"http://wimg.spriteapp.cn/picture/2019/1102/5dbc9acd279d3_wpd.jpg","image2":"http://wimg.spriteapp.cn/picture/2019/1102/5dbc9acd279d3_wpd.jpg","is_gif":false,"image0":"http://wimg.spriteapp.cn/picture/2019/1102/5dbc9acd279d3_wpd.jpg","image_small":"http://wimg.spriteapp.cn/picture/2019/1102/5dbc9acd279d3_wpd.jpg","cdn_img":"http://wimg.spriteapp.cn/picture/2019/1102/5dbc9acd279d3_wpd.jpg","width":"540","height":"960","tag":"","t":1572760682,"ding":"222","favourite":"2","top_cmt":null,"themes":null},{"type":"10","text":"可爱又治愈的马卡龙便当🥞🍘🍥","user_id":"11348180","name":"黄羊-5","screen_name":"黄羊-5","profile_image":"http://qzapp.qlogo.cn/qzapp/100336987/F84EBB7B557F528F6716397504B0EAAC/100","created_at":"2019-11-03 13:52:01","create_time":null,"passtime":"2019-11-03 13:52:01","love":"57","hate":"2","comment":"5","repost":"0","bookmark":"1","bimageuri":"","voiceuri":null,"voicetime":null,"voicelength":null,"status":"4","theme_id":"123","theme_name":"美食频道","theme_type":"1","videouri":"","videotime":0,"original_pid":"0","cache_version":2,"playcount":null,"playfcount":null,"cai":"2","weixin_url":null,"image1":"http://wimg.spriteapp.cn/ugc/2019/11/03/5dbe36d9cedf8_1.jpg","image2":"http://wimg.spriteapp.cn/ugc/2019/11/03/5dbe36d9cedf8_1.jpg","is_gif":false,"image0":"http://wimg.spriteapp.cn/ugc/2019/11/03/5dbe36d9cedf8_1.jpg","image_small":null,"cdn_img":"http://wimg.spriteapp.cn/ugc/2019/11/03/5dbe36d9cedf8_1.jpg","width":"750","height":"6514","tag":"","t":1572760321,"ding":"57","favourite":"1","top_cmt":null,"themes":null},{"type":"41","text":"吃剩的柚子皮不要丢掉,放在家中值钱又实用,一年轻松省下不少钱","user_id":"23134674","name":"遇见同桌的她","screen_name":"遇见同桌的她","profile_image":"http://wimg.spriteapp.cn/profile/large/2019/07/04/5d1da983ca10f_mini.jpg","created_at":"2019-11-03 13:28:02","create_time":null,"passtime":"2019-11-03 13:28:02","love":"70","hate":"2","comment":"3","repost":"1","bookmark":"3","bimageuri":"http://wimg.spriteapp.cn/picture/2019/1101/5dbc4318eef6a_wpd.jpg","voiceuri":null,"voicetime":null,"voicelength":null,"status":"4","theme_id":"8466","theme_name":"知识分享","theme_type":"1","videouri":"http://uvideo.spriteapp.cn/video/2019/1101/5dbc4318eef6a_wpd.mp4","videotime":163,"original_pid":"0","cache_version":2,"playcount":"601","playfcount":"9","cai":"2","weixin_url":null,"image1":"http://wimg.spriteapp.cn/picture/2019/1101/5dbc4318eef6a_wpd.jpg","image2":"http://wimg.spriteapp.cn/picture/2019/1101/5dbc4318eef6a_wpd.jpg","is_gif":false,"image0":"http://wimg.spriteapp.cn/picture/2019/1101/5dbc4318eef6a_wpd.jpg","image_small":"http://wimg.spriteapp.cn/picture/2019/1101/5dbc4318eef6a_wpd.jpg","cdn_img":"http://wimg.spriteapp.cn/picture/2019/1101/5dbc4318eef6a_wpd.jpg","width":"852","height":"480","tag":"","t":1572758882,"ding":"70","favourite":"3","top_cmt":null,"themes":null},{"type":"10","text":"一会儿就洗白于脑出血","user_id":"15704485","name":"成都李扯火","screen_name":"成都李扯火","profile_image":"http://wimg.spriteapp.cn/profile/large/2019/07/31/5d41627a28fc3_mini.jpg","created_at":"2019-11-03 13:22:01","create_time":null,"passtime":"2019-11-03 13:22:01","love":"70","hate":"1","comment":"0","repost":"0","bookmark":"0","bimageuri":"","voiceuri":null,"voicetime":null,"voicelength":null,"status":"4","theme_id":"55163","theme_name":"主版块","theme_type":"1","videouri":"","videotime":0,"original_pid":"0","cache_version":2,"playcount":null,"playfcount":null,"cai":"1","weixin_url":null,"image1":"http://wimg.spriteapp.cn/ugc/2019/11/03/5dbe4a12c4e0c.gif","image2":"http://wimg.spriteapp.cn/ugc/2019/11/03/5dbe4a12c4e0c.gif","is_gif":false,"image0":"http://wimg.spriteapp.cn/ugc/2019/11/03/5dbe4a12c4e0c.gif","image_small":null,"cdn_img":"http://wimg.spriteapp.cn/ugc/2019/11/03/5dbe4a12c4e0c.gif","width":"240","height":"320","tag":"","t":1572758521,"ding":"70","favourite":"0","top_cmt":null,"themes":null},{"type":"41","text":"一直都很喜欢kfc的土豆泥🥔今天安排上","user_id":"23133393","name":"小亚","screen_name":"小亚","profile_image":"http://wimg.spriteapp.cn/profile/large/2019/07/04/5d1da0046eec1_mini.jpg","created_at":"2019-11-03 12:57:02","create_time":null,"passtime":"2019-11-03 12:57:02","love":"78","hate":"5","comment":"5","repost":"4","bookmark":"28","bimageuri":"http://wimg.spriteapp.cn/picture/2019/1101/5dbc26420770c_wpd.jpg","voiceuri":null,"voicetime":null,"voicelength":null,"status":"4","theme_id":"123","theme_name":"美食频道","theme_type":"1","videouri":"http://uvideo.spriteapp.cn/video/2019/1101/5dbc26420770c_wpd.mp4","videotime":46,"original_pid":"0","cache_version":2,"playcount":"1008","playfcount":"173","cai":"5","weixin_url":null,"image1":"http://wimg.spriteapp.cn/picture/2019/1101/5dbc26420770c_wpd.jpg","image2":"http://wimg.spriteapp.cn/picture/2019/1101/5dbc26420770c_wpd.jpg","is_gif":false,"image0":"http://wimg.spriteapp.cn/picture/2019/1101/5dbc26420770c_wpd.jpg","image_small":"http://wimg.spriteapp.cn/picture/2019/1101/5dbc26420770c_wpd.jpg","cdn_img":"http://wimg.spriteapp.cn/picture/2019/1101/5dbc26420770c_wpd.jpg","width":"720","height":"480","tag":"","t":1572757022,"ding":"78","favourite":"28","top_cmt":null,"themes":null},{"type":"10","text":"辣妈7哥又和小鲜肉\u201c谈恋爱\u201d了~","user_id":"23164013","name":"13357909009","screen_name":"13357909009","profile_image":"","created_at":"2019-11-03 12:52:01","create_time":null,"passtime":"2019-11-03 12:52:01","love":"71","hate":"20","comment":"5","repost":"0","bookmark":"0","bimageuri":"","voiceuri":null,"voicetime":null,"voicelength":null,"status":"4","theme_id":"55163","theme_name":"主版块","theme_type":"1","videouri":"","videotime":0,"original_pid":"0","cache_version":2,"playcount":null,"playfcount":null,"cai":"20","weixin_url":null,"image1":"http://wimg.spriteapp.cn/ugc/2019/11/02/5dbd15ba9c5f3_1.jpg","image2":"http://wimg.spriteapp.cn/ugc/2019/11/02/5dbd15ba9c5f3_1.jpg","is_gif":false,"image0":"http://wimg.spriteapp.cn/ugc/2019/11/02/5dbd15ba9c5f3_1.jpg","image_small":null,"cdn_img":"http://wimg.spriteapp.cn/ugc/2019/11/02/5dbd15ba9c5f3_1.jpg","width":"480","height":"3761","tag":"","t":1572756721,"ding":"71","favourite":"0","top_cmt":null,"themes":null},{"type":"41","text":"作死!弯道、大车在前,还要玩命超车!","user_id":"20279770","name":"车神大咖","screen_name":"车神大咖","profile_image":"http://wimg.spriteapp.cn/profile/large/2017/03/02/58b7ba6a5e8ae_mini.jpg","created_at":"2019-11-03 12:36:02","create_time":null,"passtime":"2019-11-03 12:36:02","love":"99","hate":"8","comment":"4","repost":"1","bookmark":"4","bimageuri":"http://wimg.spriteapp.cn/picture/2019/1101/5dbb9dbc4e554_wpd.jpg","voiceuri":null,"voicetime":null,"voicelength":null,"status":"4","theme_id":"58091","theme_name":"汽车","theme_type":"1","videouri":"http://uvideo.spriteapp.cn/video/2019/1101/5dbb9dbc4e554_wpd.mp4","videotime":144,"original_pid":"0","cache_version":2,"playcount":"2084","playfcount":"302","cai":"8","weixin_url":null,"image1":"http://wimg.spriteapp.cn/picture/2019/1101/5dbb9dbc4e554_wpd.jpg","image2":"http://wimg.spriteapp.cn/picture/2019/1101/5dbb9dbc4e554_wpd.jpg","is_gif":false,"image0":"http://wimg.spriteapp.cn/picture/2019/1101/5dbb9dbc4e554_wpd.jpg","image_small":"http://wimg.spriteapp.cn/picture/2019/1101/5dbb9dbc4e554_wpd.jpg","cdn_img":"http://wimg.spriteapp.cn/picture/2019/1101/5dbb9dbc4e554_wpd.jpg","width":"960","height":"540","tag":"","t":1572755762,"ding":"99","favourite":"4","top_cmt":null,"themes":null},{"type":"10","text":"生活需要仪式感!","user_id":"22967661","name":"枫林游","screen_name":"枫林游","profile_image":"http://wimg.spriteapp.cn/profile/large/2019/05/30/5cef6e5b920e4_mini.png","created_at":"2019-11-03 12:36:01","create_time":null,"passtime":"2019-11-03 12:36:01","love":"103","hate":"7","comment":"12","repost":"4","bookmark":"5","bimageuri":"","voiceuri":null,"voicetime":null,"voicelength":null,"status":"4","theme_id":"58240","theme_name":"搞笑图片","theme_type":"1","videouri":"","videotime":0,"original_pid":"0","cache_version":2,"playcount":null,"playfcount":null,"cai":"7","weixin_url":null,"image1":"http://wimg.spriteapp.cn/ugc/2019/11/03/5dbe285701cc8.gif","image2":"http://wimg.spriteapp.cn/ugc/2019/11/03/5dbe285701cc8.gif","is_gif":false,"image0":"http://wimg.spriteapp.cn/ugc/2019/11/03/5dbe285701cc8.gif","image_small":null,"cdn_img":"http://wimg.spriteapp.cn/ugc/2019/11/03/5dbe285701cc8.gif","width":"250","height":"193","tag":"","t":1572755761,"ding":"103","favourite":"5","top_cmt":null,"themes":null},{"type":"41","text":"铲屎的,你这是谋杀你知道吗?","user_id":"23133185","name":"小姐姐","screen_name":"小姐姐","profile_image":"http://wimg.spriteapp.cn/profile/large/2019/07/04/5d1d9fd835e2f_mini.jpg","created_at":"2019-11-03 12:26:01","create_time":null,"passtime":"2019-11-03 12:26:01","love":"86","hate":"8","comment":"1","repost":"0","bookmark":"2","bimageuri":"http://wimg.spriteapp.cn/picture/2019/1101/5dbc473361ee0_wpd.jpg","voiceuri":null,"voicetime":null,"voicelength":null,"status":"4","theme_id":"473","theme_name":"社会新鲜事","theme_type":"1","videouri":"http://uvideo.spriteapp.cn/video/2019/1101/5dbc473361ee0_wpd.mp4","videotime":21,"original_pid":"0","cache_version":2,"playcount":"2788","playfcount":"293","cai":"8","weixin_url":null,"image1":"http://wimg.spriteapp.cn/picture/2019/1101/5dbc473361ee0_wpd.jpg","image2":"http://wimg.spriteapp.cn/picture/2019/1101/5dbc473361ee0_wpd.jpg","is_gif":false,"image0":"http://wimg.spriteapp.cn/picture/2019/1101/5dbc473361ee0_wpd.jpg","image_small":"http://wimg.spriteapp.cn/picture/2019/1101/5dbc473361ee0_wpd.jpg","cdn_img":"http://wimg.spriteapp.cn/picture/2019/1101/5dbc473361ee0_wpd.jpg","width":"360","height":"640","tag":"","t":1572755161,"ding":"86","favourite":"2","top_cmt":null,"themes":null},{"type":"10","text":"有才的姐夫\n集合了","user_id":"23130668","name":"若小曦","screen_name":"若小曦","profile_image":"http://wimg.spriteapp.cn/profile/large/2019/07/04/5d1d8a3b1ec59_mini.jpg","created_at":"2019-11-03 12:20:02","create_time":null,"passtime":"2019-11-03 12:20:02","love":"351","hate":"21","comment":"53","repost":"1","bookmark":"0","bimageuri":"","voiceuri":null,"voicetime":null,"voicelength":null,"status":"4","theme_id":"58240","theme_name":"搞笑图片","theme_type":"1","videouri":"","videotime":0,"original_pid":"0","cache_version":2,"playcount":null,"playfcount":null,"cai":"21","weixin_url":null,"image1":"http://wimg.spriteapp.cn/ugc/2019/10/12/5da0b029ef2fb_1.jpg","image2":"http://wimg.spriteapp.cn/ugc/2019/10/12/5da0b029ef2fb_1.jpg","is_gif":false,"image0":"http://wimg.spriteapp.cn/ugc/2019/10/12/5da0b029ef2fb_1.jpg","image_small":null,"cdn_img":"http://wimg.spriteapp.cn/ugc/2019/10/12/5da0b029ef2fb_1.jpg","width":"1125","height":"1102","tag":"","t":1572754802,"ding":"351","favourite":"0","top_cmt":null,"themes":null},{"type":"41","text":"万磁王利用人体一点铁元素,逃出塑料监狱,杀人于无形!","user_id":"23134046","name":"夜独醉","screen_name":"夜独醉","profile_image":"http://wimg.spriteapp.cn/profile/large/2019/07/04/5d1da08f76b3a_mini.jpg","created_at":"2019-11-03 12:16:02","create_time":null,"passtime":"2019-11-03 12:16:02","love":"100","hate":"7","comment":"13","repost":"0","bookmark":"2","bimageuri":"http://wimg.spriteapp.cn/picture/2019/1101/5dbc4c47accf6_wpd.jpg","voiceuri":null,"voicetime":null,"voicelength":null,"status":"4","theme_id":"58191","theme_name":"搞笑视频","theme_type":"1","videouri":"http://uvideo.spriteapp.cn/video/2019/1101/5dbc4c47accf6_wpd.mp4","videotime":144,"original_pid":"0","cache_version":2,"playcount":"2428","playfcount":"976","cai":"7","weixin_url":null,"image1":"http://wimg.spriteapp.cn/picture/2019/1101/5dbc4c47accf6_wpd.jpg","image2":"http://wimg.spriteapp.cn/picture/2019/1101/5dbc4c47accf6_wpd.jpg","is_gif":false,"image0":"http://wimg.spriteapp.cn/picture/2019/1101/5dbc4c47accf6_wpd.jpg","image_small":"http://wimg.spriteapp.cn/picture/2019/1101/5dbc4c47accf6_wpd.jpg","cdn_img":"http://wimg.spriteapp.cn/picture/2019/1101/5dbc4c47accf6_wpd.jpg","width":"852","height":"480","tag":"","t":1572754562,"ding":"100","favourite":"2","top_cmt":null,"themes":null},{"type":"41","text":"LEC和LCK最后的火种重现MSI对决\n\nLPL新科冠军FPX对战全球总决赛卫冕冠军iG","user_id":"23133321","name":"小狮子","screen_name":"小狮子","profile_image":"http://wimg.spriteapp.cn/profile/large/2019/07/04/5d1d9ff4b31eb_mini.jpg","created_at":"2019-11-03 12:06:01","create_time":null,"passtime":"2019-11-03 12:06:01","love":"87","hate":"8","comment":"7","repost":"0","bookmark":"0","bimageuri":"http://wimg.spriteapp.cn/picture/2019/1102/5dbc6c236a18a_wpd.jpg","voiceuri":null,"voicetime":null,"voicelength":null,"status":"4","theme_id":"164","theme_name":"游戏","theme_type":"1","videouri":"http://uvideo.spriteapp.cn/video/2019/1102/5dbc6c236a18a_wpd.mp4","videotime":165,"original_pid":"0","cache_version":2,"playcount":"1589","playfcount":"97","cai":"8","weixin_url":null,"image1":"http://wimg.spriteapp.cn/picture/2019/1102/5dbc6c236a18a_wpd.jpg","image2":"http://wimg.spriteapp.cn/picture/2019/1102/5dbc6c236a18a_wpd.jpg","is_gif":false,"image0":"http://wimg.spriteapp.cn/picture/2019/1102/5dbc6c236a18a_wpd.jpg","image_small":"http://wimg.spriteapp.cn/picture/2019/1102/5dbc6c236a18a_wpd.jpg","cdn_img":"http://wimg.spriteapp.cn/picture/2019/1102/5dbc6c236a18a_wpd.jpg","width":"852","height":"480","tag":"","t":1572753961,"ding":"87","favourite":"0","top_cmt":null,"themes":null},{"type":"10","text":"你随意。我无敌","user_id":"23126369","name":"浮生半百","screen_name":"浮生半百","profile_image":"http://wimg.spriteapp.cn/profile/large/2019/07/04/5d1d6b17d839d_mini.jpg","created_at":"2019-11-03 12:04:01","create_time":null,"passtime":"2019-11-03 12:04:01","love":"323","hate":"16","comment":"44","repost":"4","bookmark":"4","bimageuri":"","voiceuri":null,"voicetime":null,"voicelength":null,"status":"4","theme_id":"58240","theme_name":"搞笑图片","theme_type":"1","videouri":"","videotime":0,"original_pid":"0","cache_version":2,"playcount":null,"playfcount":null,"cai":"16","weixin_url":null,"image1":"http://wimg.spriteapp.cn/ugc/2019/10/25/5db314068ed4e.gif","image2":"http://wimg.spriteapp.cn/ugc/2019/10/25/5db314068ed4e.gif","is_gif":false,"image0":"http://wimg.spriteapp.cn/ugc/2019/10/25/5db314068ed4e.gif","image_small":null,"cdn_img":"http://wimg.spriteapp.cn/ugc/2019/10/25/5db314068ed4e.gif","width":"323","height":"283","tag":"","t":1572753841,"ding":"323","favourite":"4","top_cmt":null,"themes":null},{"type":"41","text":"啊雄出海布下陷阱赶紧回家,威胁旺旺不给它找老婆,旺旺不乐意了","user_id":"23024005","name":"小时候可俊了","screen_name":"小时候可俊了","profile_image":"http://qzapp.qlogo.cn/qzapp/100336987/E1EF975C0C78525705204B484D3E0DD8/100","created_at":"2019-11-03 11:56:02","create_time":null,"passtime":"2019-11-03 11:56:02","love":"97","hate":"17","comment":"1","repost":"1","bookmark":"0","bimageuri":"http://wimg.spriteapp.cn/picture/2019/1102/5dbd093d0dc64__b.jpg","voiceuri":null,"voicetime":null,"voicelength":null,"status":"4","theme_id":"0","theme_name":"","theme_type":"0","videouri":"http://uvideo.spriteapp.cn/video/2019/1102/5dbd094058159_wpd.mp4","videotime":325,"original_pid":"0","cache_version":2,"playcount":"1412","playfcount":"137","cai":"17","weixin_url":null,"image1":"http://wimg.spriteapp.cn/picture/2019/1102/5dbd093d0dc64__b.jpg","image2":"http://wimg.spriteapp.cn/picture/2019/1102/5dbd093d0dc64__b.jpg","is_gif":false,"image0":"http://wimg.spriteapp.cn/picture/2019/1102/5dbd093d0dc64__b.jpg","image_small":"http://wimg.spriteapp.cn/picture/2019/1102/5dbd093d0dc64__b.jpg","cdn_img":"http://wimg.spriteapp.cn/picture/2019/1102/5dbd093d0dc64__b.jpg","width":"640","height":"360","tag":"","t":1572753362,"ding":"97","favourite":"0","top_cmt":null,"themes":null},{"type":"10","text":"看便便颜色,就知道你哪里老化!","user_id":"23164013","name":"13357909009","screen_name":"13357909009","profile_image":"","created_at":"2019-11-03 11:48:01","create_time":null,"passtime":"2019-11-03 11:48:01","love":"75","hate":"5","comment":"7","repost":"2","bookmark":"2","bimageuri":"","voiceuri":null,"voicetime":null,"voicelength":null,"status":"4","theme_id":"55163","theme_name":"主版块","theme_type":"1","videouri":"","videotime":0,"original_pid":"0","cache_version":2,"playcount":null,"playfcount":null,"cai":"5","weixin_url":null,"image1":"http://wimg.spriteapp.cn/ugc/2019/11/02/5dbd15ca4dd02_1.jpg","image2":"http://wimg.spriteapp.cn/ugc/2019/11/02/5dbd15ca4dd02_1.jpg","is_gif":false,"image0":"http://wimg.spriteapp.cn/ugc/2019/11/02/5dbd15ca4dd02_1.jpg","image_small":null,"cdn_img":"http://wimg.spriteapp.cn/ugc/2019/11/02/5dbd15ca4dd02_1.jpg","width":"480","height":"4273","tag":"","t":1572752881,"ding":"75","favourite":"2","top_cmt":null,"themes":null}]
     */

    private int code;
    private String msg;
    private List<DataBean> data;

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public List<DataBean> getData() {
        return data;
    }

    public void setData(List<DataBean> data) {
        this.data = data;
    }

    public static class DataBean {
        /**
         * type : 10
         * text : 穿袜子总是破洞,有木有跟我一样的脚
         * user_id : 23176996
         * name : 百思用户23176996
         * screen_name : 百思用户23176996
         * profile_image :
         * created_at : 2019-11-03 15:32:02
         * create_time : null
         * passtime : 2019-11-03 15:32:02
         * love : 73
         * hate : 10
         * comment : 7
         * repost : 0
         * bookmark : 0
         * bimageuri :
         * voiceuri : null
         * voicetime : null
         * voicelength : null
         * status : 4
         * theme_id : 55163
         * theme_name : 主版块
         * theme_type : 1
         * videouri :
         * videotime : 0
         * original_pid : 0
         * cache_version : 2
         * playcount : null
         * playfcount : null
         * cai : 10
         * weixin_url : null
         * image1 : http://wimg.spriteapp.cn/ugc/2019/11/02/5dbda482c7ae7_1.jpg
         * image2 : http://wimg.spriteapp.cn/ugc/2019/11/02/5dbda482c7ae7_1.jpg
         * is_gif : false
         * image0 : http://wimg.spriteapp.cn/ugc/2019/11/02/5dbda482c7ae7_1.jpg
         * image_small : null
         * cdn_img : http://wimg.spriteapp.cn/ugc/2019/11/02/5dbda482c7ae7_1.jpg
         * width : 640
         * height : 852
         * tag :
         * t : 1572766322
         * ding : 73
         * favourite : 0
         * top_cmt : null
         * themes : null
         */

        private String type;
        private String text;
        private String user_id;
        private String name;
        private String screen_name;
        private String profile_image;
        private String created_at;
        private Object create_time;
        private String passtime;
        private String love;
        private String hate;
        private String comment;
        private String repost;
        private String bookmark;
        private String bimageuri;
        private Object voiceuri;
        private Object voicetime;
        private Object voicelength;
        private String status;
        private String theme_id;
        private String theme_name;
        private String theme_type;
        private String videouri;
        private int videotime;
        private String original_pid;
        private int cache_version;
        private Object playcount;
        private Object playfcount;
        private String cai;
        private Object weixin_url;
        private String image1;
        private String image2;
        private boolean is_gif;
        private String image0;
        private Object image_small;
        private String cdn_img;
        private String width;
        private String height;
        private String tag;
        private int t;
        private String ding;
        private String favourite;
        private Object top_cmt;
        private Object themes;

        public String getType() {
            return type;
        }

        public void setType(String type) {
            this.type = type;
        }

        public String getText() {
            return text;
        }

        public void setText(String text) {
            this.text = text;
        }

        public String getUser_id() {
            return user_id;
        }

        public void setUser_id(String user_id) {
            this.user_id = user_id;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public String getScreen_name() {
            return screen_name;
        }

        public void setScreen_name(String screen_name) {
            this.screen_name = screen_name;
        }

        public String getProfile_image() {
            return profile_image;
        }

        public void setProfile_image(String profile_image) {
            this.profile_image = profile_image;
        }

        public String getCreated_at() {
            return created_at;
        }

        public void setCreated_at(String created_at) {
            this.created_at = created_at;
        }

        public Object getCreate_time() {
            return create_time;
        }

        public void setCreate_time(Object create_time) {
            this.create_time = create_time;
        }

        public String getPasstime() {
            return passtime;
        }

        public void setPasstime(String passtime) {
            this.passtime = passtime;
        }

        public String getLove() {
            return love;
        }

        public void setLove(String love) {
            this.love = love;
        }

        public String getHate() {
            return hate;
        }

        public void setHate(String hate) {
            this.hate = hate;
        }

        public String getComment() {
            return comment;
        }

        public void setComment(String comment) {
            this.comment = comment;
        }

        public String getRepost() {
            return repost;
        }

        public void setRepost(String repost) {
            this.repost = repost;
        }

        public String getBookmark() {
            return bookmark;
        }

        public void setBookmark(String bookmark) {
            this.bookmark = bookmark;
        }

        public String getBimageuri() {
            return bimageuri;
        }

        public void setBimageuri(String bimageuri) {
            this.bimageuri = bimageuri;
        }

        public Object getVoiceuri() {
            return voiceuri;
        }

        public void setVoiceuri(Object voiceuri) {
            this.voiceuri = voiceuri;
        }

        public Object getVoicetime() {
            return voicetime;
        }

        public void setVoicetime(Object voicetime) {
            this.voicetime = voicetime;
        }

        public Object getVoicelength() {
            return voicelength;
        }

        public void setVoicelength(Object voicelength) {
            this.voicelength = voicelength;
        }

        public String getStatus() {
            return status;
        }

        public void setStatus(String status) {
            this.status = status;
        }

        public String getTheme_id() {
            return theme_id;
        }

        public void setTheme_id(String theme_id) {
            this.theme_id = theme_id;
        }

        public String getTheme_name() {
            return theme_name;
        }

        public void setTheme_name(String theme_name) {
            this.theme_name = theme_name;
        }

        public String getTheme_type() {
            return theme_type;
        }

        public void setTheme_type(String theme_type) {
            this.theme_type = theme_type;
        }

        public String getVideouri() {
            return videouri;
        }

        public void setVideouri(String videouri) {
            this.videouri = videouri;
        }

        public int getVideotime() {
            return videotime;
        }

        public void setVideotime(int videotime) {
            this.videotime = videotime;
        }

        public String getOriginal_pid() {
            return original_pid;
        }

        public void setOriginal_pid(String original_pid) {
            this.original_pid = original_pid;
        }

        public int getCache_version() {
            return cache_version;
        }

        public void setCache_version(int cache_version) {
            this.cache_version = cache_version;
        }

        public Object getPlaycount() {
            return playcount;
        }

        public void setPlaycount(Object playcount) {
            this.playcount = playcount;
        }

        public Object getPlayfcount() {
            return playfcount;
        }

        public void setPlayfcount(Object playfcount) {
            this.playfcount = playfcount;
        }

        public String getCai() {
            return cai;
        }

        public void setCai(String cai) {
            this.cai = cai;
        }

        public Object getWeixin_url() {
            return weixin_url;
        }

        public void setWeixin_url(Object weixin_url) {
            this.weixin_url = weixin_url;
        }

        public String getImage1() {
            return image1;
        }

        public void setImage1(String image1) {
            this.image1 = image1;
        }

        public String getImage2() {
            return image2;
        }

        public void setImage2(String image2) {
            this.image2 = image2;
        }

        public boolean isIs_gif() {
            return is_gif;
        }

        public void setIs_gif(boolean is_gif) {
            this.is_gif = is_gif;
        }

        public String getImage0() {
            return image0;
        }

        public void setImage0(String image0) {
            this.image0 = image0;
        }

        public Object getImage_small() {
            return image_small;
        }

        public void setImage_small(Object image_small) {
            this.image_small = image_small;
        }

        public String getCdn_img() {
            return cdn_img;
        }

        public void setCdn_img(String cdn_img) {
            this.cdn_img = cdn_img;
        }

        public String getWidth() {
            return width;
        }

        public void setWidth(String width) {
            this.width = width;
        }

        public String getHeight() {
            return height;
        }

        public void setHeight(String height) {
            this.height = height;
        }

        public String getTag() {
            return tag;
        }

        public void setTag(String tag) {
            this.tag = tag;
        }

        public int getT() {
            return t;
        }

        public void setT(int t) {
            this.t = t;
        }

        public String getDing() {
            return ding;
        }

        public void setDing(String ding) {
            this.ding = ding;
        }

        public String getFavourite() {
            return favourite;
        }

        public void setFavourite(String favourite) {
            this.favourite = favourite;
        }

        public Object getTop_cmt() {
            return top_cmt;
        }

        public void setTop_cmt(Object top_cmt) {
            this.top_cmt = top_cmt;
        }

        public Object getThemes() {
            return themes;
        }

        public void setThemes(Object themes) {
            this.themes = themes;
        }
    }
}

工具列(重点)

package com.example.day1102homework;

import android.os.Handler;

import com.example.day1102homework.listener.MyListener;
import com.example.day1102homework.listener.MyProgressListener;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;


import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.Interceptor;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
import okhttp3.logging.HttpLoggingInterceptor;

public class Myutils {
    private static Handler handler = new Handler() ;
    //单例模式
    private static OkHttpClient client = null;
    private Myutils(){
        //log拦截器
        HttpLoggingInterceptor httpLoggingInterceptor = new HttpLoggingInterceptor();
        httpLoggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);//设置等级
        //token拦截器
        Interceptor interceptor = new Interceptor() {
            @Override
            public Response intercept(Chain chain) throws IOException {
                Request build = chain.request().newBuilder().header("token", "luan666").build();
                //网址
//                String host = chain.request().url().host();
                return chain.proceed(build);
            }
        };

        client = new OkHttpClient.Builder()
                .connectTimeout(20, TimeUnit.SECONDS)
                .readTimeout(20,TimeUnit.SECONDS)
                .build();


    }
    private static  Myutils myutils = null;
    public static Myutils getInstance(){
        if (myutils == null){
            synchronized (Object.class){
                if (myutils == null){
                    myutils = new Myutils();
                }
            }
        }
        return myutils;
    }

    //Get请求
    public static void doget(String url, final MyListener myListener){
        Request request = new Request.Builder()
                .url(url)
                .get()
                .build();
        client.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                final String message = e.getMessage();
                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        myListener.onError(message);
                    }
                });
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                final String string = response.body().string();
                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        myListener.onOk(string);
                    }
                });
            }
        });
    }




    //Post请求
    public static void dopost(String url, HashMap<String,String> map, final MyListener myListener){
        FormBody.Builder builder = new FormBody.Builder();
        Set<Map.Entry<String, String>> entries = map.entrySet();
        for (Map.Entry<String, String> entry : entries) {
            builder.add(entry.getKey(),entry.getValue());
        }

        Request request = new Request.Builder()
                .post(builder.build())
                .url(url)
                .build();

        client.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                final String message = e.getMessage();
                handler.post(new Runnable() {
                    @Override
                    public void run() {
                            myListener.onError(message);
                    }
                });
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                final String string = response.body().string();
                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        myListener.onOk(string);
                    }
                });
            }
        });
    }


    //下载
    public static void dodowuload(String url, final String path, final MyProgressListener myProgressListener){
        Request request = new Request.Builder()
                .url(url)
                .get()
                .build();
        client.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                final String message = e.getMessage();
                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        myProgressListener.onError(message);
                    }
                });
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                long length = response.body().contentLength();//总长度
                InputStream is = response.body().byteStream();
                FileOutputStream fileOutputStream = new FileOutputStream(path);
                int len = 0;
                int count = 0;
                byte[] bytes = new byte[1024];
                while ((len = is.read(bytes))!=-1){
                    fileOutputStream.write(bytes,0,len);

                    count += len;

                    final int progress = (int) (count*100/length);

                    handler.post(new Runnable() {
                        @Override
                        public void run() {
                                myProgressListener.oonprogress(progress);
                         }
                    });
                }
                handler.post(new Runnable() {
                    @Override
                    public void run() {
                            myProgressListener.finish();
                    }
                });
            }
        });
    }


    //上传
    public static void upload(String url, String path, String servername,String type, final MyListener myListener){
        RequestBody requestBody = RequestBody.create(MediaType.parse(type),new File(path));
        MultipartBody build = new MultipartBody.Builder()
                .addFormDataPart("file",servername,requestBody)
                .setType(MultipartBody.FORM)
                .build();
        Request request = new Request.Builder()
                .post(build)
                .url(url)
                .build();

        client.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                final String message = e.getMessage();
                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        myListener.onError(message);
                    }
                });
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                        handler.post(new Runnable() {
                            @Override
                            public void run() {myListener.onOk("上传成功");
                            }
                     });
            }
        });
    }




}

适配器

package com.example.day1102homework;

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

import com.bumptech.glide.Glide;

import java.util.List;

public class MyAdapter extends BaseAdapter {
    private List<JavaBean.DataBean> list;
    private Context context;
    private LayoutInflater layoutInflater;

    public MyAdapter(List<JavaBean.DataBean> list, Context context) {
        this.list = list;
        this.context = context;
        layoutInflater = LayoutInflater.from(context);
    }

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

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

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

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView == null){
            holder = new ViewHolder();
            convertView = layoutInflater.inflate(R.layout.layout_item,null);
            holder.iv = convertView.findViewById(R.id.iv);
            holder.tv = convertView.findViewById(R.id.tv);
            convertView.setTag(holder);
        }else{
            holder = (ViewHolder) convertView.getTag();
        }
        holder.tv.setText(list.get(position).getText());
        Glide.with(context).load(list.get(position).getImage1()).into(holder.iv);
        return convertView;
    }
    class ViewHolder{
        ImageView iv;
        TextView tv;
    }
}

登录界面

package com.example.day1102homework;

import android.Manifest;
import android.content.Intent;
import android.os.Build;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.example.day1102homework.listener.MyListener;
import com.example.day1102homework.model.Model;
import com.example.day1102homework.model.Mymodel;
import com.google.gson.JsonObject;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;

public class MainActivity extends AppCompatActivity {
    private static final String TAG = "MainActivity";

    private EditText name;
    private EditText pwd;
    private Button login;
    private Button register;


    private String token_url="https://www.apiopen.top/createUserKey?appId=com.github.wlsj&passwd=123456";
    private String appkey;
    private Mymodel  m = new Model();
    private String  register_url="https://www.apiopen.top/createUser";
    private String login_url="https://www.apiopen.top/login";


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        name = (EditText) findViewById(R.id.name);
        pwd = (EditText) findViewById(R.id.pwd);
        login = (Button) findViewById(R.id.login);
        register = (Button) findViewById(R.id.register);

        //动态获取权限
        if (Build.VERSION.SDK_INT>= Build.VERSION_CODES.M){
            requestPermissions(new String[]{Manifest.permission.READ_EXTERNAL_STORAGE,Manifest.permission.WRITE_EXTERNAL_STORAGE},100);
        }

        //开始就获取token
        Myutils.getInstance().doget(token_url, new MyListener() {
            @Override
            public void onOk(String ok) {
                String s = ok.toString();
                try {
                    JSONObject jsonObject = new JSONObject(s);
                    int code = jsonObject.getInt("code");
                    Log.i(TAG, "onOk: "+code);
                    if (code == 200){
                        JSONObject data = jsonObject.getJSONObject("data");
                        appkey = data.getString("appkey");
                    }
                    Log.i(TAG, "onOk: "+appkey);

                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
            @Override
            public void onError(String message) {
                Toast.makeText(MainActivity.this, "token获取失败", Toast.LENGTH_SHORT).show();
            }
        });


        //点击进行注册
//        key=00d91e8e0cca2b76f515926a36db68f5  值为你之前得到的token
//        phone=13594347817 用户名/ID 唯一
//        passwd=123654 密码
        register.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String s = name.getText().toString();
                String s1 = pwd.getText().toString();
                HashMap<String, String> map = new HashMap<>();
                map.put("key",appkey);
                map.put("phone",s);
                map.put("passwd",s1);
                m.post(register_url, map, new MyListener() {
                    @Override
                    public void onOk(String ok) {
                        try {
                            JSONObject jsonObject = new JSONObject(ok);
                            int code = jsonObject.getInt("code");
                            if (code == 200){
                                Toast.makeText(MainActivity.this, "注册成功", Toast.LENGTH_SHORT).show();
                            }else if (code == 202){
                                Toast.makeText(MainActivity.this, "已经被注册", Toast.LENGTH_SHORT).show();
                            }       
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }

                    @Override
                    public void onError(String message) {
                        Toast.makeText(MainActivity.this, "注册失败"+message, Toast.LENGTH_SHORT).show();
                    }
                });
            }
        });


//        必传参数
//         key=00d91e8e0cca2b76f515926a36db68f5 值为你之前得到的token
//        phone=13594347817 用户名/ID 唯一
//         passwd=123654 密码

        //点击登录
        login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String s = name.getText().toString();
                String s1 = pwd.getText().toString();
                HashMap<String, String> map = new HashMap<>();
                map.put("key",appkey);
                map.put("phone",s);
                map.put("passwd",s1);
                m.post(login_url, map, new MyListener() {
                    @Override
                    public void onOk(String ok) {
                        try {
                            JSONObject jsonObject = new JSONObject(ok);
                            int code = jsonObject.getInt("code");
                            if (code == 200){
                                Intent intent = new Intent(MainActivity.this, Main2Activity.class);
                                startActivity(intent);
                                Toast.makeText(MainActivity.this, "登录成功", Toast.LENGTH_SHORT).show();
                                finish();
                            }else if(code == 202){
                                Toast.makeText(MainActivity.this, "用户名或密码错误", Toast.LENGTH_SHORT).show();
                            }   
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }

                    @Override
                    public void onError(String message) {
                        Toast.makeText(MainActivity.this, "登录失败"+message, Toast.LENGTH_SHORT).show();
                    }
                });
            }
        });

    }
}

展示界面

package com.example.day1102homework;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;

import com.example.day1102homework.listener.MyListener;
import com.example.day1102homework.listener.MyProgressListener;
import com.example.day1102homework.model.Model;
import com.example.day1102homework.model.Mymodel;
import com.google.gson.Gson;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

public class Main2Activity extends AppCompatActivity {

    private static final String TAG = "Main2Activity";
    private String url="https://www.apiopen.top/satinApi?type=1&page=2";
    private Mymodel m = new Model();
    private ListView lv;
    private List<JavaBean.DataBean> list = new ArrayList<>();
    private MyAdapter myAdapter;
    private String upload_utl="http://192.168.43.145/hfs/";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);

        lv = (ListView) findViewById(R.id.lv);
        //获取数据
        m.get(url, new MyListener() {
            @Override
            public void onOk(String ok) {
                Log.i(TAG, "onOk: "+ok);
                JavaBean javaBean = new Gson().fromJson(ok, JavaBean.class);
                List<JavaBean.DataBean> data = javaBean.getData();
                list.addAll(data);
                //设置适配器
                myAdapter = new MyAdapter(list,Main2Activity.this);
                lv.setAdapter(myAdapter);
            }

            @Override
            public void onError(String message) {
                Toast.makeText(Main2Activity.this, "抖音数据请求失败"+message, Toast.LENGTH_SHORT).show();
            }
        });


        //设置短点击
        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                String videouri = list.get(position).getVideouri();
                if (videouri!=""){
                    m.download(videouri, "/storage/emulated/0/Movies/Video" + position + ".mp4", new MyProgressListener() {
                        @Override
                        public void onError(String message) {
                            Toast.makeText(Main2Activity.this, "视频下载失败"+message, Toast.LENGTH_SHORT).show();
                        }

                        @Override
                        public void oonprogress(int progress) {
                            //设置进度
                        }

                        @Override
                        public void finish() {
                            Toast.makeText(Main2Activity.this, "下载成功", Toast.LENGTH_SHORT).show();
                        }
                    });
                }else{
                    Toast.makeText(Main2Activity.this, "没有下载地址", Toast.LENGTH_SHORT).show();
                }
            }
        });

        //长点击
        lv.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
                File file = new File("/storage/emulated/0/Movies/Video" + position + ".mp4");
                if (file.exists()){
                    m.upload(upload_utl, "/storage/emulated/0/Movies/Video" + position + ".mp4", "UploadVideo" + position + ".mp4", "media/mp4", new MyListener() {
                        @Override
                        public void onOk(String ok) {
                            Toast.makeText(Main2Activity.this, ""+ok, Toast.LENGTH_SHORT).show();
                        }

                        @Override
                        public void onError(String message) {
                            Toast.makeText(Main2Activity.this, "下载失败"+message, Toast.LENGTH_SHORT).show();
                        }
                    });
                }else{
                    Toast.makeText(Main2Activity.this, "本地没有这个视频,请下载", Toast.LENGTH_SHORT).show();
                }
                
                return true;
            }
        });


    }
}

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值