美文网首页
retrofit+gson+okhttp使用的简单记录

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

作者: dong_hui | 来源:发表于2016-12-07 16:15 被阅读58次

    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) {    }});
    

    相关文章

      网友评论

          本文标题:retrofit+gson+okhttp使用的简单记录

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