From 295019d54a0cbb9447d87e85755aff0431a82c08 Mon Sep 17 00:00:00 2001 From: yimiao Date: Tue, 20 Jul 2021 11:39:57 +0800 Subject: [PATCH] =?UTF-8?q?author:wangyimiao=20desc:=E6=B7=BB=E5=8A=A0?= =?UTF-8?q?=E8=8E=B7=E5=8F=96=E5=93=8D=E5=BA=94=E5=A4=B4=E9=83=A8=E4=BF=A1?= =?UTF-8?q?=E6=81=AF=E5=85=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../common/commonlibtest/MainActivity.java | 37 +++++++++++++++++-- .../common/commonlibtest/bean/LoginData.kt | 17 +++++++++ .../commonlibtest/bean/LoginResponse.kt | 10 +++++ .../commonlibtest/manager/LoginLoader.kt | 31 ++++++++++++++++ app/src/main/res/layout/activity_main.xml | 13 ++++++- ...terceptor.kt => RequestHeadInterceptor.kt} | 8 ++-- .../interceptor/ResponseHeadInterceptor.kt | 21 +++++++++++ 7 files changed, 129 insertions(+), 8 deletions(-) create mode 100644 app/src/main/java/com/common/commonlibtest/bean/LoginData.kt create mode 100644 app/src/main/java/com/common/commonlibtest/bean/LoginResponse.kt create mode 100644 app/src/main/java/com/common/commonlibtest/manager/LoginLoader.kt rename commonLib/src/main/java/com/common/commonlib/net/interceptor/{HttpCommonInterceptor.kt => RequestHeadInterceptor.kt} (89%) create mode 100644 commonLib/src/main/java/com/common/commonlib/net/interceptor/ResponseHeadInterceptor.kt diff --git a/app/src/main/java/com/common/commonlibtest/MainActivity.java b/app/src/main/java/com/common/commonlibtest/MainActivity.java index 2b1ee28..70d9082 100644 --- a/app/src/main/java/com/common/commonlibtest/MainActivity.java +++ b/app/src/main/java/com/common/commonlibtest/MainActivity.java @@ -6,18 +6,25 @@ import android.os.Bundle; import android.util.Log; import com.common.commonlib.net.RequestCallBack; -import com.common.commonlib.net.interceptor.HttpCommonInterceptor; +import com.common.commonlib.net.interceptor.RequestHeadInterceptor; +import com.common.commonlib.net.interceptor.ResponseHeadInterceptor; import com.common.commonlibtest.bean.ArticlesResponse; import com.common.commonlibtest.bean.CollectionResponse; +import com.common.commonlibtest.bean.LoginResponse; import com.common.commonlibtest.manager.CollectionLoader; import com.common.commonlibtest.manager.InternetLoader; +import com.common.commonlibtest.manager.LoginLoader; import com.yinuo.commonlibtest.databinding.ActivityMainBinding; +import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import java.util.ArrayList; import java.util.List; +import okhttp3.Headers; +import okhttp3.Response; + public class MainActivity extends AppCompatActivity { private static final String TAG = MainActivity.class.getSimpleName(); ActivityMainBinding binding; @@ -52,8 +59,8 @@ public class MainActivity extends AppCompatActivity { }); binding.btn.setOnClickListener(l -> { - List interceptors = new ArrayList<>(); - HttpCommonInterceptor.Builder builder = new HttpCommonInterceptor.Builder(); + List interceptors = new ArrayList<>(); + RequestHeadInterceptor.Builder builder = new RequestHeadInterceptor.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); @@ -74,5 +81,29 @@ public class MainActivity extends AppCompatActivity { } }); }); + + binding.loginBtn.setOnClickListener(l -> { + List interceptors = new ArrayList<>(); + interceptors.add(new ResponseHeadInterceptor(response -> { + Headers headers = response.headers(); + List cookies = headers.values("Set-Cookie"); + })); + new LoginLoader(interceptors).login("wang11", "wang456852", new RequestCallBack() { + @Override + public void onResult(LoginResponse 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/LoginData.kt b/app/src/main/java/com/common/commonlibtest/bean/LoginData.kt new file mode 100644 index 0000000..0482b4c --- /dev/null +++ b/app/src/main/java/com/common/commonlibtest/bean/LoginData.kt @@ -0,0 +1,17 @@ +package com.common.commonlibtest.bean + +class LoginData { + var admin: Boolean = false + var chapterTops: List? = null + var coinCount: Int = 0 + var collectIds: List? = null + var email: String = "" + var icon: String = "" + var id: Long = 0 + var nickname: String = "" + var password: String = "" + var publicName: String = "" + var token: String = "" + var type: Int = 0 + var username: String = "" +} \ No newline at end of file diff --git a/app/src/main/java/com/common/commonlibtest/bean/LoginResponse.kt b/app/src/main/java/com/common/commonlibtest/bean/LoginResponse.kt new file mode 100644 index 0000000..dafbbd6 --- /dev/null +++ b/app/src/main/java/com/common/commonlibtest/bean/LoginResponse.kt @@ -0,0 +1,10 @@ +package com.common.commonlibtest.bean + +import android.os.Parcelable +import com.common.commonlib.net.bean.BaseResponse +import kotlinx.android.parcel.Parcelize + +@Parcelize +class LoginResponse : BaseResponse(), Parcelable { + var data: LoginData? = null +} \ No newline at end of file diff --git a/app/src/main/java/com/common/commonlibtest/manager/LoginLoader.kt b/app/src/main/java/com/common/commonlibtest/manager/LoginLoader.kt new file mode 100644 index 0000000..0492134 --- /dev/null +++ b/app/src/main/java/com/common/commonlibtest/manager/LoginLoader.kt @@ -0,0 +1,31 @@ +package com.common.commonlibtest.manager + +import com.common.commonlib.net.BaseObserve +import com.common.commonlib.net.RequestCallBack +import com.common.commonlibtest.bean.LoginResponse +import io.reactivex.Observable +import okhttp3.Interceptor +import retrofit2.http.* + +class LoginLoader(interceptors: List) : + BaseObserve() { + private val api: LoginApi + + init { + api = initService(LoginApi::class.java, interceptors) + } + + fun login(userName: String, pwd: String, callBack: RequestCallBack) { + observe(api.login(userName, pwd), callBack) + } + + interface LoginApi { + @FormUrlEncoded + @Headers("baseurl:debug1") + @POST("/user/login") + fun login( + @Field("username") userName: String, + @Field("password") pwd: String + ): Observable + } +} \ No newline at end of file diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml index 0e19d78..fad0dab 100644 --- a/app/src/main/res/layout/activity_main.xml +++ b/app/src/main/res/layout/activity_main.xml @@ -19,11 +19,22 @@ android:id="@+id/btn" android:layout_width="wrap_content" android:layout_height="wrap_content" - android:text="Hello World!" + android:text="get collections" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toTopOf="parent" app:layout_constraintVertical_bias="0.599" /> +