From ba794144122a679fad6bf6120aa81e2b36ef51fb Mon Sep 17 00:00:00 2001 From: yimiao Date: Mon, 19 Jul 2021 18:16:42 +0800 Subject: [PATCH] =?UTF-8?q?author:wangyimiao=20desc:=E6=B7=BB=E5=8A=A0?= =?UTF-8?q?=E8=87=AA=E5=AE=9A=E4=B9=89=E6=8B=A6=E6=88=AA=E5=99=A8API?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../common/commonlibtest/MainActivity.java | 30 +++++++++ .../commonlibtest/bean/CollectionData.kt | 15 +++++ .../commonlibtest/bean/CollectionResponse.kt | 8 +++ .../commonlibtest/manager/CollectionLoader.kt | 29 +++++++++ .../commonlibtest/manager/InternetLoader.kt | 3 + app/src/main/res/layout/activity_main.xml | 14 +++- commonLib/build.gradle | 4 +- .../com/common/commonlib/net/BaseObserve.kt | 7 +- .../common/commonlib/net/RequestManager.kt | 64 +++++++++++++------ .../net/interceptor/BaseUrlInterceptor.kt | 2 +- .../net/interceptor/HttpCommonInterceptor.kt | 6 +- 11 files changed, 153 insertions(+), 29 deletions(-) create mode 100644 app/src/main/java/com/common/commonlibtest/bean/CollectionData.kt create mode 100644 app/src/main/java/com/common/commonlibtest/bean/CollectionResponse.kt create mode 100644 app/src/main/java/com/common/commonlibtest/manager/CollectionLoader.kt diff --git a/app/src/main/java/com/common/commonlibtest/MainActivity.java b/app/src/main/java/com/common/commonlibtest/MainActivity.java index f55c5e1..2b1ee28 100644 --- a/app/src/main/java/com/common/commonlibtest/MainActivity.java +++ b/app/src/main/java/com/common/commonlibtest/MainActivity.java @@ -6,12 +6,18 @@ import android.os.Bundle; import android.util.Log; import com.common.commonlib.net.RequestCallBack; +import com.common.commonlib.net.interceptor.HttpCommonInterceptor; import com.common.commonlibtest.bean.ArticlesResponse; +import com.common.commonlibtest.bean.CollectionResponse; +import com.common.commonlibtest.manager.CollectionLoader; import com.common.commonlibtest.manager.InternetLoader; import com.yinuo.commonlibtest.databinding.ActivityMainBinding; import org.jetbrains.annotations.Nullable; +import java.util.ArrayList; +import java.util.List; + public class MainActivity extends AppCompatActivity { private static final String TAG = MainActivity.class.getSimpleName(); ActivityMainBinding binding; @@ -44,5 +50,29 @@ public class MainActivity extends AppCompatActivity { } }); }); + + binding.btn.setOnClickListener(l -> { + List interceptors = new ArrayList<>(); + HttpCommonInterceptor.Builder builder = new HttpCommonInterceptor.Builder(); + builder.addHeadParams("Cookie", "loginUserName_wanandroid_com=wang11;token_pass_wanandroid_com=6fbec4fe15d69af8b4531171798f3926;loginUserName=wang11;token_pass=6fbec4fe15d69af8b4531171798f3926"); + interceptors.add(builder.build()); + CollectionLoader loader = new CollectionLoader(interceptors); + loader.getCollections(0, new RequestCallBack() { + @Override + public void onResult(CollectionResponse result) { + Log.d(TAG, "onResult"); + } + + @Override + public void onError(@Nullable String error) { + Log.d(TAG, "onError"); + } + + @Override + public void onComplete() { + Log.d(TAG, "onComplete"); + } + }); + }); } } \ No newline at end of file diff --git a/app/src/main/java/com/common/commonlibtest/bean/CollectionData.kt b/app/src/main/java/com/common/commonlibtest/bean/CollectionData.kt new file mode 100644 index 0000000..6abcdb5 --- /dev/null +++ b/app/src/main/java/com/common/commonlibtest/bean/CollectionData.kt @@ -0,0 +1,15 @@ +package com.common.commonlibtest.bean + +import android.os.Parcelable +import com.common.commonlib.net.bean.BaseResponse +import kotlinx.android.parcel.Parcelize + +@Parcelize +data class CollectionData(var curPage: Int = 0) : Parcelable { + var datas: List
? = null + var offset: Int = 0 + var over: Boolean = false + var pageCount: Int = 0 + var size: Int = 0 + var total: Int = 0 +} diff --git a/app/src/main/java/com/common/commonlibtest/bean/CollectionResponse.kt b/app/src/main/java/com/common/commonlibtest/bean/CollectionResponse.kt new file mode 100644 index 0000000..89a9934 --- /dev/null +++ b/app/src/main/java/com/common/commonlibtest/bean/CollectionResponse.kt @@ -0,0 +1,8 @@ +package com.common.commonlibtest.bean + +import android.os.Parcelable +import com.common.commonlib.net.bean.BaseResponse +import kotlinx.android.parcel.Parcelize + +@Parcelize +data class CollectionResponse(var data: CollectionData) : BaseResponse(), Parcelable diff --git a/app/src/main/java/com/common/commonlibtest/manager/CollectionLoader.kt b/app/src/main/java/com/common/commonlibtest/manager/CollectionLoader.kt new file mode 100644 index 0000000..8d51636 --- /dev/null +++ b/app/src/main/java/com/common/commonlibtest/manager/CollectionLoader.kt @@ -0,0 +1,29 @@ +package com.common.commonlibtest.manager + +import com.common.commonlib.net.BaseObserve +import com.common.commonlib.net.RequestCallBack +import com.common.commonlibtest.bean.CollectionResponse +import io.reactivex.Observable +import okhttp3.Interceptor +import retrofit2.http.GET +import retrofit2.http.Headers +import retrofit2.http.Path + +class CollectionLoader(interceptors: List) : + BaseObserve() { + private val api: CollectionApi + + init { + api = initService(CollectionApi::class.java, interceptors) + } + + fun getCollections(pageName: Int, callBack: RequestCallBack) { + observe(api.getCollections(pageName), callBack) + } + + interface CollectionApi { + @Headers("baseurl:debug1") + @GET("/lg/collect/list/{pageNum}/json") + fun getCollections(@Path("pageNum") pageNum: Int): Observable + } +} \ No newline at end of file diff --git a/app/src/main/java/com/common/commonlibtest/manager/InternetLoader.kt b/app/src/main/java/com/common/commonlibtest/manager/InternetLoader.kt index 1e74af8..c88a652 100644 --- a/app/src/main/java/com/common/commonlibtest/manager/InternetLoader.kt +++ b/app/src/main/java/com/common/commonlibtest/manager/InternetLoader.kt @@ -8,6 +8,9 @@ import retrofit2.http.GET import retrofit2.http.Headers import retrofit2.http.Path +/** + * 网络请求样例 + */ class InternetLoader : BaseObserve() { var homeApi: HomeApi diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml index 7b64f39..0e19d78 100644 --- a/app/src/main/res/layout/activity_main.xml +++ b/app/src/main/res/layout/activity_main.xml @@ -3,8 +3,7 @@ xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" - android:layout_height="match_parent" - tools:context=".MainActivity"> + android:layout_height="match_parent"> +