author:wangyimiao

desc:添加获取响应头部信息入口
master
yimiao 3 years ago
parent 832e6b70f8
commit 295019d54a

@ -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<HttpCommonInterceptor> interceptors = new ArrayList<>();
HttpCommonInterceptor.Builder builder = new HttpCommonInterceptor.Builder();
List<RequestHeadInterceptor> 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<ResponseHeadInterceptor> interceptors = new ArrayList<>();
interceptors.add(new ResponseHeadInterceptor(response -> {
Headers headers = response.headers();
List<String> cookies = headers.values("Set-Cookie");
}));
new LoginLoader(interceptors).login("wang11", "wang456852", new RequestCallBack<LoginResponse>() {
@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");
}
});
});
}
}

@ -0,0 +1,17 @@
package com.common.commonlibtest.bean
class LoginData {
var admin: Boolean = false
var chapterTops: List<Long>? = null
var coinCount: Int = 0
var collectIds: List<String>? = 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 = ""
}

@ -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
}

@ -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<Interceptor>) :
BaseObserve<LoginLoader.LoginApi>() {
private val api: LoginApi
init {
api = initService(LoginApi::class.java, interceptors)
}
fun login(userName: String, pwd: String, callBack: RequestCallBack<LoginResponse>) {
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<LoginResponse>
}
}

@ -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" />
<Button
android:id="@+id/login_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="login"
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.690" />
</androidx.constraintlayout.widget.ConstraintLayout>

@ -7,7 +7,7 @@ import okhttp3.Response
/**
* 公共请求拦截器
*/
class HttpCommonInterceptor : Interceptor {
class RequestHeadInterceptor : Interceptor {
/**
* 请求头部参数
*/
@ -32,10 +32,10 @@ class HttpCommonInterceptor : Interceptor {
* builder构造函数
*/
class Builder {
private var mHttpCommonInterceptor: HttpCommonInterceptor? = null
private var mHttpCommonInterceptor: RequestHeadInterceptor? = null
init {
mHttpCommonInterceptor = HttpCommonInterceptor()
mHttpCommonInterceptor = RequestHeadInterceptor()
}
fun addHeadParams(key: String, value: String): Builder {
@ -63,7 +63,7 @@ class HttpCommonInterceptor : Interceptor {
return this
}
fun build(): HttpCommonInterceptor {
fun build(): RequestHeadInterceptor {
return mHttpCommonInterceptor!!
}
}

@ -0,0 +1,21 @@
package com.common.commonlib.net.interceptor
import android.util.Log
import okhttp3.Interceptor
import okhttp3.Response
/**
* 响应头部拦截器
*/
class ResponseHeadInterceptor(private var callBack: ResponseCallBack?) : Interceptor {
override fun intercept(chain: Interceptor.Chain): Response {
// 获取响应
val response = chain.proceed(chain.request())
callBack?.onResult(response)
return response
}
interface ResponseCallBack {
fun onResult(response: Response)
}
}
Loading…
Cancel
Save