desc:提供网络请求的抽象类

author:wangyimiao
master
yimiao 3 years ago
parent e13edf32aa
commit 3d7a82125c

@ -8,7 +8,8 @@ import com.common.commonlib.image.config.GlideHolderConfig;
import com.common.commonlib.image.config.GlideTransitionConfig; import com.common.commonlib.image.config.GlideTransitionConfig;
import com.common.commonlib.image.loader.ImageLoader; import com.common.commonlib.image.loader.ImageLoader;
import com.common.commonlib.log.Logger; import com.common.commonlib.log.Logger;
import com.common.commonlib.net.RequestCallBack; import com.common.commonlib.net.callback.RequestNoResultCallBack;
import com.common.commonlib.net.callback.RequestResultCallBack;
import com.common.commonlib.net.interceptor.RequestHeadInterceptor; import com.common.commonlib.net.interceptor.RequestHeadInterceptor;
import com.common.commonlib.net.interceptor.ResponseHeadInterceptor; import com.common.commonlib.net.interceptor.ResponseHeadInterceptor;
import com.common.commonlib.utils.BaseUtils; import com.common.commonlib.utils.BaseUtils;
@ -53,7 +54,7 @@ public class DemoActivity extends AppCompatActivity {
binding.tv.setOnClickListener(l -> { binding.tv.setOnClickListener(l -> {
InternetLoader loader = new InternetLoader(); InternetLoader loader = new InternetLoader();
loader.getHomeArticles(0, new RequestCallBack<ArticlesResponse>() { loader.getHomeArticles(0, new RequestResultCallBack<ArticlesResponse>() {
@Override @Override
public void onResult(ArticlesResponse result) { public void onResult(ArticlesResponse result) {
Logger.INSTANCE.d(TAG, result.getData().toString()); Logger.INSTANCE.d(TAG, result.getData().toString());
@ -63,11 +64,6 @@ public class DemoActivity extends AppCompatActivity {
public void onError(@Nullable String error) { public void onError(@Nullable String error) {
Logger.INSTANCE.d(TAG, "onError"); Logger.INSTANCE.d(TAG, "onError");
} }
@Override
public void onComplete() {
Logger.INSTANCE.d(TAG, "onComplete");
}
}); });
}); });
@ -79,12 +75,7 @@ public class DemoActivity extends AppCompatActivity {
builder.addHeadParams("Cookie", "loginUserName_wanandroid_com=wang11;token_pass_wanandroid_com=6fbec4fe15d69af8b4531171798f3926;loginUserName=wang11;token_pass=6fbec4fe15d69af8b4531171798f3926"); builder.addHeadParams("Cookie", "loginUserName_wanandroid_com=wang11;token_pass_wanandroid_com=6fbec4fe15d69af8b4531171798f3926;loginUserName=wang11;token_pass=6fbec4fe15d69af8b4531171798f3926");
interceptors.add(builder.build()); interceptors.add(builder.build());
CollectionLoader loader = new CollectionLoader(interceptors); CollectionLoader loader = new CollectionLoader(interceptors);
loader.getCollections(0, new RequestCallBack<CollectionResponse>() { loader.getCollections(0, new RequestNoResultCallBack<CollectionResponse>() {
@Override
public void onResult(CollectionResponse result) {
Logger.INSTANCE.d(TAG, "onResult");
}
@Override @Override
public void onError(@Nullable String error) { public void onError(@Nullable String error) {
Logger.INSTANCE.d(TAG, "onError"); Logger.INSTANCE.d(TAG, "onError");
@ -103,12 +94,7 @@ public class DemoActivity extends AppCompatActivity {
Headers headers = response.headers(); Headers headers = response.headers();
List<String> cookies = headers.values("Set-Cookie"); List<String> cookies = headers.values("Set-Cookie");
})); }));
new LoginLoader(interceptors).login("wang11", "wang456852", new RequestCallBack<LoginResponse>() { new LoginLoader(interceptors).login("wang11", "wang456852", new RequestNoResultCallBack<LoginResponse>() {
@Override
public void onResult(LoginResponse result) {
Logger.INSTANCE.d(TAG, "onResult");
}
@Override @Override
public void onError(@Nullable String error) { public void onError(@Nullable String error) {
Logger.INSTANCE.e(TAG, "onError"); Logger.INSTANCE.e(TAG, "onError");

@ -1,7 +1,7 @@
package com.common.commonlibtest.manager package com.common.commonlibtest.manager
import com.common.commonlib.net.BaseObserve import com.common.commonlib.net.BaseObserve
import com.common.commonlib.net.RequestCallBack import com.common.commonlib.net.callback.RequestCallBack
import com.common.commonlibtest.bean.CollectionResponse import com.common.commonlibtest.bean.CollectionResponse
import io.reactivex.rxjava3.core.Observable import io.reactivex.rxjava3.core.Observable
import okhttp3.Interceptor import okhttp3.Interceptor

@ -1,7 +1,7 @@
package com.common.commonlibtest.manager package com.common.commonlibtest.manager
import com.common.commonlib.net.RequestCallBack
import com.common.commonlib.net.BaseObserve import com.common.commonlib.net.BaseObserve
import com.common.commonlib.net.callback.RequestCallBack
import com.common.commonlibtest.bean.ArticlesResponse import com.common.commonlibtest.bean.ArticlesResponse
import io.reactivex.rxjava3.core.Observable import io.reactivex.rxjava3.core.Observable
import retrofit2.http.GET import retrofit2.http.GET

@ -1,7 +1,7 @@
package com.common.commonlibtest.manager package com.common.commonlibtest.manager
import com.common.commonlib.net.BaseObserve import com.common.commonlib.net.BaseObserve
import com.common.commonlib.net.RequestCallBack import com.common.commonlib.net.callback.RequestCallBack
import com.common.commonlibtest.bean.LoginResponse import com.common.commonlibtest.bean.LoginResponse
import io.reactivex.rxjava3.core.Observable import io.reactivex.rxjava3.core.Observable
import okhttp3.Interceptor import okhttp3.Interceptor

@ -2,6 +2,7 @@ package com.common.commonlib.net
import android.net.ParseException import android.net.ParseException
import android.util.Log import android.util.Log
import com.common.commonlib.net.callback.RequestCallBack
import com.google.gson.JsonParseException import com.google.gson.JsonParseException
import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers
import io.reactivex.rxjava3.core.Observable import io.reactivex.rxjava3.core.Observable

@ -1,13 +0,0 @@
package com.common.commonlib.net
/**
* 请求回调
*
* @author wangym
* @since 2021/7/28
*/
interface RequestCallBack<T> {
fun onResult(result: T)
fun onError(error: String?)
fun onComplete()
}

@ -17,12 +17,12 @@ import java.util.concurrent.TimeUnit
*/ */
object RequestManager { object RequestManager {
/** /**
* 默认超时时间 * 默认超时时间(s)
*/ */
private const val defaultTimeOut: Long = 5 private const val defaultTimeOut: Long = 5
/** /**
* 默认读取超时时间 * 默认读取超时时间(s)
*/ */
private const val defaultReadTimeOut: Long = 10 private const val defaultReadTimeOut: Long = 10

@ -0,0 +1,19 @@
package com.common.commonlib.net.callback
import com.common.commonlib.log.Logger
/**
* 不需要关注onComplete的请求回调
*
* @author wangym
* @since 2021/8/2
*/
abstract class RequestResultCallBack<T> : RequestCallBack<T> {
abstract override fun onResult(result: T)
abstract override fun onError(error: String?)
override fun onComplete() {
Logger.d("RequestResultCallBack: onComplete")
}
}

@ -10,11 +10,15 @@ import android.widget.FrameLayout;
import android.widget.ImageView; import android.widget.ImageView;
import android.widget.TextView; import android.widget.TextView;
import androidx.navigation.Navigation;
import com.common.commonlib.R; import com.common.commonlib.R;
import com.common.commonlib.utils.DisplayUtils; import com.common.commonlib.utils.DisplayUtils;
/**
*
*
* @author miracle
* @since 2021/8/2
*/
public class CommonTitleView extends FrameLayout implements View.OnClickListener { public class CommonTitleView extends FrameLayout implements View.OnClickListener {
private Context mContext; private Context mContext;
private ImageView ivLeft; private ImageView ivLeft;

@ -16,6 +16,12 @@ import androidx.annotation.Nullable;
import com.common.commonlib.R; import com.common.commonlib.R;
/**
* micView
*
* @author miracle
* @since 2021/8/2
*/
public class MikeView extends View { public class MikeView extends View {
//进度条格数 //进度条格数
private final int COUNT = 16; private final int COUNT = 16;

@ -16,10 +16,10 @@ import androidx.recyclerview.widget.RecyclerView;
/** /**
* RecyclerView * RecyclerView
* <p> *
* Created by DavidChen on 2018/5/29. * @author wangym
* @since 2021/8/2
*/ */
public class SlideRecyclerView extends RecyclerView { public class SlideRecyclerView extends RecyclerView {
private static final String TAG = "SlideRecyclerView"; private static final String TAG = "SlideRecyclerView";
private static final int INVALID_POSITION = -1; // 触摸到的点不在子View范围内 private static final int INVALID_POSITION = -1; // 触摸到的点不在子View范围内

@ -9,6 +9,12 @@ import androidx.appcompat.app.AppCompatActivity;
import com.common.commonlib.R; import com.common.commonlib.R;
/**
* activity
*
* @author miracle
* @since 2021/8/2
*/
public class BaseActivity extends AppCompatActivity { public class BaseActivity extends AppCompatActivity {
@Override @Override

@ -28,6 +28,9 @@ import java.util.List;
* Activity * Activity
* ActivityrequestPermission * ActivityrequestPermission
* *
*
* @author miracle
* @since 2021/8/2
*/ */
public class PermissionCompatActivity extends AppCompatActivity { public class PermissionCompatActivity extends AppCompatActivity {
private static final String TAG = "PermissionCompatActivity"; private static final String TAG = "PermissionCompatActivity";

@ -32,6 +32,7 @@
<dimen name="font_40dp">40dp</dimen> <dimen name="font_40dp">40dp</dimen>
<dimen name="font_42dp">42dp</dimen> <dimen name="font_42dp">42dp</dimen>
<dimen name="font_46dp">46dp</dimen> <dimen name="font_46dp">46dp</dimen>
<dimen name="font_60dp">60dp</dimen>
<dimen name="font_65dp">65dp</dimen> <dimen name="font_65dp">65dp</dimen>
<dimen name="font_70dp">70dp</dimen> <dimen name="font_70dp">70dp</dimen>
<dimen name="font_72dp">72dp</dimen> <dimen name="font_72dp">72dp</dimen>

Loading…
Cancel
Save