Merge remote-tracking branch 'origin/2.3' into 2.3

# Conflicts:
#	app/src/main/java/com/gh/common/util/PostCommentUtils.java
#	app/src/main/java/com/gh/gamecenter/retrofit/ApiServiceImpl.java
#	app/src/main/java/com/gh/gamecenter/retrofit/CommentService.java
#	app/src/main/java/com/gh/gamecenter/retrofit/CommentServiceImpl.java
This commit is contained in:
khy
2016-12-06 17:18:18 +08:00
105 changed files with 1786 additions and 8151 deletions

View File

@ -15,16 +15,10 @@ import android.view.Window;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.DiskBasedCache;
import com.gh.base.AppController;
import com.gh.common.constant.Config;
import com.gh.common.util.ConcernContentUtils;
import com.gh.common.util.DataUtils;
import com.gh.common.util.DisplayUtils;
import com.gh.common.util.GzipUtils;
import com.gh.common.util.NewsUtils;
import com.gh.common.util.PostCommentUtils;
import com.gh.common.util.TimestampUtils;
@ -47,17 +41,15 @@ import com.gh.gamecenter.entity.CommentEntity;
import com.gh.gamecenter.entity.ConcernEntity;
import com.gh.gamecenter.manager.DataCollectionManager;
import com.gh.gamecenter.manager.VisitManager;
import com.gh.gamecenter.volley.extended.JsonArrayExtendedRequest;
import com.gh.gamecenter.volley.extended.JsonObjectExtendedRequest;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.gh.gamecenter.retrofit.JSONObjectResponse;
import com.gh.gamecenter.retrofit.OkHttpCache;
import com.gh.gamecenter.retrofit.Response;
import com.gh.gamecenter.retrofit.RetrofitManager;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.File;
import java.lang.reflect.Type;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
@ -69,6 +61,10 @@ import java.util.Map;
import java.util.Timer;
import java.util.TimerTask;
import retrofit2.adapter.rxjava.HttpException;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;
/**
* Created by khy on 2016/11/8.
* 消息详情-数据适配器
@ -139,71 +135,59 @@ public class MessageDetailAdapter extends RecyclerView.Adapter<RecyclerView.View
}
public void addHotComment(int offset) {
String hotCommentUrl = Config.COMMENT_HOST + "article/" + mConcernEntity.getId() +
"/comment?order=hot&limit=10&offset=" + offset;
JsonArrayExtendedRequest request = new JsonArrayExtendedRequest(hotCommentUrl,
new Response.Listener<JSONArray>() {
RetrofitManager.getComment().getHotComment(mConcernEntity.getId(), 10, offset)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Response<List<CommentEntity>>() {
@Override
public void onResponse(JSONArray response) {
Type listType = new TypeToken<ArrayList<CommentEntity>>() {}.getType();
Gson gson = new Gson();
List<CommentEntity> list = gson.fromJson(response.toString(), listType);
if (list.size() != 0) {
public void onResponse(List<CommentEntity> response) {
if (response.size() != 0) {
mHotCommentList.clear();
mHotCommentList.addAll(list);
mHotCommentList.addAll(response);
notifyDataSetChanged();
}
addNormalComment(mNormalCommentList.size()); // 考虑到断网刷新问题
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
public void onFailure(Throwable e) {
addNormalComment(mNormalCommentList.size());
}
});
AppController.addToRequestQueue(request);
}
public void addNormalComment(int offset) {
if (isLoading) {
return;
}
isLoading = true;
String commentUrl = Config.COMMENT_HOST + "article/" + mConcernEntity.getId() +
"/comment?limit=10&offset=" + offset;
JsonArrayExtendedRequest request = new JsonArrayExtendedRequest(commentUrl,
new Response.Listener<JSONArray>() {
RetrofitManager.getComment().getComment(mConcernEntity.getId(), 10, offset)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Response<List<CommentEntity>>() {
@Override
public void onResponse(JSONArray response) {
isLoading = false;
Type listType = new TypeToken<ArrayList<CommentEntity>>() {}.getType();
Gson gson = new Gson();
List<CommentEntity> list = gson.fromJson(response.toString(), listType);
if (list.size() < 10) {
public void onResponse(List<CommentEntity> response) {
if (response.size() < 10) {
isOver = true;
}
if (list.size() != 0) {
mNormalCommentList.addAll(list);
if (response.size() != 0) {
mNormalCommentList.addAll(response);
}
notifyDataSetChanged();
isLoading = false;
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
public void onFailure(Throwable e) {
isLoading = false;
isNetworkError = true;
notifyDataSetChanged();
}
});
AppController.addToRequestQueue(request);
}
@ -606,8 +590,7 @@ public class MessageDetailAdapter extends RecyclerView.Adapter<RecyclerView.View
}
@Override
public void postFailed(VolleyError error) {
public void postFailed(Throwable e) {
finalCommentEntity.setVote(finalCommentEntity.getVote() - 1);
holder.commentLikeCountTv.setTextColor(mContext.getResources().getColor(R.color.hint));
@ -618,12 +601,21 @@ public class MessageDetailAdapter extends RecyclerView.Adapter<RecyclerView.View
} else {
holder.commentLikeCountTv.setVisibility(View.VISIBLE);
}
if (error.networkResponse != null && new String(error.networkResponse.data).contains("voted")) {
Utils.toast(mContext, "已经点过赞啦!");
} else {
Utils.toast(mContext, "网络异常,点赞失败");
}
if (e instanceof HttpException) {
HttpException exception = (HttpException) e;
if (exception.code() == 403) {
try {
if (new JSONObject(exception.response().errorBody().string()).getString("detail").equals("voted")) {
Utils.toast(mContext, "已经点过赞啦!");
}
} catch (Exception ex) {
ex.printStackTrace();
}
return;
}
}
Utils.toast(mContext, "网络异常,点赞失败");
}
});
@ -724,42 +716,39 @@ public class MessageDetailAdapter extends RecyclerView.Adapter<RecyclerView.View
private void statNewsViews(final String news_id) {
String url = Config.DATA_HOST + "news/stat?news_id=" + news_id;
JsonObjectExtendedRequest request = new JsonObjectExtendedRequest(
Request.Method.POST, url,
new Response.Listener<JSONObject>() {
RetrofitManager.getData().postNewsViews(news_id)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new JSONObjectResponse() {
@Override
public void onResponse(JSONObject response) {
try {
if ("success".equals(response.getString("status"))) {
if (response.length() != 0) {
try {
if ("success".equals(response.getString("status"))) {
mConcernEntity.setViews(mConcernEntity.getViews() + 1);
notifyItemChanged(0);
mConcernEntity.setViews(mConcernEntity.getViews() + 1);
notifyItemChanged(0);
// 更新okhttp缓存数据
VisitManager.updateOkhttpCache(mConcernEntity.getId());
// 更新okhttp缓存数据
VisitManager.updateOkhttpCache(mConcernEntity.getId());
}
} catch (JSONException e) {
e.printStackTrace();
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}, null);
request.setShouldCache(false);
AppController.addToRequestQueue(request);
});
}
private static final String DEFAULT_CACHE_DIR = "volley";
private void modifyVolleyCache(String id, String url) {
if (url == null) {
return;
}
url = TimestampUtils.addTimestamp(url);
File cacheDir = new File(mContext.getCacheDir(), DEFAULT_CACHE_DIR);
DiskBasedCache cache = new DiskBasedCache(cacheDir);
byte[] data = cache.getData(url);
byte[] data = OkHttpCache.getCache(url);
if (data != null) {
try {
JSONArray jsonArray = new JSONArray(new String(GzipUtils.decompressBytes(data)));
JSONArray jsonArray = new JSONArray(new String(data));
JSONObject jsonObject;
for (int i = 0, size = jsonArray.length(); i < size; i++) {
jsonObject = jsonArray.getJSONObject(i);
@ -768,13 +757,10 @@ public class MessageDetailAdapter extends RecyclerView.Adapter<RecyclerView.View
break;
}
}
Utils.log(jsonArray.toString());
cache.modify(url, GzipUtils.compressBytes(jsonArray.toString().getBytes()));
OkHttpCache.updateCache(url, jsonArray.toString().getBytes());
} catch (JSONException e) {
e.printStackTrace();
}
} else {
Utils.log("modifyVolleyCache is null");
}
}