package com.gh.common.util; import android.content.Context; import com.gh.gamecenter.retrofit.JSONObjectResponse; import com.gh.gamecenter.retrofit.Response; import com.gh.gamecenter.retrofit.RetrofitManager; import org.json.JSONObject; import okhttp3.MediaType; import okhttp3.RequestBody; import okhttp3.ResponseBody; import retrofit2.adapter.rxjava.HttpException; import rx.android.schedulers.AndroidSchedulers; import rx.schedulers.Schedulers; /** * Created by khy on 2016/11/9. * */ 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() { @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 interface PostCommentListener { void postSucced(JSONObject response); void postFailed(Throwable error); } }