98 lines
4.1 KiB
Java
98 lines
4.1 KiB
Java
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 org.json.JSONObject;
|
|
|
|
/**
|
|
* 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(final Context context, final String url, 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>() {
|
|
@Override
|
|
public void onResponse(JSONObject response) {
|
|
if (listener != null){
|
|
listener.postSucced(response);
|
|
}
|
|
}
|
|
}, new Response.ErrorListener() {
|
|
@Override
|
|
public void onErrorResponse(VolleyError error) {
|
|
if (error.networkResponse != null && error.networkResponse.statusCode == 401) {
|
|
addCommentData(context, url, content, false, listener);
|
|
return;
|
|
}
|
|
if (listener != null){
|
|
listener.postFailed(error);
|
|
}
|
|
}
|
|
});
|
|
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 newsId, 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>() {
|
|
@Override
|
|
public void onResponse(String response) {
|
|
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);
|
|
}
|
|
}
|
|
});
|
|
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);
|
|
}
|
|
|
|
}
|