Skip to content

Commit 8e5501d

Browse files
committed
Server:getArray返回类型由JSONObject改为JSONArray;Client:JSONReponse新增支持key[]:[];与Server同步公共类;优化HttpManager,JSONReponse等日志;优化动态体验
1 parent 195f07f commit 8e5501d

10 files changed

Lines changed: 264 additions & 209 deletions

File tree

APIJSON(Android)/APIJSON(ADT)/APIJSONApp/APIJSONApp/src/apijson/demo/client/activity_fragment/MomentActivity.java

Lines changed: 59 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,7 @@ public void initView() {//必须调用
174174

175175
private MomentItem momentItem;
176176
/**
177+
* 内部转到UI线程
177178
* @param momentItem_
178179
*/
179180
private void setHead(MomentItem momentItem_) {
@@ -501,60 +502,72 @@ public void onDialogButtonClick(int requestCode, boolean isPositive) {
501502
private static final int HTTP_REPLY = 3;
502503
private static final int HTTP_DELETE = 4;
503504
@Override
504-
public void onHttpResponse(int requestCode, String resultJson, Exception e) {
505-
JSONResponse response = new JSONResponse(resultJson);
506-
if (requestCode <= 0) {
507-
if (requestCode == 0 && momentItem != null) {
508-
setHead(momentItem.setCommentCount(response.getTotal()));
509-
}
510-
super.onHttpResponse(requestCode, resultJson, e);
511-
return;
512-
}
505+
public void onHttpResponse(final int requestCode, final String resultJson, final Exception e) {
506+
runThread(TAG + "onHttpResponse", new Runnable() {
507+
508+
@Override
509+
public void run() {
513510

514-
if (requestCode == HTTP_GET_MOMENT) {
515-
MomentItem data = JSONResponse.toObject(response, MomentItem.class);
516-
if (data == null || data.getId() <= 0) {
517-
if (JSONResponse.isSucceed(response)) {
518-
showShortToast("动态不存在");
519-
super.finish();//需要动画,且不需要保存缓存
511+
JSONResponse response = new JSONResponse(resultJson);
512+
if (requestCode <= 0) {
513+
if (requestCode == 0 && momentItem != null) {
514+
setHead(momentItem.setCommentCount(response.getTotal()));
515+
}
516+
MomentActivity.super.onHttpResponse(requestCode, resultJson, e);
520517
return;
521518
}
522-
showShortToast("获取动态失败,请检查网络后重试");
523-
return;
524-
}
525-
setHead(data);
526-
return;
527-
}
528519

520+
if (requestCode == HTTP_GET_MOMENT) {
521+
MomentItem data = JSONResponse.toObject(response, MomentItem.class);
522+
if (data == null || data.getId() <= 0) {
523+
if (JSONResponse.isSucceed(response)) {
524+
showShortToast("动态不存在");
525+
MomentActivity.super.finish();//需要动画,且不需要保存缓存
526+
return;
527+
}
528+
showShortToast("获取动态失败,请检查网络后重试");
529+
return;
530+
}
531+
setHead(data);
532+
return;
533+
}
529534

530-
JSONResponse comment = response.getJSONResponse(Comment.class.getSimpleName());
531-
boolean succeed = JSONResponse.isSucceed(comment);
532-
String operation = "操作";
533-
switch (requestCode) {
534-
case HTTP_COMMENT: // 新增评论
535-
operation = "评论";
536-
break;
537-
case HTTP_REPLY:// 回复
538-
operation = "回复";
539-
break;
540-
case HTTP_DELETE:// 删除
541-
operation = "删除";
542-
if (succeed) {//MomentItem中仍然存有Comment,可重写saveCache,单独存里面的Moment和Comment等
543-
CacheManager.getInstance().remove(getCacheClass(), comment == null ? "0" : "" + comment.getId());
544-
}
545-
break;
546-
default:
547-
return;
548-
}
549535

550-
showShortToast(operation + (succeed ? "成功" : "失败,请检查网络后重试"));
551-
if (succeed) {
552-
etMomentInput.setText("");
536+
JSONResponse comment = response.getJSONResponse(Comment.class.getSimpleName());
537+
final boolean succeed = JSONResponse.isSucceed(comment);
538+
String operation = "操作";
539+
switch (requestCode) {
540+
case HTTP_COMMENT: // 新增评论
541+
operation = "评论";
542+
break;
543+
case HTTP_REPLY:// 回复
544+
operation = "回复";
545+
break;
546+
case HTTP_DELETE:// 删除
547+
operation = "删除";
548+
if (succeed) {//MomentItem中仍然存有Comment,可重写saveCache,单独存里面的Moment和Comment等
549+
CacheManager.getInstance().remove(getCacheClass(), comment == null ? "0" : "" + comment.getId());
550+
}
551+
break;
552+
default:
553+
return;
554+
}
555+
showShortToast(operation + (succeed ? "成功" : "失败,请检查网络后重试"));
553556

554-
loadHead = false;
555-
super.onRefresh();
556-
}
557+
runUiThread(new Runnable() {
558+
559+
@Override
560+
public void run() {
561+
if (succeed) {
562+
etMomentInput.setText("");
557563

564+
loadHead = false;
565+
MomentActivity.super.onRefresh();
566+
}
567+
}
568+
});
569+
}
570+
});
558571
}
559572

560573

APIJSON(Android)/APIJSON(ADT)/APIJSONApp/APIJSONApp/src/apijson/demo/client/manager/HttpManager.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ public void get(final String url_, final com.alibaba.fastjson.JSONObject request
102102
@Override
103103
protected Exception doInBackground(Void... params) {
104104
String body = request == null || request.isEmpty() ? null : JSON.toJSONString(request);
105-
Log.d(TAG, "post url_ = " + url_ + "\n request = \n" + body);
105+
Log.d(TAG, "\n\n<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<\n get url_ = " + url_ + "\n request = \n" + body);
106106
try {
107107
String url = StringUtil.getNoBlankString(url_)
108108
+ (body == null ? "" : URLEncoder.encode(StringUtil.getNoBlankString(body), UTF_8));
@@ -117,6 +117,7 @@ protected Exception doInBackground(Void... params) {
117117
result = getResponseJson(client, new Request.Builder()
118118
.addHeader(KEY_TOKEN, getToken(url))
119119
.url(sb.toString()).build());
120+
Log.d(TAG, "\n get result = \n" + result + "\n >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n\n");
120121
} catch (Exception e) {
121122
Log.e(TAG, "get AsyncTask.doInBackground try { result = getResponseJson(..." +
122123
"} catch (Exception e) {\n" + e.getMessage());
@@ -168,13 +169,14 @@ protected Exception doInBackground(Void... params) {
168169
return new Exception(TAG + ".post AsyncTask.doInBackground client == null >> return;");
169170
}
170171
String body = JSON.toJSONString(request);
171-
Log.d(TAG, "post url_ = " + url_ + "\n request = \n" + body);
172-
172+
Log.d(TAG, "\n\n<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<\n get post = " + url_ + "\n request = \n" + body);
173+
173174
RequestBody requestBody = RequestBody.create(TYPE_JSON, body);
174175

175176
result = getResponseJson(client, new Request.Builder()
176177
.addHeader(KEY_TOKEN, getToken(url)).url(StringUtil.getNoBlankString(url))
177178
.post(requestBody).build());
179+
Log.d(TAG, "\n post result = \n" + result + "\n >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n\n");
178180
} catch (Exception e) {
179181
Log.e(TAG, "post AsyncTask.doInBackground try { result = getResponseJson(..." +
180182
"} catch (Exception e) {\n" + e.getMessage());

APIJSON(Android)/APIJSON(ADT)/APIJSONApp/APIJSONApp/src/apijson/demo/client/model/MomentItem.java

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -120,15 +120,15 @@ public MomentItem setMoment(Moment moment) {
120120
public List<User> getUserList() {
121121
if (userList == null) {
122122
userList = new ArrayList<User>();
123-
List<Long> list = getPraiseUserIdList();
124-
if (list != null) {
125-
User u;
126-
for (Long id : list) {
127-
u = new User(id);
128-
u.setName("" + id);
129-
userList.add(u);
130-
}
131-
}
123+
// List<Long> list = getPraiseUserIdList();
124+
// if (list != null) {
125+
// User u;
126+
// for (Long id : list) {
127+
// u = new User(id);
128+
// u.setName("" + id);
129+
// userList.add(u);
130+
// }
131+
// }
132132
}
133133
return userList;
134134
}
@@ -168,7 +168,7 @@ public boolean getIsPraised(final long userId) {
168168
}
169169
public MomentItem setIsPraised(boolean isPraised) {
170170
this.isPraised = isPraised;
171-
171+
172172
User currentUser = APIJSONApplication.getInstance().getCurrentUser();
173173
long userId = currentUser == null ? 0 : currentUser.getId();
174174

@@ -198,6 +198,7 @@ public MomentItem setIsPraised(boolean isPraised) {
198198
}
199199
getMoment().setPraiseUserIdList(list);
200200

201+
setPraiseCount(praiseCount + (isPraised ? 1 : -1));
201202

202203
return this;
203204
}

0 commit comments

Comments
 (0)