Retrofit简单使用

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

推荐阅读更多精彩内容