Android:完善 Java 和 Kotlin 测试解析 Response JSON 的代码

This commit is contained in:
TommyLemon 2019-05-26 02:10:37 +08:00
parent 83328c995c
commit f907fc2c79
3 changed files with 462 additions and 36 deletions

View File

@ -86,25 +86,25 @@ public class MainTabActivity extends BaseBottomTabActivity implements OnBottomDr
if (SettingUtil.isOnTestMode) {
showShortToast("测试服务器\n" + HttpRequest.URL_BASE);
HttpRequest.get(JSON.parseObject(JSON.toJSONString(TestRequestAndResponseJava.request()), zuo.biao.apijson.JSONObject.class), 0, new OnHttpResponseListener() {
@Override
public void onHttpResponse(int requestCode, String resultJson, Exception e) {
TestRequestAndResponseJava.response(resultJson);
TestRequestAndResponseJava.smartResponse(resultJson);
}
});
HttpRequest.get(JSON.parseObject(JSON.toJSONString(TestRequestAndResponseKt.request()), zuo.biao.apijson.JSONObject.class), 0, new OnHttpResponseListener() {
@Override
public void onHttpResponse(int requestCode, String resultJson, Exception e) {
TestRequestAndResponseKt.response(resultJson);
TestRequestAndResponseKt.smartResponse(resultJson);
}
});
}
HttpRequest.get(JSON.parseObject(JSON.toJSONString(TestRequestAndResponseJava.request()), zuo.biao.apijson.JSONObject.class), 0, new OnHttpResponseListener() {
@Override
public void onHttpResponse(int requestCode, String resultJson, Exception e) {
TestRequestAndResponseJava.response(resultJson);
}
});
HttpRequest.get(JSON.parseObject(JSON.toJSONString(TestRequestAndResponseKt.request()), zuo.biao.apijson.JSONObject.class), 0, new OnHttpResponseListener() {
@Override
public void onHttpResponse(int requestCode, String resultJson, Exception e) {
TestRequestAndResponseKt.response(resultJson);
}
});
}
@ -145,14 +145,14 @@ public class MainTabActivity extends BaseBottomTabActivity implements OnBottomDr
@Override
protected Fragment getFragment(int position) {
switch (position) {
case 1:
UserListFragment fragment = UserListFragment.createInstance();
fragment.setSearchType(EditTextInfoWindow.TYPE_NAME);
return fragment;
case 2:
return MineFragment.createInstance();
default:
return MomentListFragment.createInstance();
case 1:
UserListFragment fragment = UserListFragment.createInstance();
fragment.setSearchType(EditTextInfoWindow.TYPE_NAME);
return fragment;
case 2:
return MineFragment.createInstance();
default:
return MomentListFragment.createInstance();
}
}
@ -221,8 +221,8 @@ public class MainTabActivity extends BaseBottomTabActivity implements OnBottomDr
@Override
public void run() {
sendBroadcast(new Intent(ActionUtil.ACTION_USER_CHANGED)
.putExtra(INTENT_ID, APIJSONApplication.getInstance().getCurrentUserId())
.putExtra(ActionUtil.INTENT_USER, APIJSONApplication.getInstance().getCurrentUser()));
.putExtra(INTENT_ID, APIJSONApplication.getInstance().getCurrentUserId())
.putExtra(ActionUtil.INTENT_USER, APIJSONApplication.getInstance().getCurrentUser()));
}
});
}
@ -263,15 +263,15 @@ public class MainTabActivity extends BaseBottomTabActivity implements OnBottomDr
@Override
public boolean onKeyUp(int keyCode, KeyEvent event) {
switch(keyCode){
case KeyEvent.KEYCODE_BACK:
long secondTime = System.currentTimeMillis();
if(secondTime - firstTime > 2000){
showShortToast("再按一次退出");
firstTime = secondTime;
} else {//完全退出
sendBroadcast(new Intent(ACTION_EXIT_APP));
}
return true;
case KeyEvent.KEYCODE_BACK:
long secondTime = System.currentTimeMillis();
if(secondTime - firstTime > 2000){
showShortToast("再按一次退出");
firstTime = secondTime;
} else {//完全退出
sendBroadcast(new Intent(ACTION_EXIT_APP));
}
return true;
}
return super.onKeyUp(keyCode, event);

View File

@ -1,5 +1,9 @@
package apijson.demo.client.activity_fragment
import apijson.demo.client.model.Comment
import apijson.demo.client.model.CommentItem
import apijson.demo.client.model.Moment
import apijson.demo.client.model.User
import com.alibaba.fastjson.JSONArray
import com.alibaba.fastjson.JSONObject
import zuo.biao.apijson.JSON
@ -228,4 +232,194 @@ fun response(resultJson: String?) {
var msg = response.getString("msg")
println("response.msg = " + msg);
}
fun smartResponse(resultJson: String?) {
var response: JSONObject = JSON.parseObject(resultJson)
//[]<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
var list:JSONArray? = response.getJSONArray("[]")
if (list == null) {
list = JSONArray();
}
var item: JSONObject?
for (i in 0..list.size - 1) {
item = list.getJSONObject(i)
if (item == null) {
continue
}
println("\nitem = list[" + i + "] = \n" + item + "\n\n")
//TODO 你的代码
//Moment<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
var moment: Moment? = item.getObject("Moment", Moment::class.java)
if (moment == null) {
moment = Moment()
}
var id = moment.getId()
println("moment.id = " + id)
var userId = moment.getUserId()
println("moment.userId = " + userId)
var date = moment.getDate()
println("moment.date = " + date)
var content = moment.getContent()
println("moment.content = " + content)
//praiseUserIdList<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
var praiseUserIdList1:List<Long?>? = moment.getPraiseUserIdList()
if (praiseUserIdList1 == null) {
praiseUserIdList1 = ArrayList();
}
var item2: Long?
for (i1 in 0..praiseUserIdList1.size - 1) {
item2 = praiseUserIdList1.get(i1)
if (item2 == null) {
continue
}
println("\nitem1 = praiseUserIdList1[" + i1 + "] = \n" + item2 + "\n\n")
//TODO 你的代码
}//praiseUserIdList>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
//pictureList<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
var pictureList1:List<String?>? = moment.getPictureList()
if (pictureList1 == null) {
pictureList1 = ArrayList();
}
var item3: String?
for (i1 in 0..pictureList1.size - 1) {
item3 = pictureList1.get(i1)
if (item3 == null) {
continue
}
println("\nitem1 = pictureList1[" + i1 + "] = \n" + item3 + "\n\n")
//TODO 你的代码
}//pictureList>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
//Moment>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
//User:owner<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
var owner: User? = item.getObject("User:owner", User::class.java)
if (owner == null) {
owner = User()
}
var id2 = owner.getId()
println("owner.id = " + id2)
var name = owner.getName()
println("owner.name = " + name)
var head = owner.getHead()
println("owner.head = " + head)
//User:owner>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
//User:praiseUser[]<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
var praiseUserList1:List<User?>? = JSON.parseArray(item.getString("User:praiseUser[]"), User::class.java)
if (praiseUserList1 == null) {
praiseUserList1 = ArrayList();
}
var item34: User?
for (i1 in 0..praiseUserList1.size - 1) {
item34 = praiseUserList1.get(i1)
if (item34 == null) {
continue
}
println("\nitem1 = praiseUserList1[" + i1 + "] = \n" + item34 + "\n\n")
//TODO 你的代码
var id = item34.getId()
println("item1.id = " + id)
var name = item34.getName()
println("item1.name = " + name)
}//User:praiseUser[]>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
//CommentItem[]<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
var commentItemList1:List<CommentItem?>? = JSON.parseArray(item.getString("CommentItem[]"), CommentItem::class.java)
if (commentItemList1 == null) {
commentItemList1 = ArrayList();
}
var item1: CommentItem?
for (i1 in 0..commentItemList1.size - 1) {
item1 = commentItemList1.get(i1)
if (item1 == null) {
continue
}
println("\nitem1 = commentItemList1[" + i1 + "] = \n" + item1 + "\n\n")
//TODO 你的代码
//Comment<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
var comment: Comment? = item1.getComment()
if (comment == null) {
comment = Comment()
}
var id3 = comment.getId()
println("comment.id = " + id3)
var toId = comment.getToId()
println("comment.toId = " + toId)
var userId = comment.getUserId()
println("comment.userId = " + userId)
var momentId = comment.getMomentId()
println("comment.momentId = " + momentId)
var date = comment.getDate()
println("comment.date = " + date)
var content = comment.getContent()
println("comment.content = " + content)
//Comment>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
//User:publisher<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
var publisher: User? = item1.getUser()
if (publisher == null) {
publisher = User()
}
var id = publisher.getId()
println("publisher.id = " + id)
var name = publisher.getName()
println("publisher.name = " + name)
//User:publisher>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
}//CommentItem[]>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
}//[]>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
//emptyList<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
var emptyList:JSONArray? = response.getJSONArray("emptyList")
if (emptyList == null) {
emptyList = JSONArray();
}
var item5: Any?
for (i in 0..emptyList.size - 1) {
item5 = emptyList.get(i)
if (item5 == null) {
continue
}
println("\nitem = emptyList[" + i + "] = \n" + item5 + "\n\n")
//TODO 你的代码
}//emptyList>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
var code = response.getIntValue("code")
println("response.code = " + code);
var msg = response.getString("msg")
println("response.msg = " + msg);
}

View File

@ -3,9 +3,14 @@ package apijson.demo.client.activity_fragment;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import apijson.demo.client.model.Comment;
import apijson.demo.client.model.Moment;
import apijson.demo.client.model.User;
import zuo.biao.apijson.JSON;
public class TestRequestAndResponseJava {
@ -64,6 +69,233 @@ public class TestRequestAndResponseJava {
return request;
}
public static void smartResponse(String resultJson) {
JSONObject response = JSON.parseObject(resultJson);
{ //[]<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
JSONArray list = response.getJSONArray("[]");
if (list == null) {
list = new JSONArray();
}
JSONObject item;
for (int i = 0; i < list.size(); i ++) {
item = list.getJSONObject(i);
if (item == null) {
continue;
}
System.out.println("\nitem = list[" + i + "] = \n" + item + "\n\n");
//TODO 你的代码
{ //Moment<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
Moment moment = item.getObject("Moment", Moment.class);
if (moment == null) {
moment = new Moment();
}
long id = moment.getId();
System.out.println("moment.id = " + id);
long userId = moment.getUserId();
System.out.println("moment.userId = " + userId);
Long date = moment.getDate();
System.out.println("moment.date = " + date);
String content = moment.getContent();
System.out.println("moment.content = " + content);
{ //praiseUserIdList<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
List<Long> praiseUserIdList3 = moment.getPraiseUserIdList();
if (praiseUserIdList3 == null) {
praiseUserIdList3 = new ArrayList<>();
}
Long item3;
for (int i3 = 0; i3 < praiseUserIdList3.size(); i3 ++) {
item3 = praiseUserIdList3.get(i3);
if (item3 == null) {
continue;
}
System.out.println("\nitem3 = praiseUserIdList3[" + i3 + "] = \n" + item3 + "\n\n");
//TODO 你的代码
}
} //praiseUserIdList>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
{ //pictureList<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
List<String> pictureList3 = moment.getPictureList();
if (pictureList3 == null) {
pictureList3 = new ArrayList<>();
}
String item3;
for (int i3 = 0; i3 < pictureList3.size(); i3 ++) {
item3 = pictureList3.get(i3);
if (item3 == null) {
continue;
}
System.out.println("\nitem3 = pictureList3[" + i3 + "] = \n" + item3 + "\n\n");
//TODO 你的代码
}
} //pictureList>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
// { //obj<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
// JSONObject obj = moment.getObj();
// if (obj == null) {
// obj = new JSONObject();
// }
//
// int test = obj.getIntValue("test");
// System.out.println("obj.test = " + test);
// } //obj>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
//
//
// { //Comment<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
// Comment comment = moment.getComment();
// if (comment == null) {
// comment = new Comment();
// }
//
// long id = comment.getId();
// System.out.println("comment.id = " + id);
// long toId = comment.getToId();
// System.out.println("comment.toId = " + toId);
// long userId = comment.getUserId();
// System.out.println("comment.userId = " + userId);
// long momentId = comment.getMomentId();
// System.out.println("comment.momentId = " + momentId);
// String date = comment.getDate();
// System.out.println("comment.date = " + date);
// String content = comment.getContent();
// System.out.println("comment.content = " + content);
// } //Comment>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
} //Moment>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
{ //User<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
User user = item.getObject("User", User.class);
if (user == null) {
user = new User();
}
long id = user.getId();
System.out.println("user.id = " + id);
String name = user.getName();
System.out.println("user.name = " + name);
String head = user.getHead();
System.out.println("user.head = " + head);
} //User>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
{ //User[]<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
List<User> userList2 = JSON.parseArray(item.getString("User[]"), User.class);
if (userList2 == null) {
userList2 = new ArrayList<>();
}
User item2;
for (int i2 = 0; i2 < userList2.size(); i2 ++) {
item2 = userList2.get(i2);
if (item2 == null) {
continue;
}
System.out.println("\nitem2 = userList2[" + i2 + "] = \n" + item2 + "\n\n");
//TODO 你的代码
long id = item2.getId();
System.out.println("item2.id = " + id);
String name = item2.getName();
System.out.println("item2.name = " + name);
}
} //User[]>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
{ //[]<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
JSONArray list2 = item.getJSONArray("[]");
if (list2 == null) {
list2 = new JSONArray();
}
JSONObject item2;
for (int i2 = 0; i2 < list2.size(); i2 ++) {
item2 = list2.getJSONObject(i2);
if (item2 == null) {
continue;
}
System.out.println("\nitem2 = list2[" + i2 + "] = \n" + item2 + "\n\n");
//TODO 你的代码
{ //Comment<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
Comment comment = item2.getObject("Comment", Comment.class);
if (comment == null) {
comment = new Comment();
}
long id = comment.getId();
System.out.println("comment.id = " + id);
long toId = comment.getToId();
System.out.println("comment.toId = " + toId);
long userId = comment.getUserId();
System.out.println("comment.userId = " + userId);
long momentId = comment.getMomentId();
System.out.println("comment.momentId = " + momentId);
Long date = comment.getDate();
System.out.println("comment.date = " + date);
String content = comment.getContent();
System.out.println("comment.content = " + content);
} //Comment>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
{ //User<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
User user = item2.getObject("User", User.class);
if (user == null) {
user = new User();
}
long id = user.getId();
System.out.println("user.id = " + id);
String name = user.getName();
System.out.println("user.name = " + name);
} //User>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
}
} //[]>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
}
} //[]>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
{ //emptyList<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
JSONArray emptyList = response.getJSONArray("emptyList");
if (emptyList == null) {
emptyList = new JSONArray();
}
Object item;
for (int i = 0; i < emptyList.size(); i ++) {
item = emptyList.get(i);
if (item == null) {
continue;
}
System.out.println("\nitem = emptyList[" + i + "] = \n" + item + "\n\n");
//TODO 你的代码
}
} //emptyList>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
int code = response.getIntValue("code");
System.out.println("response.code = " + code);
String msg = response.getString("msg");
System.out.println("response.msg = " + msg);
}
public static void response(String resultJson) {
JSONObject response = JSON.parseObject(resultJson);