retrofit+gson+okhttp使用的简单记录

1.引用:

compile 'com.squareup.retrofit2:retrofit:2.1.0'      //retrofit2compile 'com.squareup.retrofit2:converter-gson:2.1.0'   //gson适配器compile 'com.squareup.retrofit2:adapter-rxjava:2.1.0'    //  rxjava适配器compile 'com.squareup.okhttp3:okhttp:3.3.1'        //okhttp

2.定义接口类:

 public interface WeatherApi {    @GET("onebox/weather/query?cityname=深圳")    Call<WeatherDataBean>  getWeather(@Query("key") String key);    @GET("index/")    Call<WeatherDataBean>  getWeather(@QueryMap Map<String ,String> params );}

3.编写解析类,gsonformat这个插件挺好用的:Mac下快捷键 : command+N。

public class WeatherDataBean {    /**     * resultcode : 200     * reason : 查询成功!     * result : {"sk":{"temp":"21","wind_direction":"西风","wind_strength":"2级","humidity":"4%","time":"14:25"},"today":{"city":"天津","date_y":"2014年03月21日","week":"星期五","temperature":"8℃~20℃","weather":"晴转霾","weather_id":{"fa":"00","fb":"53"},"wind":"西南风微风","dressing_index":"较冷","dressing_advice":"建议着大衣、呢外套加毛衣、卫衣等服装。","uv_index":"中等","comfort_index":"","wash_index":"较适宜","travel_index":"适宜","exercise_index":"较适宜","drying_index":""},"future":[{"temperature":"28℃~36℃","weather":"晴转多云","weather_id":{"fa":"00","fb":"01"},"wind":"南风3-4级","week":"星期一","date":"20140804"},{"temperature":"28℃~36℃","weather":"晴转多云","weather_id":{"fa":"00","fb":"01"},"wind":"东南风3-4级","week":"星期二","date":"20140805"},{"temperature":"27℃~35℃","weather":"晴转多云","weather_id":{"fa":"00","fb":"01"},"wind":"东南风3-4级","week":"星期三","date":"20140806"},{"temperature":"27℃~34℃","weather":"多云","weather_id":{"fa":"01","fb":"01"},"wind":"东南风3-4级","week":"星期四","date":"20140807"},{"temperature":"27℃~33℃","weather":"多云","weather_id":{"fa":"01","fb":"01"},"wind":"东北风4-5级","week":"星期五","date":"20140808"},{"temperature":"26℃~33℃","weather":"多云","weather_id":{"fa":"01","fb":"01"},"wind":"北风4-5级","week":"星期六","date":"20140809"},{"temperature":"26℃~33℃","weather":"多云","weather_id":{"fa":"01","fb":"01"},"wind":"北风4-5级","week":"星期日","date":"20140810"}]}     * error_code : 0     */    private String resultcode;    private String reason;    /**     * sk : {"temp":"21","wind_direction":"西风","wind_strength":"2级","humidity":"4%","time":"14:25"}     * today : {"city":"天津","date_y":"2014年03月21日","week":"星期五","temperature":"8℃~20℃","weather":"晴转霾","weather_id":{"fa":"00","fb":"53"},"wind":"西南风微风","dressing_index":"较冷","dressing_advice":"建议着大衣、呢外套加毛衣、卫衣等服装。","uv_index":"中等","comfort_index":"","wash_index":"较适宜","travel_index":"适宜","exercise_index":"较适宜","drying_index":""}     * future : [{"temperature":"28℃~36℃","weather":"晴转多云","weather_id":{"fa":"00","fb":"01"},"wind":"南风3-4级","week":"星期一","date":"20140804"},{"temperature":"28℃~36℃","weather":"晴转多云","weather_id":{"fa":"00","fb":"01"},"wind":"东南风3-4级","week":"星期二","date":"20140805"},{"temperature":"27℃~35℃","weather":"晴转多云","weather_id":{"fa":"00","fb":"01"},"wind":"东南风3-4级","week":"星期三","date":"20140806"},{"temperature":"27℃~34℃","weather":"多云","weather_id":{"fa":"01","fb":"01"},"wind":"东南风3-4级","week":"星期四","date":"20140807"},{"temperature":"27℃~33℃","weather":"多云","weather_id":{"fa":"01","fb":"01"},"wind":"东北风4-5级","week":"星期五","date":"20140808"},{"temperature":"26℃~33℃","weather":"多云","weather_id":{"fa":"01","fb":"01"},"wind":"北风4-5级","week":"星期六","date":"20140809"},{"temperature":"26℃~33℃","weather":"多云","weather_id":{"fa":"01","fb":"01"},"wind":"北风4-5级","week":"星期日","date":"20140810"}]     */    private ResultBean result;    private int error_code;    public String getResultcode() {        return resultcode;    }    public void setResultcode(String resultcode) {        this.resultcode = resultcode;    }    public String getReason() {        return reason;    }    public void setReason(String reason) {        this.reason = reason;    }    public ResultBean getResult() {        return result;    }    public void setResult(ResultBean result) {        this.result = result;    }    public int getError_code() {        return error_code;    }    public void setError_code(int error_code) {        this.error_code = error_code;    }    public static class ResultBean {        /**         * temp : 21         * wind_direction : 西风         * wind_strength : 2级         * humidity : 4%         * time : 14:25         */        private SkBean sk;        /**         * city : 天津         * date_y : 2014年03月21日         * week : 星期五         * temperature : 8℃~20℃         * weather : 晴转霾         * weather_id : {"fa":"00","fb":"53"}         * wind : 西南风微风         * dressing_index : 较冷         * dressing_advice : 建议着大衣、呢外套加毛衣、卫衣等服装。         * uv_index : 中等         * comfort_index :         * wash_index : 较适宜         * travel_index : 适宜         * exercise_index : 较适宜         * drying_index :         */        private TodayBean today;        /**         * temperature : 28℃~36℃         * weather : 晴转多云         * weather_id : {"fa":"00","fb":"01"}         * wind : 南风3-4级         * week : 星期一         * date : 20140804         */        private List<FutureBean> future;        public SkBean getSk() {            return sk;        }        public void setSk(SkBean sk) {            this.sk = sk;        }        public TodayBean getToday() {            return today;        }        public void setToday(TodayBean today) {            this.today = today;        }        public List<FutureBean> getFuture() {            return future;        }        public void setFuture(List<FutureBean> future) {            this.future = future;        }        public static class SkBean {            private String temp;            private String wind_direction;            private String wind_strength;            private String humidity;            private String time;            public String getTemp() {                return temp;            }            public void setTemp(String temp) {                this.temp = temp;            }            public String getWind_direction() {                return wind_direction;            }            public void setWind_direction(String wind_direction) {                this.wind_direction = wind_direction;            }            public String getWind_strength() {                return wind_strength;            }            public void setWind_strength(String wind_strength) {                this.wind_strength = wind_strength;            }            public String getHumidity() {                return humidity;            }            public void setHumidity(String humidity) {                this.humidity = humidity;            }            public String getTime() {                return time;            }            public void setTime(String time) {                this.time = time;            }        }        public static class TodayBean {            private String city;            private String date_y;            private String week;            private String temperature;            private String weather;            /**             * fa : 00             * fb : 53             */            private WeatherIdBean weather_id;            private String wind;            private String dressing_index;            private String dressing_advice;            private String uv_index;            private String comfort_index;            private String wash_index;            private String travel_index;            private String exercise_index;            private String drying_index;            public String getCity() {                return city;            }            public void setCity(String city) {                this.city = city;            }            public String getDate_y() {                return date_y;            }            public void setDate_y(String date_y) {                this.date_y = date_y;            }            public String getWeek() {                return week;            }            public void setWeek(String week) {                this.week = week;            }            public String getTemperature() {                return temperature;            }            public void setTemperature(String temperature) {                this.temperature = temperature;            }            public String getWeather() {                return weather;            }            public void setWeather(String weather) {                this.weather = weather;            }            public WeatherIdBean getWeather_id() {                return weather_id;            }            public void setWeather_id(WeatherIdBean weather_id) {                this.weather_id = weather_id;            }            public String getWind() {                return wind;            }            public void setWind(String wind) {                this.wind = wind;            }            public String getDressing_index() {                return dressing_index;            }            public void setDressing_index(String dressing_index) {                this.dressing_index = dressing_index;            }            public String getDressing_advice() {                return dressing_advice;            }            public void setDressing_advice(String dressing_advice) {                this.dressing_advice = dressing_advice;            }            public String getUv_index() {                return uv_index;            }            public void setUv_index(String uv_index) {                this.uv_index = uv_index;            }            public String getComfort_index() {                return comfort_index;            }            public void setComfort_index(String comfort_index) {                this.comfort_index = comfort_index;            }            public String getWash_index() {                return wash_index;            }            public void setWash_index(String wash_index) {                this.wash_index = wash_index;            }            public String getTravel_index() {                return travel_index;            }            public void setTravel_index(String travel_index) {                this.travel_index = travel_index;            }            public String getExercise_index() {                return exercise_index;            }            public void setExercise_index(String exercise_index) {                this.exercise_index = exercise_index;            }            public String getDrying_index() {                return drying_index;            }            public void setDrying_index(String drying_index) {                this.drying_index = drying_index;            }            public static class WeatherIdBean {                private String fa;                private String fb;                public String getFa() {                    return fa;                }                public void setFa(String fa) {                    this.fa = fa;                }                public String getFb() {                    return fb;                }                public void setFb(String fb) {                    this.fb = fb;                }            }        }        public static class FutureBean {            private String temperature;            private String weather;            /**             * fa : 00             * fb : 01             */            private WeatherIdBean weather_id;            private String wind;            private String week;            private String date;            public String getTemperature() {                return temperature;            }            public void setTemperature(String temperature) {                this.temperature = temperature;            }            public String getWeather() {                return weather;            }            public void setWeather(String weather) {                this.weather = weather;            }            public WeatherIdBean getWeather_id() {                return weather_id;            }            public void setWeather_id(WeatherIdBean weather_id) {                this.weather_id = weather_id;            }            public String getWind() {                return wind;            }            public void setWind(String wind) {                this.wind = wind;            }            public String getWeek() {                return week;            }            public void setWeek(String week) {                this.week = week;            }            public String getDate() {                return date;            }            public void setDate(String date) {                this.date = date;            }            public static class WeatherIdBean {                private String fa;                private String fb;                public String getFa() {                    return fa;                }                public void setFa(String fa) {                    this.fa = fa;                }                public String getFb() {                    return fb;                }                public void setFb(String fb) {                    this.fb = fb;                }            }        }    }}

