导入依赖
M和P层接口
P和V
Retrofit请求地址拼接接口
RetrofitUtils
NewsPresenter
V层使用
- compile 'com.squareup.retrofit2:retrofit:2.0.0'
- compile 'com.squareup.retrofit2:converter-gson:2.0.2'
- compile 'com.google.code.gson:gson:2.8.2'
实体类
- public class Beannes<T> {
- private String code;
- private String msg;
- private T newslist;
- public String getCode() {
- return code;
- }
- public void setCode(String code) {
- this.code = code;
- }
- public String getMsg() {
- return msg;
- }
- public void setMsg(String msg) {
- this.msg = msg;
- }
- public T getNewslist() {
- return newslist;
- }
- public void setNewslist(T newslist) {
- this.newslist = newslist;
- }
- }
- public class NewsBean {
- 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;
- }
- }
- public interface CallBack {
- void onSuccess(Object o);
- void onFailed(Throwable t);
- }
- public interface IView {
- void success(Beannes<List<NewsBean>> data);
- void failed(Throwable e);
- }
- public interface RetrofitService {
- @HTTP(method = "GET", path = "nba", hasBody = false)
- Call<Beannes<List<NewsBean>>> getBlog(@Query("key") String ad1, @Query("num") String id2);
- }
- public class RetrofitUtils {//单例
- private static volatile RetrofitUtils instance;
- public static RetrofitUtils getInstance(){
- if (instance==null){
- synchronized (RetrofitUtils.class){
- if (instance==null){
- instance=new RetrofitUtils();
- }
- }
- }
- return instance;
- }
- public void getNews(String baseurl, final CallBack callBack, String ad1, int ad2){
- Retrofit retrofit = new Retrofit.Builder()
- .baseUrl(baseurl)
- .addConverterFactory(GsonConverterFactory.create())
- .build();
- RetrofitService retrofitService = retrofit.create(RetrofitService.class);
- Call<Beannes<List<NewsBean>>> call = retrofitService.getBlog(ad1, ad2+"");
- call.enqueue(new Callback<Beannes<List<NewsBean>>>() {
- @Override
- public void onResponse(Call<Beannes<List<NewsBean>>> call, Response<Beannes<List<NewsBean>>> response) {
- Beannes<List<NewsBean>> body = response.body();
- callBack.onSuccess(body);
- }
- @Override
- public void onFailure(Call<Beannes<List<NewsBean>>> call, Throwable t) {
- callBack.onFailed(t);
- }
- });
- }
- }
- public class NewsPresenter {
- private IView iView;
- public NewsPresenter() {
- }
- public void attachView(IView iView) {
- this.iView = iView;
- }
- public void getData(String url, String ad1, int ad2) {
- RetrofitUtils.getInstance().getNews(url, new CallBack() {
- @Override
- public void onSuccess(Object o) {
- Beannes<List<NewsBean>> data = (Beannes<List<NewsBean>>) o;
- iView.success(data);
- }
- @Override
- public void onFailed(Throwable t) {
- iView.failed(t);
- }
- }, ad1, ad2);
- }
- }
- private void getNews() {
- presenter = new NewsPresenter();
- presenter.attachView(this);
- presenter.getData("http://api.tianapi.com/", "71e58b5b2f930eaf1f937407acde08fe", i);
- }