去除volley
This commit is contained in:
@ -2,96 +2,109 @@ package com.gh.common.util;
|
||||
|
||||
import android.content.Context;
|
||||
|
||||
import com.android.volley.Request;
|
||||
import com.android.volley.Response;
|
||||
import com.android.volley.VolleyError;
|
||||
import com.gh.base.AppController;
|
||||
import com.gh.common.constant.Config;
|
||||
import com.gh.gamecenter.volley.extended.JsonObjectExtendedRequest;
|
||||
import com.gh.gamecenter.volley.extended.StringExtendedRequest;
|
||||
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 url, String content, PostCommentListener listener) {
|
||||
addCommentData(context, url, content, true, listener);
|
||||
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 url, final String content,
|
||||
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() {
|
||||
JsonObjectExtendedRequest request = new JsonObjectExtendedRequest(
|
||||
Request.Method.POST, url, content,
|
||||
new Response.Listener<JSONObject>() {
|
||||
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 (listener != null){
|
||||
listener.postSucced(response);
|
||||
if (response.length() != 0) {
|
||||
if (listener != null){
|
||||
listener.postSucced(response);
|
||||
}
|
||||
} else {
|
||||
Utils.toast(context, "提交失败,请检查网络设置");
|
||||
}
|
||||
}
|
||||
}, new Response.ErrorListener() {
|
||||
|
||||
@Override
|
||||
public void onErrorResponse(VolleyError error) {
|
||||
if (error.networkResponse != null && error.networkResponse.statusCode == 401) {
|
||||
addCommentData(context, url, content, false, listener);
|
||||
return;
|
||||
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(error);
|
||||
listener.postFailed(e);
|
||||
}
|
||||
}
|
||||
});
|
||||
request.setShouldCache(false);
|
||||
request.addHeader("TOKEN", TokenUtils.getToken(context, isCheck));
|
||||
AppController.addToRequestQueue(request);
|
||||
}
|
||||
}).start();
|
||||
}
|
||||
|
||||
public static void addCommentVoto(final Context context, final String newsId, final PostCommentListener listener) {
|
||||
addCommentVoto(context, newsId, true, listener);
|
||||
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 newsId, final boolean isCheck
|
||||
public static void addCommentVoto(final Context context, final String commentId, final boolean isCheck
|
||||
, final PostCommentListener listener) {
|
||||
new Thread(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
StringExtendedRequest request = new StringExtendedRequest(
|
||||
Request.Method.POST, Config.COMMENT_HOST + "comment/" + newsId + "/vote",
|
||||
new Response.Listener<String>() {
|
||||
RetrofitManager.getComment().postCommentVote(TokenUtils.getToken(context, isCheck), commentId)
|
||||
.subscribeOn(Schedulers.io())
|
||||
.observeOn(AndroidSchedulers.mainThread())
|
||||
.subscribe(new Response<ResponseBody>() {
|
||||
@Override
|
||||
public void onResponse(String response) {
|
||||
listener.postSucced(null);
|
||||
public void onResponse(ResponseBody response) {
|
||||
if (listener != null) {
|
||||
listener.postSucced(null);
|
||||
}
|
||||
}
|
||||
},
|
||||
new Response.ErrorListener() {
|
||||
|
||||
@Override
|
||||
public void onErrorResponse(VolleyError error) {
|
||||
if (error.networkResponse != null && error.networkResponse.statusCode == 401) {
|
||||
addCommentVoto(context, newsId, false,listener);
|
||||
} else {
|
||||
listener.postFailed(error);
|
||||
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);
|
||||
}
|
||||
}
|
||||
});
|
||||
request.setShouldCache(false);
|
||||
request.addHeader("TOKEN", TokenUtils.getToken(context, isCheck));
|
||||
AppController.addToRequestQueue(request);
|
||||
}
|
||||
}).start();
|
||||
}
|
||||
|
||||
public interface PostCommentListener {
|
||||
void postSucced(JSONObject response);
|
||||
void postFailed(VolleyError error);
|
||||
void postFailed(Throwable error);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user