Retrofit 解析最外层是数组的JSON 串
json串:http://api.yunzhancn.cn/api/app.interface.php?siteid=78703&itemid=2&act=ad_app
实体类:
package com.example.monthlesson.Modle.Bean;
public class BannerBean {
/**
* aid : 10567
* title : 1
* siteid : 78703
* userid : 0
* pid : 2
* typeid : 5
* areaid : 0
* amount : 0
* currency :
* url :
* introduce :
* hits : 0
* username : admin78703
* addtime : 1539583199
* editor : admin78703
* edittime : 1539583199
* fromtime : 1539532800
* totime : 1602777599
* stat : 0
* note :
* code :
* text_name :
* text_url :
* text_title :
* text_style :
* image_src : http://d.yunzhancn.cn/file/upload/201810/15/1359503278703.png
* image_url :
* image_alt :
* flash_src :
* flash_url :
* flash_loop : 1
* key_moduleid : 0
* key_catid : 0
* key_word :
* key_id : 0
* listorder : 0
* status : 3
* alt :
* thumb : http://d.yunzhancn.cn/file/upload/201810/15/1359503278703.png
* linkurl :
*/
private String aid;
private String title;
private String siteid;
private String userid;
private String pid;
private String typeid;
private String areaid;
private String amount;
private String currency;
private String url;
private String introduce;
private String hits;
private String username;
private String addtime;
private String editor;
private String edittime;
private String fromtime;
private String totime;
private String stat;
private String note;
private String code;
private String text_name;
private String text_url;
private String text_title;
private String text_style;
private String image_src;
private String image_url;
private String image_alt;
private String flash_src;
private String flash_url;
private String flash_loop;
private String key_moduleid;
private String key_catid;
private String key_word;
private String key_id;
private String listorder;
private String status;
private String alt;
private String thumb;
private String linkurl;
public String getAid() {
return aid;
}
public void setAid(String aid) {
this.aid = aid;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getSiteid() {
return siteid;
}
public void setSiteid(String siteid) {
this.siteid = siteid;
}
public String getUserid() {
return userid;
}
public void setUserid(String userid) {
this.userid = userid;
}
public String getPid() {
return pid;
}
public void setPid(String pid) {
this.pid = pid;
}
public String getTypeid() {
return typeid;
}
public void setTypeid(String typeid) {
this.typeid = typeid;
}
public String getAreaid() {
return areaid;
}
public void setAreaid(String areaid) {
this.areaid = areaid;
}
public String getAmount() {
return amount;
}
public void setAmount(String amount) {
this.amount = amount;
}
public String getCurrency() {
return currency;
}
public void setCurrency(String currency) {
this.currency = currency;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public String getIntroduce() {
return introduce;
}
public void setIntroduce(String introduce) {
this.introduce = introduce;
}
public String getHits() {
return hits;
}
public void setHits(String hits) {
this.hits = hits;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getAddtime() {
return addtime;
}
public void setAddtime(String addtime) {
this.addtime = addtime;
}
public String getEditor() {
return editor;
}
public void setEditor(String editor) {
this.editor = editor;
}
public String getEdittime() {
return edittime;
}
public void setEdittime(String edittime) {
this.edittime = edittime;
}
public String getFromtime() {
return fromtime;
}
public void setFromtime(String fromtime) {
this.fromtime = fromtime;
}
public String getTotime() {
return totime;
}
public void setTotime(String totime) {
this.totime = totime;
}
public String getStat() {
return stat;
}
public void setStat(String stat) {
this.stat = stat;
}
public String getNote() {
return note;
}
public void setNote(String note) {
this.note = note;
}
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
public String getText_name() {
return text_name;
}
public void setText_name(String text_name) {
this.text_name = text_name;
}
public String getText_url() {
return text_url;
}
public void setText_url(String text_url) {
this.text_url = text_url;
}
public String getText_title() {
return text_title;
}
public void setText_title(String text_title) {
this.text_title = text_title;
}
public String getText_style() {
return text_style;
}
public void setText_style(String text_style) {
this.text_style = text_style;
}
public String getImage_src() {
return image_src;
}
public void setImage_src(String image_src) {
this.image_src = image_src;
}
public String getImage_url() {
return image_url;
}
public void setImage_url(String image_url) {
this.image_url = image_url;
}
public String getImage_alt() {
return image_alt;
}
public void setImage_alt(String image_alt) {
this.image_alt = image_alt;
}
public String getFlash_src() {
return flash_src;
}
public void setFlash_src(String flash_src) {
this.flash_src = flash_src;
}
public String getFlash_url() {
return flash_url;
}
public void setFlash_url(String flash_url) {
this.flash_url = flash_url;
}
public String getFlash_loop() {
return flash_loop;
}
public void setFlash_loop(String flash_loop) {
this.flash_loop = flash_loop;
}
public String getKey_moduleid() {
return key_moduleid;
}
public void setKey_moduleid(String key_moduleid) {
this.key_moduleid = key_moduleid;
}
public String getKey_catid() {
return key_catid;
}
public void setKey_catid(String key_catid) {
this.key_catid = key_catid;
}
public String getKey_word() {
return key_word;
}
public void setKey_word(String key_word) {
this.key_word = key_word;
}
public String getKey_id() {
return key_id;
}
public void setKey_id(String key_id) {
this.key_id = key_id;
}
public String getListorder() {
return listorder;
}
public void setListorder(String listorder) {
this.listorder = listorder;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
public String getAlt() {
return alt;
}
public void setAlt(String alt) {
this.alt = alt;
}
public String getThumb() {
return thumb;
}
public void setThumb(String thumb) {
this.thumb = thumb;
}
public String getLinkurl() {
return linkurl;
}
public void setLinkurl(String linkurl) {
this.linkurl = linkurl;
}
}
Retrofit 接口:
泛型加个数组标志,其他的和正常接JSON串一样
public interface MyInterface {
@GET("api/app.interface.php?siteid=78703")
Observable<BannerBean[]> getBanner(@Query("itemid")String itemid, @Query("act")String act);
}
具体实现:
public Observable<BannerBean[]> getBannerData(String itemid,String act) {
Observable<BannerBean[]> bannerobservable = new Retrofit.Builder()
.baseUrl(baseUrl)
.addConverterFactory(GsonConverterFactory.create())
.addCallAdapterFactory(RxJavaCallAdapterFactory.create())
.build()
.create(MyInterface.class)
.getBanner(itemid, act);
return bannerobservable;
}
public void getBannerData(String itemid, String act) {
compositeSubscription.add(iModle.getBannerData(itemid,act)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Observer<BannerBean[]>() {
@Override
public void onCompleted() {
}
@Override
public void onError(Throwable e) {
}
@Override
public void onNext(BannerBean[] bannerBeans) {
myView.setBanner(bannerBeans);
}
})
);
}
拿到数据:
public void setBanner(BannerBean[] bannerBeans) {
for (int i = 0; i < bannerBeans.length; i++) {
String image_src = bannerBeans[i].getImage_src();
banners.add(image_src);
}
frag_banner.setBannerStyle(BannerConfig.NUM_INDICATOR);
frag_banner.setDelayTime(2000);
frag_banner.setImages(banners);
frag_banner.setImageLoader(new ImageLoader() {
@Override
public void displayImage(Context context, Object path, ImageView imageView) {
Glide.with(context).load(path).into(imageView);
}
});
frag_banner.start();
}