4.简单的基本路径和接口定义的拼接 get固定参数使用.
baseurl

Retrofit  retrofit=new Retrofit.Builder()        .baseUrl("http://gank.io/")        .addConverterFactory(GsonConverterFactory.create())        .build();GnakApi gnakApi=retrofit.create(GnakApi.class);Call<GnakBean>  call=gnakApi.getAndroidInfo();call.enqueue(new Callback<GnakBean>() {    @Override    public void onResponse(Call<GnakBean> call, Response<GnakBean> response) {        GnakBean.ResultsBean bean=response.body().getResults().get(0);        String result="<<"+bean.getImages()+"\n"+bean.getCreatedAt();        Log.i("result",result);        Toast.makeText(MainActivity.this,result,Toast.LENGTH_LONG).show();    }    @Override    public void onFailure(Call<GnakBean> call, Throwable t) {    }});

5.get动态参数

Retrofit  retrofit1=new Retrofit.Builder()        .baseUrl("http://op.juhe.cn/")        .addConverterFactory(GsonConverterFactory.create())        .build();WeatherApi  weatherApi=retrofit1.create(WeatherApi.class);   //124ac971e272bed4354d9c6b9b4e9de2Call<WeatherDataBean>  call1=weatherApi.getWeather("124ac971e272bed4354d9c6b9b4e9de2");call1.enqueue(new Callback<WeatherDataBean>() {    @Override    public void onResponse(Call<WeatherDataBean> call, Response<WeatherDataBean> response) {                WeatherDataBean.ResultBean  bean=response.body().getResult();               String  resultweather=bean.getToday().getTemperature()+"<<";        Log.i("result",resultweather);        Toast.makeText(MainActivity.this,resultweather,Toast.LENGTH_LONG).show();    }    @Override    public void onFailure(Call<WeatherDataBean> call, Throwable t) {    }});

6.get参数请求

Retrofit  retrofit2=new Retrofit.Builder()        .baseUrl("http://gank.io/")        .addConverterFactory(GsonConverterFactory.create())        .build();GnakApi gnakApi2=retrofit2.create(GnakApi.class);Call<GnakBean>  call2=gnakApi2.getAndroidInfo(10,1);call2.enqueue(new Callback<GnakBean>() {    @Override    public void onResponse(Call<GnakBean> call, Response<GnakBean> response) {        GnakBean.ResultsBean bean=response.body().getResults().get(0);        String result="<<"+bean.getImages()+"\n"+bean.getCreatedAt();        Log.i("result",result);        Toast.makeText(MainActivity.this,result,Toast.LENGTH_LONG).show();    }    @Override    public void onFailure(Call<GnakBean> call, Throwable t) {    }});

7.Get参数拼接

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

推荐阅读更多精彩内容