使用Retrofit网络请求SwipeRefreshLayout+Recyclerview下拉刷新上拉自动加载进行分页

导入的权限

implementation 'org.greenrobot:eventbus:3.1.1'
    implementation 'com.facebook.fresco:fresco:1.13.0'
    implementation 'com.jakewharton:butterknife:8.4.0'
    annotationProcessor "com.jakewharton:butterknife-compiler:8.4.0"
    implementation 'com.squareup.retrofit2:retrofit:2.0.0'
    implementation 'com.squareup.retrofit2:converter-gson:2.0.2'
    implementation 'com.android.support:recyclerview-v7:28.+'
    implementation 'org.greenrobot:greendao:3.0.1'
    implementation 'org.greenrobot:greendao-generator:3.0.0'
    implementation 'com.android.support:design:28.0.0'
<uses-permission android:name="android.permission.INTERNET"/>
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>

首先拼接接口

public class Api {
//http://www.xieast.com/api/news/news.php?page=1
public static final String BSEE_URL=“http://www.xieast.com/api/news/”;
public static final String SHOU=“news.php?page=1”;
}
public interface ApiService {

@GET
Observable<MyBean> shuo(@Url String url);

@GET
Observable<MyBean> nn(@Url String url,@HeaderMap HashMap<String,String> hashMap);

}

1

public class Aip {

    //http://mnews.gw.com.cn/wap/data/news/news/mobile/jbgg/page_1.json
    public static final String Base_Url="http://mnews.gw.com.cn/wap/data/news/news/";
    public static final String Main_url="mobile/jbgg/page_";
}

2

public interface ApiService {

    @GET(Aip.Main_url+"{page}")
    Call<List<MyBean>> getShow(@Path("page") String page);
}

3解析接口中的数据

public class MyBean {

    public List<DataBean> data;

    public List<DataBean> getData() {
        return data;
    }

    public void setData(List<DataBean> data) {
        this.data = data;
    }

    @Override
    public String toString() {
        return "MyBean{" +
                "data=" + data +
                '}';
    }
}

另一个类

@Entity
public class DataBean {

    @Property(nameInDb = "title")
    private String title;

    @Override
    public String toString() {
        return "DataBean{" +
                "title='" + title + '\'' +
                '}';
    }

    public String getTitle() {
        return this.title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    @Generated(hash = 1538556504)
    public DataBean(String title) {
        this.title = title;
    }

@Generated(hash = 908697775)
public DataBean() {
}

}

然后去写MVP框架

1写网络请求

public class RetrofitUtils {

    //单例模式
    private static RetrofitUtils retrofitUtils;

    private RetrofitUtils(){

    }
    public static RetrofitUtils getInstance(){
        if (retrofitUtils==null){
            synchronized (RetrofitUtils.class){
                if (retrofitUtils==null){
                    retrofitUtils=new RetrofitUtils();
                }
            }
        }
        return retrofitUtils;
    }
    //获取Retrofit方法
    private static Retrofit retrofit;

    private static synchronized Retrofit getRetrofit(String BASE_URL){
        retrofit = new Retrofit.Builder()
                .baseUrl(BASE_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .bui
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值