天天看點

下拉重新整理上拉加載第三方架構/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());
                    }
                });
    }