public class MyApp extends Application {
@Override
public void onCreate() {
super.onCreate();
Fresco.initialize(this);
}
}
失败和成功回调的接口
public interface OnNetListener<T> {
public void onSuccess(T t);
public void onFailure(Exception e);
}
Api-网络请求地址
public class Api {
public static final String HOST = "http://api.tianapi.com/nba/";
public static final String URL = "?key=71e58b5b2f930eaf1f937407acde08fe&num=10";
}
ServerApi-Retrofit需要用到的接口
public interface ServerApi {
@GET(Api.URL)
Observable<ClassBean> call();
}
Retrofit的工具类-创建OkHttpClient和ServerApi请求
public class RetrofitHelper {
public static OkHttpClient client;
public static ServerApi serverApi;
static {
getClient();
}
public static OkHttpClient getClient() {
if (client == null){
synchronized (OkHttpClient.class){
if (client == null){
client = new OkHttpClient();
}
}
}
return client;
}
public static ServerApi getServerApi(){
if (serverApi == null){
synchronized (ServerApi.class){
if (serverApi == null){
serverApi = OnCreate(ServerApi.class,Api.HOST);
}
}
}
return serverApi;
}
public static <T> T OnCreate(Class<T> tClass,String url){
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(url)
.addCallAdapterFactory(RxJavaCallAdapterFactory.create())
.addConverterFactory(GsonConverterFactory.create())
.build();
return retrofit.create(tClass);
}
}
bean类--网络请求接口的数据
public class ClassBean {
private int code;
private String msg;
private List<NewslistBean> newslist;
public int getCode() {
return code;
}
public void setCode(int code) {
this.code = code;
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
public List<NewslistBean> getNewslist() {
return newslist;
}
public void setNewslist(List<NewslistBean> newslist) {
this.newslist = newslist;
}
public static class NewslistBean {
private String ctime;
private String title;
private String description;
private String picUrl;
private String url;
public String getCtime() {
return ctime;
}
public void setCtime(String ctime) {
this.ctime = ctime;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getPicUrl() {
return picUrl;
}
public void setPicUrl(String picUrl) {
this.picUrl = picUrl;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
}
}
Model层的数据,请求数据
public class MainModel implements IMainModel {
@Override
public void recy(final OnNetListener<ClassBean> bean) {
ServerApi serverApi = RetrofitHelper.getServerApi();
serverApi.call()
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Subscriber<ClassBean>() {
@Override
public void onCompleted() {
}
@Override
public void onError(Throwable e) {
}
@Override
public void onNext(ClassBean classBean) {
bean.onSuccess(classBean);
}
});
}
}
model层对应的接口
public interface IMainModel {
public void recy(OnNetListener<ClassBean> bean);
}
presenter层--view和model的交互层
public class MainPresenter {
public IMainModel iMainModel;
public IMainActivity iMainActivity;
public MainPresenter(IMainActivity iMainActivity) {
this.iMainActivity = iMainActivity;
iMainModel = new MainModel();
}
public void getData(){
iMainModel.recy(new OnNetListener<ClassBean>() {
@Override
public void onSuccess(ClassBean classBean) {
iMainActivity.showrecy(classBean);
}
@Override
public void onFailure(Exception e) {
}
});
}
public void getMore(){
iMainModel.recy(new OnNetListener<ClassBean>() {
@Override
public void onSuccess(ClassBean classBean) {
iMainActivity.showrecys(classBean);
}
@Override
public void onFailure(Exception e) {
}
});
}
}