修改获取token逻辑,以及处理token 401问题

This commit is contained in:
kehaoyuan
2017-08-18 11:25:28 +08:00
parent 3b521ddd08
commit 043eebf013
26 changed files with 412 additions and 575 deletions

View File

@ -16,31 +16,26 @@ 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.getComment;
/**
* Created by khy on 2016/11/9.
*/
public class PostCommentUtils {
public static void addCommentData(final Context context, final String newsId, final String content,
final boolean isCheck, final CommentEntity commentEntity,
final CommentEntity commentEntity,
final PostCommentListener listener) {
TokenUtils.getToken(context, isCheck)
.flatMap(new Func1<String, Observable<ResponseBody>>() {
@Override
public Observable<ResponseBody> call(String token) {
RequestBody body = RequestBody.create(MediaType.parse("application/json"), content);
if (commentEntity != null) {
return RetrofitManager.getComment().postReplyComment(token, commentEntity.getId(), body);
} else {
return RetrofitManager.getComment().postNewsComment(token, newsId, body);
}
}
})
.subscribeOn(Schedulers.io())
RequestBody body = RequestBody.create(MediaType.parse("application/json"), content);
Observable<ResponseBody> observable;
if (commentEntity != null) {
observable = getComment().postReplyComment(LoginUtils.getToken(context), commentEntity.getId(), body);
} else {
observable = RetrofitManager.getComment().postNewsComment(LoginUtils.getToken(context), newsId, body);
}
observable.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new JSONObjectResponse() {
@Override
@ -54,18 +49,8 @@ public class PostCommentUtils {
}
}
@Override
public void onError(Throwable e) {
super.onError(e);
Utils.log("======" + e.toString());
}
@Override
public void onFailure(HttpException e) {
if (e != null && e.code() == 401) {
addCommentData(context, newsId, content, false, commentEntity, listener);
return;
}
if (listener != null) {
listener.postFailed(e);
}
@ -73,15 +58,10 @@ public class PostCommentUtils {
});
}
public static void addCommentVoto(final Context context, final String commentId, final boolean isCheck,
public static void addCommentVoto(final Context context, final String commentId,
final PostCommentListener listener) {
TokenUtils.getToken(context, isCheck)
.flatMap(new Func1<String, Observable<ResponseBody>>() {
@Override
public Observable<ResponseBody> call(String token) {
return RetrofitManager.getComment().postCommentVote(token, commentId);
}
})
getComment()
.postCommentVote(LoginUtils.getToken(context), commentId)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Response<ResponseBody>() {
@ -94,10 +74,6 @@ public class PostCommentUtils {
@Override
public void onFailure(HttpException e) {
if (e != null && e.code() == 401) {
addCommentVoto(context, commentId, false, listener);
return;
}
if (listener != null) {
listener.postFailed(e);
}
@ -105,16 +81,11 @@ public class PostCommentUtils {
});
}
public static void addReportData(final Context context, final String reportData, boolean isCheck,
public static void addReportData(final Context context, final String reportData,
final PostCommentListener listener) {
TokenUtils.getToken(context, isCheck)
.flatMap(new Func1<String, Observable<ResponseBody>>() {
@Override
public Observable<ResponseBody> call(String token) {
RequestBody body = RequestBody.create(MediaType.parse("application/json"), reportData);
return RetrofitManager.getComment().postReportData(body, token);
}
})
RequestBody body = RequestBody.create(MediaType.parse("application/json"), reportData);
getComment()
.postReportData(body, LoginUtils.getToken(context))
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Response<ResponseBody>() {
@ -125,10 +96,6 @@ public class PostCommentUtils {
@Override
public void onFailure(HttpException e) {
if (e != null && e.code() == 401) {
addReportData(context, reportData, false, listener);
return;
}
listener.postFailed(e);
}
});