去除volley

This commit is contained in:
huangzhuanghua
2016-12-05 18:43:58 +08:00
parent d02a136e2f
commit c7ceb5cf82
105 changed files with 1794 additions and 8141 deletions

View File

@ -22,15 +22,11 @@ import android.widget.RelativeLayout;
import android.widget.ScrollView;
import android.widget.TextView;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.DiskBasedCache;
import com.facebook.drawee.view.SimpleDraweeView;
import com.gh.base.AppController;
import com.gh.base.BaseActivity;
import com.gh.common.constant.Config;
import com.gh.common.util.DialogUtils;
import com.gh.common.util.GzipUtils;
import com.gh.common.util.PostCommentUtils;
import com.gh.common.util.TimestampUtils;
import com.gh.common.util.TokenUtils;
@ -42,23 +38,22 @@ import com.gh.gamecenter.entity.CommentEntity;
import com.gh.gamecenter.entity.ConcernEntity;
import com.gh.gamecenter.entity.ViewsEntity;
import com.gh.gamecenter.manager.CommentManager;
import com.gh.gamecenter.retrofit.OkHttpCache;
import com.gh.gamecenter.retrofit.Response;
import com.gh.gamecenter.retrofit.RetrofitManager;
import com.gh.gamecenter.volley.extended.JsonObjectExtendedRequest;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.File;
import java.lang.reflect.Type;
import java.util.List;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.OnTouch;
import retrofit2.adapter.rxjava.HttpException;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action1;
import rx.schedulers.Schedulers;
@ -205,13 +200,13 @@ public class MessageDetailActivity extends BaseActivity implements MessageDetail
}
private void getConcernDigest() {
JsonObjectExtendedRequest request = new JsonObjectExtendedRequest(Config.HOST + "article/"+ newsId +"/rich-digest",
new Response.Listener<JSONObject>() {
RetrofitManager.getApi().getNewsRichDigest(newsId)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Response<ConcernEntity>() {
@Override
public void onResponse(JSONObject response) {
Type listType = new TypeToken<ConcernEntity>() {}.getType();
Gson gson = new Gson();
mConcernEntity = gson.fromJson(response.toString(), listType);
public void onResponse(ConcernEntity response) {
mConcernEntity = response;
mConcernEntity.setCommentnum(commentNum);
adapter.addConcernEntity(mConcernEntity);
@ -223,14 +218,12 @@ public class MessageDetailActivity extends BaseActivity implements MessageDetail
setSoftInput(true);
}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
public void onFailure(Throwable e) {
showNoConnection(true);
}
});
AppController.addToRequestQueue(request);
}
private void getNewsViews() {
@ -241,7 +234,7 @@ public class MessageDetailActivity extends BaseActivity implements MessageDetail
.subscribe(new Action1<List<ViewsEntity>>() {
@Override
public void call(List<ViewsEntity> viewsEntities) {
if (viewsEntities.size() >0) {
if (viewsEntities.size() > 0) {
mConcernEntity.setViews(viewsEntities.get(0).getViews());
adapter.notifyItemChanged(0);
}
@ -316,20 +309,19 @@ public class MessageDetailActivity extends BaseActivity implements MessageDetail
} catch (JSONException e) {
e.printStackTrace();
}
if (newsId == null && mConcernEntity == null ||
newsId == null && mConcernEntity!= null && mConcernEntity.getId() == null) {
if (newsId == null || mConcernEntity == null || mConcernEntity.getId() == null) {
Utils.toast(this, "评论异常");
return;
} else if(newsId == null) {
} else if (newsId == null) {
newsId = mConcernEntity.getId();
}
PostCommentUtils.addCommentData(MessageDetailActivity.this,
Config.COMMENT_HOST + "article/" + newsId + "/comment", jsonObject.toString(),
PostCommentUtils.addCommentData(MessageDetailActivity.this, newsId, jsonObject.toString(),
new PostCommentUtils.PostCommentListener() {
@Override
public void postSucced(JSONObject response) {
mSendingDialog.dismiss();
Utils.toast(MessageDetailActivity.this, "发表成功");
toast("发表成功");
mMessageDetailEt.setText("");
setSoftInput(false);
@ -351,7 +343,7 @@ public class MessageDetailActivity extends BaseActivity implements MessageDetail
adapter.addNormalComment(commentEntity);
}
modifyNewsCommentVolleyCache(0, cacheObject, newsId);
modifyNewsCommentOkhttpCache(0, cacheObject, newsId);
} catch (JSONException e) {
e.printStackTrace();
}
@ -371,65 +363,58 @@ public class MessageDetailActivity extends BaseActivity implements MessageDetail
}
@Override
public void postFailed(VolleyError error) {
public void postFailed(Throwable e) {
mSendingDialog.dismiss();
if (error.networkResponse == null) {
Utils.toast(MessageDetailActivity.this, "提交失败,请检查网络设置");
Utils.log("评论错误返回为空======");
return;
}
String errorData = new String(error.networkResponse.data);
try {
JSONObject errorJson = new JSONObject(errorData);
String detail = errorJson.getString("detail");
if ("too frequent".equals(detail)) {
Utils.toast(MessageDetailActivity.this, "别话痨哦~休息一会再来评论吧~");
} else if ("user blocked".equals(detail)) {
Utils.toast(MessageDetailActivity.this, "账号状态异常,暂时无法发表评论");
} else if ("article blocked".equals(detail)) {
Utils.toast(MessageDetailActivity.this, "文章异常,无法发表评论");
setSoftInput(false);
} else if ("illegal".equals(detail)) {
Utils.toast(MessageDetailActivity.this, "评论内容可能包括敏感信息,请修改后再发表");
} else {
Utils.toast(MessageDetailActivity.this, "评论失败,未知原因");
if (e instanceof HttpException) {
HttpException exception = (HttpException) e;
if (exception.code() == 403) {
try {
JSONObject errorJson = new JSONObject(exception.response().errorBody().string());
String detail = errorJson.getString("detail");
if ("too frequent".equals(detail)) {
toast("别话痨哦~休息一会再来评论吧~");
} else if ("user blocked".equals(detail)) {
toast("账号状态异常,暂时无法发表评论");
} else if ("article blocked".equals(detail)) {
toast("文章异常,无法发表评论");
setSoftInput(false);
} else if ("illegal".equals(detail)) {
toast("评论内容可能包括敏感信息,请修改后再发表");
} else {
toast("评论失败,未知原因");
}
} catch (Exception ex) {
ex.printStackTrace();
toast("评论异常");
}
return;
}
} catch (JSONException e) {
Utils.toast(MessageDetailActivity.this, "评论异常");
e.printStackTrace();
}
toast("提交失败,请检查网络设置");
}
});
}
private static final String DEFAULT_CACHE_DIR = "volley";
private void modifyNewsCommentVolleyCache(int offset, JSONObject commentData, String id) {
File cacheDir = new File(getCacheDir(), DEFAULT_CACHE_DIR);
DiskBasedCache cache = new DiskBasedCache(cacheDir);
private void modifyNewsCommentOkhttpCache(int offset, JSONObject commentData, String id) {
String key = TimestampUtils.addTimestamp(Config.COMMENT_HOST + "article/" + id + "/comment?limit=10&offset=" + offset);
byte[] data = cache.getData(key);
byte[] data = OkHttpCache.getCache(key);
if (data != null) {
try {
JSONArray jsonArray = new JSONArray(new String(GzipUtils.decompressBytes(data)));
JSONArray jsonArray = new JSONArray(new String(data));
JSONArray newComment = new JSONArray();
newComment.put(commentData);
for (int i = 0, size = jsonArray.length() > 9 ? 9 : jsonArray.length(); i < size; i++) {
newComment.put(jsonArray.get(i));
}
Utils.log(newComment.toString());
cache.modify(key, GzipUtils.compressBytes(newComment.toString().getBytes()));
OkHttpCache.updateCache(key, newComment.toString().getBytes());
if (jsonArray.length() == 10) {
modifyNewsCommentVolleyCache(offset + 10, jsonArray.getJSONObject(9), id);
modifyNewsCommentOkhttpCache(offset + 10, jsonArray.getJSONObject(9), id);
}
} catch (JSONException e) {
e.printStackTrace();
}
} else {
Utils.log("modifyNewsCommentVolleyCache is null");
}
}