美文网首页
Retrofit简单使用

Retrofit简单使用

作者: ApolloGuibo | 来源:发表于2017-11-08 14:44 被阅读0次

    这里只讲我自己Retrofit入门时使用的get请求和post请求。
    1.在module的gradle文件中添加依赖

     compile 'com.squareup.retrofit2:retrofit:' + version
     compile 'com.squareup.retrofit2:converter-gson:' + version
     compile 'com.squareup.okhttp3:logging-interceptor:3.5.0'//log拦截器,Debug模式下详细输出,正式版只输出返回码等
    

    2.创建Retrofit管理类,使用单例模式。

    public class RetrofitWrapper {
    
        private static RetrofitWrapper instance;
        private Context mContext;
        private static Retrofit retrofit;
    
        private RetrofitWrapper() {
            retrofit = new Retrofit.Builder()
                    .baseUrl(Constant.BASE_URL_CRM)
                    .addConverterFactory(GsonConverterFactory.create())
                    .client(getOptionSocketFactory())//设置配置好的okHttpClient,信任所有证书
                    .build();
        }
    
        public static RetrofitWrapper getInstance() {
            if (instance == null) {
                synchronized (RetrofitWrapper.class) {
                    if (instance == null) {
                        instance = new RetrofitWrapper();
    
                    }
                }
            }
            return instance;
        }
    
        public Object create(Class service) {
            return retrofit.create(service);
        }
    
    
        /**
         * 配置OkHttpClient
         *
         * @return
         */
        private OkHttpClient getOptionSocketFactory() {
            //创建OkHttpClient.build进行信任所有证书配置
            OkHttpClient.Builder okhttpClient = new OkHttpClient().newBuilder();
            //信任所有服务器地址
            okhttpClient.hostnameVerifier(new HostnameVerifier() {
                @Override
                public boolean verify(String s, SSLSession sslSession) {
                    //设置为true
                    return true;
                }
            });
            //创建管理器
            TrustManager[] trustAllCerts = new TrustManager[]{new X509TrustManager() {
                @Override
                public void checkClientTrusted(
                        java.security.cert.X509Certificate[] x509Certificates,
                        String s) throws java.security.cert.CertificateException {
                }
    
                @Override
                public void checkServerTrusted(
                        java.security.cert.X509Certificate[] x509Certificates,
                        String s) throws java.security.cert.CertificateException {
                }
    
                @Override
                public java.security.cert.X509Certificate[] getAcceptedIssuers() {
                    return new java.security.cert.X509Certificate[]{};
                }
            }};
            try {
                SSLContext sslContext = SSLContext.getInstance("TLS");
                sslContext.init(null, trustAllCerts, new java.security.SecureRandom());
    
    
                SSLSocketFactory socketFactory = sslContext.getSocketFactory();
    
                okhttpClient
                        .connectTimeout(10, TimeUnit.SECONDS)//连接超时时间10s
                        .readTimeout(20, TimeUnit.SECONDS)//读取超时时间20s
                        .sslSocketFactory(socketFactory)
                        .retryOnConnectionFailure(true)//开启错误重连
                ;
    
    
            } catch (Exception e) {
                e.printStackTrace();
            }
    
            return okhttpClient.build();
        }
    }
    

    3.Get请求:url:http://c.api.budejie.com/topic/list/jingxuan/10/budejie-android-6.8.4/0-20.json?market=tencentyingyongbao&ver=6.8.4&visiting=21479789&os=7.0&appname=baisibudejie&client=android&udid=863696030798111&mac=a4%3Aca%3Aa0%3A17%3A18%3Ad7

    生成实体类:

    
    public class BudejieBean {
    
        private InfoBean info;
        private List< ListBean > list;
    
        public InfoBean getInfo() {
            return info;
        }
    
        public void setInfo( InfoBean info ) {
            this.info = info;
        }
    
        public List< ListBean > getList() {
            return list;
        }
    
        public void setList( List< ListBean > list ) {
            this.list = list;
        }
    
        public static class InfoBean {
            /**
             * count : 4085
             * np : 1508715362
             */
    
            private int count;
            private int np;
    
            public int getCount() {
                return count;
            }
    
            public void setCount( int count ) {
                this.count = count;
            }
    
            public int getNp() {
                return np;
            }
    
            public void setNp( int np ) {
                this.np = np;
            }
        }
    
        public static class ListBean {
    
    
            private int status;
            private String comment;
            private String bookmark;
            private String text;
            private GifBean gif;
            private String up;
            private String share_url;
            private int down;
            private int forward;
            private UBean u;
            private String passtime;
            private String type;
            private String id;
            private ImageBean image;
            private TopCommentBean top_comment;
            private List< TopCommentsBean > top_comments;
            private List< TagsBean > tags;
    
            public int getStatus() {
                return status;
            }
    
            public void setStatus( int status ) {
                this.status = status;
            }
    
            public String getComment() {
                return comment;
            }
    
            public void setComment( String comment ) {
                this.comment = comment;
            }
    
            public String getBookmark() {
                return bookmark;
            }
    
            public void setBookmark( String bookmark ) {
                this.bookmark = bookmark;
            }
    
            public String getText() {
                return text;
            }
    
            public void setText( String text ) {
                this.text = text;
            }
    
            public GifBean getGif() {
                return gif;
            }
    
            public void setGif( GifBean gif ) {
                this.gif = gif;
            }
    
            public String getUp() {
                return up;
            }
    
            public void setUp( String up ) {
                this.up = up;
            }
    
            public String getShare_url() {
                return share_url;
            }
    
            public void setShare_url( String share_url ) {
                this.share_url = share_url;
            }
    
            public int getDown() {
                return down;
            }
    
            public void setDown( int down ) {
                this.down = down;
            }
    
            public int getForward() {
                return forward;
            }
    
            public void setForward( int forward ) {
                this.forward = forward;
            }
    
            public UBean getU() {
                return u;
            }
    
            public void setU( UBean u ) {
                this.u = u;
            }
    
            public String getPasstime() {
                return passtime;
            }
    
            public void setPasstime( String passtime ) {
                this.passtime = passtime;
            }
    
            public String getType() {
                return type;
            }
    
            public void setType( String type ) {
                this.type = type;
            }
    
            public String getId() {
                return id;
            }
    
            public void setId( String id ) {
                this.id = id;
            }
    
            public ImageBean getImage() {
                return image;
            }
    
            public void setImage( ImageBean image ) {
                this.image = image;
            }
    
            public TopCommentBean getTop_comment() {
                return top_comment;
            }
    
            public void setTop_comment( TopCommentBean top_comment ) {
                this.top_comment = top_comment;
            }
    
            public List< TopCommentsBean > getTop_comments() {
                return top_comments;
            }
    
            public void setTop_comments( List< TopCommentsBean > top_comments ) {
                this.top_comments = top_comments;
            }
    
            public List< TagsBean > getTags() {
                return tags;
            }
    
            public void setTags( List< TagsBean > tags ) {
                this.tags = tags;
            }
    
            public static class GifBean {
                /**
                 * images : ["http://wimg.spriteapp.cn/ugc/2017/09/01/59a973fb6dba8.gif","http://dimg.spriteapp.cn/ugc/2017/09/01/59a973fb6dba8.gif"]
                 * width : 281
                 * gif_thumbnail : ["http://wimg.spriteapp.cn/ugc/2017/09/01/59a973fb6dba8_a_1.jpg","http://dimg.spriteapp.cn/ugc/2017/09/01/59a973fb6dba8_a_1.jpg"]
                 * download_url : ["http://wimg.spriteapp.cn/ugc/2017/09/01/59a973fb6dba8_d.jpg","http://dimg.spriteapp.cn/ugc/2017/09/01/59a973fb6dba8_d.jpg","http://wimg.spriteapp
                 * .cn/ugc/2017/09/01/59a973fb6dba8_a_1.jpg","http://dimg.spriteapp.cn/ugc/2017/09/01/59a973fb6dba8_a_1.jpg"]
                 * height : 190
                 */
    
                private int width;
                private int height;
                private List< String > images;
                private List< String > gif_thumbnail;
                private List< String > download_url;
    
                public int getWidth() {
                    return width;
                }
    
                public void setWidth( int width ) {
                    this.width = width;
                }
    
                public int getHeight() {
                    return height;
                }
    
                public void setHeight( int height ) {
                    this.height = height;
                }
    
                public List< String > getImages() {
                    return images;
                }
    
                public void setImages( List< String > images ) {
                    this.images = images;
                }
    
                public List< String > getGif_thumbnail() {
                    return gif_thumbnail;
                }
    
                public void setGif_thumbnail( List< String > gif_thumbnail ) {
                    this.gif_thumbnail = gif_thumbnail;
                }
    
                public List< String > getDownload_url() {
                    return download_url;
                }
    
                public void setDownload_url( List< String > download_url ) {
                    this.download_url = download_url;
                }
            }
    
            public static class UBean {
                /**
                 * header : ["http://wimg.spriteapp.cn/profile/large/2016/12/26/586059118dd30_mini.jpg","http://dimg.spriteapp.cn/profile/large/2016/12/26/586059118dd30_mini.jpg"]
                 * uid : 18619888
                 * is_vip : false
                 * is_v : false
                 * room_url :
                 * room_name : Vxin搞笑小村
                 * room_role : 副帮主
                 * room_icon : http://wimg.spriteapp.cn/ugc/2016/1101/gang_level_15.png
                 * name : 搞笑小村 [Vxin搞笑小村]
                 */
    
                private String uid;
                private boolean is_vip;
                private boolean is_v;
                private String room_url;
                private String room_name;
                private String room_role;
                private String room_icon;
                private String name;
                private List< String > header;
    
                public String getUid() {
                    return uid;
                }
    
                public void setUid( String uid ) {
                    this.uid = uid;
                }
    
                public boolean isIs_vip() {
                    return is_vip;
                }
    
                public void setIs_vip( boolean is_vip ) {
                    this.is_vip = is_vip;
                }
    
                public boolean isIs_v() {
                    return is_v;
                }
    
                public void setIs_v( boolean is_v ) {
                    this.is_v = is_v;
                }
    
                public String getRoom_url() {
                    return room_url;
                }
    
                public void setRoom_url( String room_url ) {
                    this.room_url = room_url;
                }
    
                public String getRoom_name() {
                    return room_name;
                }
    
                public void setRoom_name( String room_name ) {
                    this.room_name = room_name;
                }
    
                public String getRoom_role() {
                    return room_role;
                }
    
                public void setRoom_role( String room_role ) {
                    this.room_role = room_role;
                }
    
                public String getRoom_icon() {
                    return room_icon;
                }
    
                public void setRoom_icon( String room_icon ) {
                    this.room_icon = room_icon;
                }
    
                public String getName() {
                    return name;
                }
    
                public void setName( String name ) {
                    this.name = name;
                }
    
                public List< String > getHeader() {
                    return header;
                }
    
                public void setHeader( List< String > header ) {
                    this.header = header;
                }
            }
    
            public static class ImageBean {
                /**
                 * medium : []
                 * big : ["http://wimg.spriteapp.cn/ugc/2017/10/22/59ec22b98b81e_1.jpg","http://dimg.spriteapp.cn/ugc/2017/10/22/59ec22b98b81e_1.jpg"]
                 * download_url : ["http://wimg.spriteapp.cn/ugc/2017/10/22/59ec22b98b81e_d.jpg","http://dimg.spriteapp.cn/ugc/2017/10/22/59ec22b98b81e_d.jpg","http://wimg.spriteapp
                 * .cn/ugc/2017/10/22/59ec22b98b81e.jpg","http://dimg.spriteapp.cn/ugc/2017/10/22/59ec22b98b81e.jpg"]
                 * height : 595
                 * width : 595
                 * small : []
                 * thumbnail_small : ["http://wimg.spriteapp.cn/crop/150x150/ugc/2017/10/22/59ec22b98b81e.jpg","http://dimg.spriteapp.cn/crop/150x150/ugc/2017/10/22/59ec22b98b81e.jpg"]
                 */
    
                private int height;
                private int width;
                private List< ? > medium;
                private List< String > big;
                private List< String > download_url;
                private List< ? > small;
                private List< String > thumbnail_small;
    
                public int getHeight() {
                    return height;
                }
    
                public void setHeight( int height ) {
                    this.height = height;
                }
    
                public int getWidth() {
                    return width;
                }
    
                public void setWidth( int width ) {
                    this.width = width;
                }
    
                public List< ? > getMedium() {
                    return medium;
                }
    
                public void setMedium( List< ? > medium ) {
                    this.medium = medium;
                }
    
                public List< String > getBig() {
                    return big;
                }
    
                public void setBig( List< String > big ) {
                    this.big = big;
                }
    
                public List< String > getDownload_url() {
                    return download_url;
                }
    
                public void setDownload_url( List< String > download_url ) {
                    this.download_url = download_url;
                }
    
                public List< ? > getSmall() {
                    return small;
                }
    
                public void setSmall( List< ? > small ) {
                    this.small = small;
                }
    
                public List< String > getThumbnail_small() {
                    return thumbnail_small;
                }
    
                public void setThumbnail_small( List< String > thumbnail_small ) {
                    this.thumbnail_small = thumbnail_small;
                }
            }
    
            public static class TopCommentBean {
                /**
                 * voicetime : 0
                 * status : 0
                 * hate_count : 4
                 * cmt_type : text
                 * precid : 0
                 * content : 在他射箭的同时手腕极速的抖动,给了箭一个水平的加速度,这就是箭斗术
                 * like_count : 534
                 * u : {"header":["http://qzapp.qlogo.cn/qzapp/100336987/8CFDC87B431965EA581C2646546A4480/100","http://qzapp.qlogo.cn/qzapp/100336987/8CFDC87B431965EA581C2646546A4480/100"],
                 * "uid":"15394731","is_vip":false,"room_url":"","sex":"m","room_name":"","room_role":"","room_icon":"","name":"娘子陪我可好"}
                 * preuid : 0
                 * passtime : 2017-10-22 19:35:35
                 * voiceuri :
                 * id : 92082875
                 */
    
                private int voicetime;
                private int status;
                private int hate_count;
                private String cmt_type;
                private int precid;
                private String content;
                private int like_count;
                private UBeanX u;
                private int preuid;
                private String passtime;
                private String voiceuri;
                private int id;
    
                public int getVoicetime() {
                    return voicetime;
                }
    
                public void setVoicetime( int voicetime ) {
                    this.voicetime = voicetime;
                }
    
                public int getStatus() {
                    return status;
                }
    
                public void setStatus( int status ) {
                    this.status = status;
                }
    
                public int getHate_count() {
                    return hate_count;
                }
    
                public void setHate_count( int hate_count ) {
                    this.hate_count = hate_count;
                }
    
                public String getCmt_type() {
                    return cmt_type;
                }
    
                public void setCmt_type( String cmt_type ) {
                    this.cmt_type = cmt_type;
                }
    
                public int getPrecid() {
                    return precid;
                }
    
                public void setPrecid( int precid ) {
                    this.precid = precid;
                }
    
                public String getContent() {
                    return content;
                }
    
                public void setContent( String content ) {
                    this.content = content;
                }
    
                public int getLike_count() {
                    return like_count;
                }
    
                public void setLike_count( int like_count ) {
                    this.like_count = like_count;
                }
    
                public UBeanX getU() {
                    return u;
                }
    
                public void setU( UBeanX u ) {
                    this.u = u;
                }
    
                public int getPreuid() {
                    return preuid;
                }
    
                public void setPreuid( int preuid ) {
                    this.preuid = preuid;
                }
    
                public String getPasstime() {
                    return passtime;
                }
    
                public void setPasstime( String passtime ) {
                    this.passtime = passtime;
                }
    
                public String getVoiceuri() {
                    return voiceuri;
                }
    
                public void setVoiceuri( String voiceuri ) {
                    this.voiceuri = voiceuri;
                }
    
                public int getId() {
                    return id;
                }
    
                public void setId( int id ) {
                    this.id = id;
                }
    
                public static class UBeanX {
                    /**
                     * header : ["http://qzapp.qlogo.cn/qzapp/100336987/8CFDC87B431965EA581C2646546A4480/100","http://qzapp.qlogo.cn/qzapp/100336987/8CFDC87B431965EA581C2646546A4480/100"]
                     * uid : 15394731
                     * is_vip : false
                     * room_url :
                     * sex : m
                     * room_name :
                     * room_role :
                     * room_icon :
                     * name : 娘子陪我可好
                     */
    
                    private String uid;
                    private boolean is_vip;
                    private String room_url;
                    private String sex;
                    private String room_name;
                    private String room_role;
                    private String room_icon;
                    private String name;
                    private List< String > header;
    
                    public String getUid() {
                        return uid;
                    }
    
                    public void setUid( String uid ) {
                        this.uid = uid;
                    }
    
                    public boolean isIs_vip() {
                        return is_vip;
                    }
    
                    public void setIs_vip( boolean is_vip ) {
                        this.is_vip = is_vip;
                    }
    
                    public String getRoom_url() {
                        return room_url;
                    }
    
                    public void setRoom_url( String room_url ) {
                        this.room_url = room_url;
                    }
    
                    public String getSex() {
                        return sex;
                    }
    
                    public void setSex( String sex ) {
                        this.sex = sex;
                    }
    
                    public String getRoom_name() {
                        return room_name;
                    }
    
                    public void setRoom_name( String room_name ) {
                        this.room_name = room_name;
                    }
    
                    public String getRoom_role() {
                        return room_role;
                    }
    
                    public void setRoom_role( String room_role ) {
                        this.room_role = room_role;
                    }
    
                    public String getRoom_icon() {
                        return room_icon;
                    }
    
                    public void setRoom_icon( String room_icon ) {
                        this.room_icon = room_icon;
                    }
    
                    public String getName() {
                        return name;
                    }
    
                    public void setName( String name ) {
                        this.name = name;
                    }
    
                    public List< String > getHeader() {
                        return header;
                    }
    
                    public void setHeader( List< String > header ) {
                        this.header = header;
                    }
                }
            }
    
            public static class TopCommentsBean {
                /**
                 * voicetime : 0
                 * status : 0
                 * hate_count : 1
                 * cmt_type : text
                 * precid : 0
                 * content : 老衲法号屌无情
                 * like_count : 20
                 * u : {"header":["http://qzapp.qlogo.cn/qzapp/100336987/04C9A082B76D5A41B07985918EEC8F67/100","http://qzapp.qlogo.cn/qzapp/100336987/04C9A082B76D5A41B07985918EEC8F67/100"],
                 * "uid":"19951280","is_vip":false,"room_url":"","sex":"m","room_name":"","room_role":"","room_icon":"","name":"你好呀6"}
                 * preuid : 0
                 * passtime : 2017-10-23 16:01:29
                 * voiceuri :
                 * id : 92126886
                 */
    
                private int voicetime;
                private int status;
                private int hate_count;
                private String cmt_type;
                private int precid;
                private String content;
                private int like_count;
                private UBeanXX u;
                private int preuid;
                private String passtime;
                private String voiceuri;
                private int id;
    
                public int getVoicetime() {
                    return voicetime;
                }
    
                public void setVoicetime( int voicetime ) {
                    this.voicetime = voicetime;
                }
    
                public int getStatus() {
                    return status;
                }
    
                public void setStatus( int status ) {
                    this.status = status;
                }
    
                public int getHate_count() {
                    return hate_count;
                }
    
                public void setHate_count( int hate_count ) {
                    this.hate_count = hate_count;
                }
    
                public String getCmt_type() {
                    return cmt_type;
                }
    
                public void setCmt_type( String cmt_type ) {
                    this.cmt_type = cmt_type;
                }
    
                public int getPrecid() {
                    return precid;
                }
    
                public void setPrecid( int precid ) {
                    this.precid = precid;
                }
    
                public String getContent() {
                    return content;
                }
    
                public void setContent( String content ) {
                    this.content = content;
                }
    
                public int getLike_count() {
                    return like_count;
                }
    
                public void setLike_count( int like_count ) {
                    this.like_count = like_count;
                }
    
                public UBeanXX getU() {
                    return u;
                }
    
                public void setU( UBeanXX u ) {
                    this.u = u;
                }
    
                public int getPreuid() {
                    return preuid;
                }
    
                public void setPreuid( int preuid ) {
                    this.preuid = preuid;
                }
    
                public String getPasstime() {
                    return passtime;
                }
    
                public void setPasstime( String passtime ) {
                    this.passtime = passtime;
                }
    
                public String getVoiceuri() {
                    return voiceuri;
                }
    
                public void setVoiceuri( String voiceuri ) {
                    this.voiceuri = voiceuri;
                }
    
                public int getId() {
                    return id;
                }
    
                public void setId( int id ) {
                    this.id = id;
                }
    
                public static class UBeanXX {
                    /**
                     * header : ["http://qzapp.qlogo.cn/qzapp/100336987/04C9A082B76D5A41B07985918EEC8F67/100","http://qzapp.qlogo.cn/qzapp/100336987/04C9A082B76D5A41B07985918EEC8F67/100"]
                     * uid : 19951280
                     * is_vip : false
                     * room_url :
                     * sex : m
                     * room_name :
                     * room_role :
                     * room_icon :
                     * name : 你好呀6
                     */
    
                    private String uid;
                    private boolean is_vip;
                    private String room_url;
                    private String sex;
                    private String room_name;
                    private String room_role;
                    private String room_icon;
                    private String name;
                    private List< String > header;
    
                    public String getUid() {
                        return uid;
                    }
    
                    public void setUid( String uid ) {
                        this.uid = uid;
                    }
    
                    public boolean isIs_vip() {
                        return is_vip;
                    }
    
                    public void setIs_vip( boolean is_vip ) {
                        this.is_vip = is_vip;
                    }
    
                    public String getRoom_url() {
                        return room_url;
                    }
    
                    public void setRoom_url( String room_url ) {
                        this.room_url = room_url;
                    }
    
                    public String getSex() {
                        return sex;
                    }
    
                    public void setSex( String sex ) {
                        this.sex = sex;
                    }
    
                    public String getRoom_name() {
                        return room_name;
                    }
    
                    public void setRoom_name( String room_name ) {
                        this.room_name = room_name;
                    }
    
                    public String getRoom_role() {
                        return room_role;
                    }
    
                    public void setRoom_role( String room_role ) {
                        this.room_role = room_role;
                    }
    
                    public String getRoom_icon() {
                        return room_icon;
                    }
    
                    public void setRoom_icon( String room_icon ) {
                        this.room_icon = room_icon;
                    }
    
                    public String getName() {
                        return name;
                    }
    
                    public void setName( String name ) {
                        this.name = name;
                    }
    
                    public List< String > getHeader() {
                        return header;
                    }
    
                    public void setHeader( List< String > header ) {
                        this.header = header;
                    }
                }
            }
    
            public static class TagsBean {
                /**
                 * post_number : 2993
                 * image_list : http://img.spriteapp.cn/ugc/2017/07/9b8ecb42703211e7bf00842b2b4c75ab.png
                 * forum_sort : 0
                 * forum_status : 2
                 * id : 3241
                 * info :
                 * name : 萌妹子
                 * colum_set : 1
                 * tail :
                 * sub_number : 1004
                 * display_level : 0
                 */
    
                private int post_number;
                private String image_list;
                private int forum_sort;
                private int forum_status;
                private int id;
                private String info;
                private String name;
                private int colum_set;
                private String tail;
                private int sub_number;
                private int display_level;
    
                public int getPost_number() {
                    return post_number;
                }
    
                public void setPost_number( int post_number ) {
                    this.post_number = post_number;
                }
    
                public String getImage_list() {
                    return image_list;
                }
    
                public void setImage_list( String image_list ) {
                    this.image_list = image_list;
                }
    
                public int getForum_sort() {
                    return forum_sort;
                }
    
                public void setForum_sort( int forum_sort ) {
                    this.forum_sort = forum_sort;
                }
    
                public int getForum_status() {
                    return forum_status;
                }
    
                public void setForum_status( int forum_status ) {
                    this.forum_status = forum_status;
                }
    
                public int getId() {
                    return id;
                }
    
                public void setId( int id ) {
                    this.id = id;
                }
    
                public String getInfo() {
                    return info;
                }
    
                public void setInfo( String info ) {
                    this.info = info;
                }
    
                public String getName() {
                    return name;
                }
    
                public void setName( String name ) {
                    this.name = name;
                }
    
                public int getColum_set() {
                    return colum_set;
                }
    
                public void setColum_set( int colum_set ) {
                    this.colum_set = colum_set;
                }
    
                public String getTail() {
                    return tail;
                }
    
                public void setTail( String tail ) {
                    this.tail = tail;
                }
    
                public int getSub_number() {
                    return sub_number;
                }
    
                public void setSub_number( int sub_number ) {
                    this.sub_number = sub_number;
                }
    
                public int getDisplay_level() {
                    return display_level;
                }
    
                public void setDisplay_level( int display_level ) {
                    this.display_level = display_level;
                }
    
                @Override
                public String toString() {
                    return "TagsBean{" + "post_number=" + post_number + ", image_list='" + image_list + '\'' + ", forum_sort=" + forum_sort + ", forum_status=" + forum_status + ", id=" + id + ", info='"
                            + info + '\'' + ", name='" + name + '\'' + ", colum_set=" + colum_set + ", tail='" + tail + '\'' + ", sub_number=" + sub_number + ", display_level=" + display_level + '}';
                }
            }
    
            @Override
            public String toString() {
                return "ListBean{" + "status=" + status + ", comment='" + comment + '\'' + ", bookmark='" + bookmark + '\'' + ", text='" + text + '\'' + ", gif=" + gif + ", up='" + up + '\'' + ", " +
                        "share_url='" + share_url + '\'' + ", down=" + down + ", forward=" + forward + ", u=" + u + ", passtime='" + passtime + '\'' + ", type='" + type + '\'' + ", id='" + id + '\'' +
                        ", image=" + image + ", top_comment=" + top_comment + ", top_comments=" + top_comments + ", tags=" + tags + '}';
            }
        }
    
        @Override
        public String toString() {
            return "BudejieBean{" + "info=" + info + ", list=" + list + '}';
        }
    }
    
    

    定义API:

    public interface API {
    @GET("http://c.api.budejie.com/topic/list/jingxuan/10/budejie-android-6.8.4/0-20.json?market=tencentyingyongbao&ver=6.8.4&visiting=21479789&os=7.0&appname=baisibudejie&client=android&udid=863696030798111&mac=a4%3Aca%3Aa0%3A17%3A18%3Ad7")
        Call<BudejieBean> getBudejie();
    }
    

    在布局文件中定义按钮并使用Butterknife插件在activity中创建监听

    @OnClick({R.id.btn_request_get, R.id.btn_request_post})
        public void onViewClicked(View view) {
        API api = (API) RetrofitWrapper.getInstance().create(API.class);
                    Call<BudejieBean> call = api.getBudejie();
                    call.enqueue(new Callback<BudejieBean>() {
                        @Override
                        public void onResponse(Call<BudejieBean> call, Response<BudejieBean> response) {
                            tvGetandpost.setText(response.toString()+"\n\n"+"message=="+response.message().toString()+"\n\n"+"errorBody=="+response.errorBody()+"\n\n"+"headers=="+response.headers().toString()+"\n\n"+"body=="+response.body().toString());
                        }
                        @Override
                        public void onFailure(Call<BudejieBean> call, Throwable t) {
                            tvGetandpost.setText(call.toString()+t.toString());
                        }
                    });
    }
    

    不要忘记添加网络权限!

    4.Post请求:url:https://103.234.21.49:8024/crm-app/

    生成实体类:

    
    public class CrmLoginBean {
        /**
         * code : 0
         * dataObject : {"result":{"departmentName":"项目二组","id":1492,"jobtitleType":88663,"mobile":"","username":"pengjie_yc"}}
         * errorDescription : 测试内容8041
         */
    
        private String code;
        private DataObjectBean dataObject;
        private String errorDescription;
    
        public String getCode() {
            return code;
        }
    
        public void setCode( String code ) {
            this.code = code;
        }
    
        public DataObjectBean getDataObject() {
            return dataObject;
        }
    
        public void setDataObject( DataObjectBean dataObject ) {
            this.dataObject = dataObject;
        }
    
        public String getErrorDescription() {
            return errorDescription;
        }
    
        public void setErrorDescription( String errorDescription ) {
            this.errorDescription = errorDescription;
        }
    
        public static class DataObjectBean {
            /**
             * result : {"departmentName":"项目二组","id":1492,"jobtitleType":88663,"mobile":"","username":"pengjie_yc"}
             */
    
            private ResultBean result;
    
            public ResultBean getResult() {
                return result;
            }
    
            public void setResult( ResultBean result ) {
                this.result = result;
            }
    
            public static class ResultBean {
                /**
                 * departmentName : 项目二组
                 * id : 1492
                 * jobtitleType : 88663
                 * mobile :
                 * username : pengjie_yc
                 */
    
                private String departmentName;
                private int id;
                private int jobtitleType;
                private String mobile;
                private String username;
    
                public String getDepartmentName() {
                    return departmentName;
                }
    
                public void setDepartmentName( String departmentName ) {
                    this.departmentName = departmentName;
                }
    
                public int getId() {
                    return id;
                }
    
                public void setId( int id ) {
                    this.id = id;
                }
    
                public int getJobtitleType() {
                    return jobtitleType;
                }
    
                public void setJobtitleType( int jobtitleType ) {
                    this.jobtitleType = jobtitleType;
                }
    
                public String getMobile() {
                    return mobile;
                }
    
                public void setMobile( String mobile ) {
                    this.mobile = mobile;
                }
    
                public String getUsername() {
                    return username;
                }
    
                public void setUsername( String username ) {
    
                    this.username = username;
                }
    
                @Override
                public String toString() {
                    return "ResultBean{" + "departmentName='" + departmentName + '\'' + ", id=" + id + ", jobtitleType=" + jobtitleType + ", mobile='" + mobile + '\'' + ", username='" + username + '\'' + '}';
                }
            }
    
            @Override
            public String toString() {
                return "DataObjectBean{" + "result=" + result + '}';
            }
        }
    
        @Override
        public String toString() {
            return "CrmLoginBean{" + "code='" + code + '\'' + ", dataObject=" + dataObject + ", errorDescription='" + errorDescription + '\'' + '}';
        }
    }
    
    

    创建API:

    public interface API {
        @FormUrlEncoded
        @POST("https://103.234.21.49:8024/crm-app/loginModule/login.do")
        Call<CrmLoginBean> crmLogin(@FieldMap Map<String,String> maps);
    }
    

    在activity中使用:

    API api1 = (API) RetrofitWrapper.getInstance().create(API.class);
                    Map map = new HashMap();
                    map.put("username","liurui_qc");
                    map.put("password","123456");
                    Call<CrmLoginBean> call1 = api1.crmLogin(map);
                    call1.enqueue(new Callback<CrmLoginBean>() {
                        @Override
                        public void onResponse(Call<CrmLoginBean> call, Response<CrmLoginBean> response) {
                            tvGetandpost.setText("code=="+response.code()+"\n\n"+response.isSuccessful()+"\n\n"+"message=="+response.message()+"\n\n"+"headers=="+response.headers().toString()+response.body().toString());
                        }
                        @Override
                        public void onFailure(Call<CrmLoginBean> call, Throwable t) {
                            tvGetandpost.setText(call.toString()+t.toString());
                        }
                    });
    

    相关文章

      网友评论

          本文标题:Retrofit简单使用

          本文链接:https://www.haomeiwen.com/subject/axtgpxtx.html