天天看点

下拉刷新上拉加载第三方框架/post 网络请求

下拉刷新上拉加载第三方框架

依赖

implementation 'com.scwang.smartrefresh:SmartRefreshLayout:1.0.3'
    implementation 'com.scwang.smartrefresh:SmartRefreshHeader:1.0.3'
           

布局

<com.scwang.smartrefresh.layout.SmartRefreshLayout
        android:id="@+id/smartrefresh"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <android.support.v7.widget.RecyclerView
            android:layout_width="match_parent"
            android:layout_below="@id/toolbar"
            android:layout_height="match_parent"
            android:id="@+id/rv"></android.support.v7.widget.RecyclerView>
    </com.scwang.smartrefresh.layout.SmartRefreshLayout>
           

java代码获取id 设置点击事件

smartrefresh.setOnRefreshListener(new OnRefreshListener() {
    @Override
    public void onRefresh(RefreshLayout refreshlayout) {

        smartrefresh.finishRefresh();//时间
        initData();//数据
    }
});
smartrefresh.setOnLoadmoreListener(new OnLoadmoreListener() {
    @Override
    public void onLoadmore(RefreshLayout refreshlayout) {
   smartrefresh.finishLoadmore();
    }
});
           

post 网络请求

依赖

implementation 'com.google.code.gson:gson:2.8.1'  //gson
          implementation 'com.squareup.retrofit2:adapter-rxjava:2.1.0'//retrofit+rxjava
          implementation 'io.reactivex:rxandroid:1.1.0'
          implementation 'io.reactivex:rxjava:1.1.6'
          implementation 'com.squareup.okhttp3:logging-interceptor:3.3.1'
          compile 'com.squareup.retrofit2:converter-gson:2.1.0'
           

Retrofit +Rxjava

public static Retrofit getRetrofit(String baseUrl) {
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(baseUrl)
                .addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(RxJavaCallAdapterFactory.create()).build();
        return retrofit;
    }
           
public interface NetService<T> {
/*
*
* 第一个接口:
http://39.107.224.233/firstga/app/news/listNewsChannel
请求头:
Content-Type:application/x-www-form-urlencoded
* */
public  static String base1 ="http://39.107.224.233/firstga/app/news/";
    @Headers("Content-Type:application/x-www-form-urlencoded")//请求头
    @POST("listNewsChannel")//post请求
   //Rxjava
    Observable<DataInfo> one();
           

请求数据

public void one(View view) {
        NetService netService = getRetrofit(NetService.base1).create(NetService.class);
        Observable<DataInfo> one = netService.one();
        one.subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<DataInfo>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
                        Log.e("失败", e.toString());//请求失败

                    }

                    @Override
                    public void onNext(DataInfo dataInfo) {
                    //请求成功
                        if (dataInfo.getMessage() != null) mTextView.setText(dataInfo.getMessage());
                    }
                });
    }