修改getToken逻辑

This commit is contained in:
huangzhuanghua
2016-12-29 17:50:12 +08:00
parent 750c73fef9
commit 94b4d730e3
15 changed files with 653 additions and 694 deletions

View File

@ -12,7 +12,9 @@ import okhttp3.MediaType;
import okhttp3.RequestBody;
import okhttp3.ResponseBody;
import retrofit2.adapter.rxjava.HttpException;
import rx.Observable;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Func1;
import rx.schedulers.Schedulers;
/**
@ -21,118 +23,110 @@ import rx.schedulers.Schedulers;
*/
public class PostCommentUtils {
public static void addCommentData(Context context, String newsId, String content, PostCommentListener listener) {
addCommentData(context, newsId, content, true, listener);
}
public static void addCommentData(final Context context, final String newsId, final String content,
final boolean isCheck, final PostCommentListener listener) {
new Thread(new Runnable() {
@Override
public void run() {
RequestBody body = RequestBody.create(MediaType.parse("application/json"), content);
RetrofitManager.getComment().postNewsComment(TokenUtils.getToken(context, isCheck), newsId, body)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new JSONObjectResponse() {
@Override
public void onResponse(JSONObject response) {
if (response.length() != 0) {
if (listener != null){
listener.postSucced(response);
}
} else {
Utils.toast(context, "提交失败,请检查网络设置");
}
}
@Override
public void onFailure(Throwable e) {
if (e instanceof HttpException) {
HttpException exception = (HttpException) e;
if (exception.code() == 401) {
addCommentData(context, newsId, content, false, listener);
return;
}
}
if (listener != null){
listener.postFailed(e);
}
}
});
}
}).start();
}
public static void addCommentVoto(final Context context, final String commentId, final PostCommentListener listener) {
addCommentVoto(context, commentId, true, listener);
}
public static void addCommentVoto(final Context context, final String commentId, final boolean isCheck
, final PostCommentListener listener) {
new Thread(new Runnable() {
@Override
public void run() {
RetrofitManager.getComment().postCommentVote(TokenUtils.getToken(context, isCheck), commentId)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Response<ResponseBody>() {
@Override
public void onResponse(ResponseBody response) {
if (listener != null) {
listener.postSucced(null);
}
}
@Override
public void onFailure(Throwable e) {
if (e instanceof HttpException) {
HttpException exception = (HttpException) e;
if (exception.code() == 401) {
addCommentVoto(context, commentId, false, listener);
return;
}
}
if (listener != null) {
listener.postFailed(e);
}
}
});
}
}).start();
}
public static void addReportData(Context context, String reportData, PostCommentListener listener) {
addReportData(context, reportData, listener, true);
}
private static void addReportData(final Context context, final String reportData,
final PostCommentListener listener, boolean isCheck) {
RequestBody body = RequestBody.create(MediaType.parse("application/json"), reportData);
RetrofitManager.getComment().postReportData(body,TokenUtils.getToken(context, isCheck))
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);
return RetrofitManager.getComment().postNewsComment(token, newsId, body);
}
})
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Response<ResponseBody>(){
.subscribe(new JSONObjectResponse() {
@Override
public void onNext(ResponseBody response) {
super.onNext(response);
public void onResponse(JSONObject response) {
if (response.length() != 0) {
if (listener != null){
listener.postSucced(response);
}
} else {
Utils.toast(context, "提交失败,请检查网络设置");
}
}
@Override
public void onFailure(Throwable e) {
if (e instanceof HttpException) {
HttpException exception = (HttpException) e;
if (exception.code() == 401) {
addCommentData(context, newsId, content, false, listener);
return;
}
}
if (listener != null){
listener.postFailed(e);
}
}
});
}
public static void addCommentVoto(final Context context, final String commentId, final boolean isCheck,
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);
}
})
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Response<ResponseBody>() {
@Override
public void onResponse(ResponseBody response) {
if (listener != null) {
listener.postSucced(null);
}
}
@Override
public void onFailure(Throwable e) {
if (e instanceof HttpException) {
HttpException exception = (HttpException) e;
if (exception.code() == 401) {
addCommentVoto(context, commentId, false, listener);
return;
}
}
if (listener != null) {
listener.postFailed(e);
}
}
});
}
public static void addReportData(final Context context, final String reportData, boolean isCheck,
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);
}
})
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Response<ResponseBody>() {
@Override
public void onResponse(ResponseBody response) {
listener.postSucced(null);
}
@Override
public void onError(Throwable e) {
super.onError(e);
public void onFailure(Throwable e) {
if (e instanceof HttpException) {
if (((HttpException) e).code() == 401) {
addReportData(context, reportData, listener, false);
addReportData(context, reportData, false, listener);
return;
}
}
Utils.log("addReportData=onError::" + e.toString());
listener.postFailed(e);
}
});
}
public interface PostCommentListener {