retrofit 如何传入一个对象

来源:互联网 发布:python utils 模块 编辑:程序博客网 时间:2024/05/12 19:40

如果数据格式是这样的


{
    "results": [
        {
            "location": {
                "id": "WX4FBXXFKE4F",
                "name": "北京",
                "country": "CN",
                "path": "北京,北京,中国",
                "timezone": "Asia/Shanghai",
                "timezone_offset": "+08:00"
            },
            "now": {
                "text": "多云",
                "code": "4",
                "temperature": "13"
            },
            "last_update": "2016-10-12T00:45:00+08:00"
        }
    ]
}

就应该解析成这样 类的名字可以随便写 

类的属性名字就需要与解析的数据一一对应

public class ResultEnity {    @SerializedName("results")    public List<Kongbai> location;    public class Kongbai {        /**         * id : WX4FBXXFKE4F         * name : 北京         * country : CN         * path : 北京,北京,中国         * timezone : Asia/Shanghai         * timezone_offset : +08:00         */        public LocationBean location;        /**         * text : 多云         * code : 4         * temperature : 13         */        public NowBean now;        /**         * location : {"id":"WX4FBXXFKE4F","name":"北京","country":"CN","path":"北京,北京,中国","timezone":"Asia/Shanghai","timezone_offset":"+08:00"}         * now : {"text":"多云","code":"4","temperature":"13"}         * last_update : 2016-10-12T00:45:00+08:00         */        public String last_update;        public  class LocationBean {            public String id;            public String name;            public String country;            public String path;            public String timezone;            public String timezone_offset;        }        public  class NowBean {            public String text;            public String code;            public String temperature;        }    }}
service端
public interface GitHubService {    // http://v.juhe.cn/weather/citys?key=2c55ec0294cc464e92ba4647be41d9b2//    @GET("/v3/weather/now.json")//    Call<MainInfo> listRepos(@Query("key") String key);
//    https://api.thinkpage.cn/v3/weather/now.json?key=rot2enzrehaztkdk&location=beijing    @GET("v3/weather/now.json?key=rot2enzrehaztkdk&location=beijing")    Call<ResultEnity> retrofit(); //@QueryMap HashMap<String,String> hashMap}
activity端 当然也可以自己封装
Retrofit retrofit2 = new Retrofit.Builder()        .baseUrl("https://api.thinkpage.cn/")        .addConverterFactory(GsonConverterFactory.create())        .build();GitHubService service = retrofit2.create(GitHubService.class);HashMap<String,String> hashMap = new HashMap<>();hashMap.put("key", "rot2enzrehaztkdk");hashMap.put("location","beijing");Call<ResultEnity> call = service.retrofit();

call.enqueue(new Callback<ResultEnity>() {    @Override    public void onResponse(Call<ResultEnity> call, Response<ResultEnity> response) {        ResultEnity enity = response.body();        ResultEnity.Kongbai location = enity.location.get(0);        Log.e("cylog", location.now.code + " ID:" + location.location.id);    }    @Override    public void onFailure(Call<ResultEnity> call, Throwable t) {    }});

---------------------------- 换一种数据--------------------------

http://112.124.22.238:8081/course_api/banner/query?type=1

[
    {
        "id": 1,
        "name": "音箱狂欢",
        "description": null,
        "imgUrl": "http://7mno4h.com2.z0.glb.qiniucdn.com/5608f3b5Nc8d90151.jpg",
        "type": 1
    },
    {
        "id": 2,
        "name": "手机国庆礼",
        "description": null,
        "imgUrl": "http://7mno4h.com2.z0.glb.qiniucdn.com/5608eb8cN9b9a0a39.jpg",
        "type": 1
    },
   
]

String BASE_URL = "http://112.124.22.238:8081/course_api/banner/";

@GET("query?")Call<List<SliderEntity>> getSliderEntitys(@Query("type") String type);
public class SliderBean {   /* {        "id": 1,            "name": "音箱狂欢",            "description": null,            "imgUrl": "http://7mno4h.com2.z0.glb.qiniucdn.com/5608f3b5Nc8d90151.jpg",            "type": 1    }*/    public int id;    public String name;    public String description;    public String imgUrl;    public int type;}
接下来就在activ一样了

CNService cnService = retrofit.create(CNService.class);retrofit2.Call<List<SliderBean>> call = cnService.getSliderEntitys("1");
public void onResponse(Call<List<SliderBean>> call, Response<List<SliderBean>> response) {    List<SliderBean> entities = response.body();    for (SliderBean entity : entities) {        final TextSliderView textSliderView = new TextSliderView(getActivity());        textSliderView.description(entity.name);        textSliderView.image(entity.imgUrl);        textSliderView.setScaleType(BaseSliderView.ScaleType.Fit);        mSliderLayout.addSlider(textSliderView);        LogUtil.d("qq","name :" + entity.name);    }}
打印的数据为

10-12 11:55:18.229 15433-15433/? D/qq: name :音箱狂欢
10-12 11:55:18.249 15433-15433/? D/qq: name :手机国庆礼
10-12 11:55:18.259 15433-15433/? D/qq: name :IT生活
10-12 11:55:18.269 15433-15433/? D/qq: name :母婴萌宝
10-12 11:55:18.279 15433-15433/? D/qq: name :国庆大礼包
10-12 11:55:18.289 15433-15433/? D/qq: name :手机大放假


0 0
原创粉丝点击