修改获取token逻辑,以及处理token 401问题
This commit is contained in:
@ -40,9 +40,10 @@ import okhttp3.ResponseBody;
|
||||
import retrofit2.HttpException;
|
||||
import rx.Observable;
|
||||
import rx.android.schedulers.AndroidSchedulers;
|
||||
import rx.functions.Func1;
|
||||
import rx.schedulers.Schedulers;
|
||||
|
||||
import static com.gh.gamecenter.retrofit.RetrofitManager.getLibao;
|
||||
|
||||
/**
|
||||
* Created by khy on 2016/12/16.
|
||||
* 礼包工具类, 包括联网操作和领取按钮状态
|
||||
@ -72,14 +73,10 @@ public class LibaoUtils {
|
||||
}
|
||||
|
||||
//初始化存号箱 获取存号箱所有礼包
|
||||
public static void getCunHaoXiang(final Context context, final boolean isCheck) {
|
||||
TokenUtils.getToken(context, isCheck)
|
||||
.flatMap(new Func1<String, Observable<List<LibaoEntity>>>() {
|
||||
@Override
|
||||
public Observable<List<LibaoEntity>> call(String token) {
|
||||
return RetrofitManager.getLibao().getCunHaoXiang(token);
|
||||
}
|
||||
}).subscribeOn(Schedulers.io())
|
||||
public static void getCunHaoXiang(final Context context) {
|
||||
getLibao()
|
||||
.getCunHaoXiang(LoginUtils.getToken(context))
|
||||
.subscribeOn(Schedulers.io())
|
||||
.observeOn(AndroidSchedulers.mainThread())
|
||||
.subscribe(new Response<List<LibaoEntity>>() {
|
||||
@Override
|
||||
@ -104,26 +101,21 @@ public class LibaoUtils {
|
||||
|
||||
@Override
|
||||
public void onFailure(HttpException e) {
|
||||
if (e != null && e.code() == 401) {
|
||||
getCunHaoXiang(context, false);
|
||||
}
|
||||
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private static void postLibaoLing(final Context context, final String libaoId, final boolean isCheck,
|
||||
final PostLibaoListener listener, final String captchaCode) {
|
||||
TokenUtils.getToken(context, isCheck)
|
||||
.flatMap(new Func1<String, Observable<ResponseBody>>() {
|
||||
@Override
|
||||
public Observable<ResponseBody> call(String token) {
|
||||
if (!TextUtils.isEmpty(captchaCode)) {
|
||||
return RetrofitManager.getLibao().postLibaoLing(token, captchaCode, libaoId);
|
||||
} else {
|
||||
return RetrofitManager.getLibao().postLibaoLing(token, libaoId);
|
||||
}
|
||||
}
|
||||
}).subscribeOn(Schedulers.io())
|
||||
|
||||
Observable<ResponseBody> observable;
|
||||
if (!TextUtils.isEmpty(captchaCode)) {
|
||||
observable = getLibao().postLibaoLing(LoginUtils.getToken(context), captchaCode, libaoId);
|
||||
} else {
|
||||
observable = RetrofitManager.getLibao().postLibaoLing(LoginUtils.getToken(context), libaoId);
|
||||
}
|
||||
observable.subscribeOn(Schedulers.io())
|
||||
.observeOn(AndroidSchedulers.mainThread())
|
||||
.subscribe(new JSONObjectResponse() {
|
||||
@Override
|
||||
@ -133,10 +125,7 @@ public class LibaoUtils {
|
||||
|
||||
@Override
|
||||
public void onFailure(HttpException e) {
|
||||
if (e != null && e.code() == 401) {
|
||||
postLibaoLing(context, libaoId, false, listener, captchaCode);
|
||||
return;
|
||||
} else if (e != null && e.code() == 410) { // 该接口已废弃
|
||||
if (e != null && e.code() == 410) { // 该接口已废弃
|
||||
Utils.toast(context, "领取失败,请安装最新版本的光环助手");
|
||||
}
|
||||
listener.postFailed(e);
|
||||
@ -146,13 +135,8 @@ public class LibaoUtils {
|
||||
|
||||
private static void postLibaoTao(final Context context, final String libaoId, final boolean isCheck,
|
||||
final PostLibaoListener listener) {
|
||||
TokenUtils.getToken(context, isCheck)
|
||||
.flatMap(new Func1<String, Observable<ResponseBody>>() {
|
||||
@Override
|
||||
public Observable<ResponseBody> call(String token) {
|
||||
return RetrofitManager.getLibao().postLibaoTao(token, libaoId);
|
||||
}
|
||||
}).subscribeOn(Schedulers.io())
|
||||
getLibao().postLibaoTao(LoginUtils.getToken(context), libaoId)
|
||||
.subscribeOn(Schedulers.io())
|
||||
.observeOn(AndroidSchedulers.mainThread())
|
||||
.subscribe(new JSONObjectResponse() {
|
||||
@Override
|
||||
@ -162,10 +146,6 @@ public class LibaoUtils {
|
||||
|
||||
@Override
|
||||
public void onFailure(HttpException e) {
|
||||
if (e != null && e.code() == 401) {
|
||||
postLibaoTao(context, libaoId, false, listener);
|
||||
return;
|
||||
}
|
||||
listener.postFailed(e);
|
||||
}
|
||||
});
|
||||
@ -173,13 +153,8 @@ public class LibaoUtils {
|
||||
|
||||
public static void deleteLibaoCode(final Context context, final String code, final boolean isCheck,
|
||||
final PostLibaoListener listener) {
|
||||
TokenUtils.getToken(context, isCheck)
|
||||
.flatMap(new Func1<String, Observable<ResponseBody>>() {
|
||||
@Override
|
||||
public Observable<ResponseBody> call(String token) {
|
||||
return RetrofitManager.getLibao().deleteLibaoCode(token, code);
|
||||
}
|
||||
}).subscribeOn(Schedulers.io())
|
||||
getLibao().deleteLibaoCode(LoginUtils.getToken(context), code)
|
||||
.subscribeOn(Schedulers.io())
|
||||
.observeOn(AndroidSchedulers.mainThread())
|
||||
.subscribe(new Response<ResponseBody>() {
|
||||
@Override
|
||||
@ -189,17 +164,13 @@ public class LibaoUtils {
|
||||
|
||||
@Override
|
||||
public void onFailure(HttpException e) {
|
||||
if (e != null && e.code() == 401) {
|
||||
deleteLibaoCode(context, code, false, listener);
|
||||
return;
|
||||
}
|
||||
listener.postFailed(e);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public static void getLibaoStatus(String ids, final PostLibaoListener listener) {
|
||||
RetrofitManager.getLibao().getLibaoStatus(ids)
|
||||
getLibao().getLibaoStatus(ids)
|
||||
.subscribeOn(Schedulers.io())
|
||||
.observeOn(AndroidSchedulers.mainThread())
|
||||
.subscribe(new Response<List<LibaoStatusEntity>>() {
|
||||
@ -408,7 +379,7 @@ public class LibaoUtils {
|
||||
break;
|
||||
case "fetched":
|
||||
Utils.toast(context, "你已领过这个礼包了");
|
||||
getCunHaoXiang(context, true);
|
||||
getCunHaoXiang(context);
|
||||
|
||||
int[][] states2 = new int[2][];
|
||||
states2[0] = new int[]{android.R.attr.state_pressed};
|
||||
@ -545,7 +516,7 @@ public class LibaoUtils {
|
||||
if (countdown > 0 && countdown < 60 * 10) {
|
||||
EventBus.getDefault().post(new EBUISwitch(REFRESH_LIBAO_TIME, countdown));
|
||||
} else {
|
||||
getCunHaoXiang(context, true);
|
||||
getCunHaoXiang(context);
|
||||
}
|
||||
|
||||
int[][] states = new int[2][];
|
||||
|
||||
Reference in New Issue
Block a user