Skip to content

Commit de6ba39

Browse files
committed
Server:同步eclipse版至idea版;Client:同步adt版至studio版
1 parent 4fd4556 commit de6ba39

10 files changed

Lines changed: 372 additions & 73 deletions

File tree

APIJSON(Android)/APIJSON(AndroidStudio)/APIJSONLibrary/src/main/java/zuo/biao/apijson/JSON.java

Lines changed: 39 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@ public class JSON {
3434
public static boolean isJsonCorrect(String s) {
3535
System.out.println(TAG + "isJsonCorrect <<<< " + s + " >>>>>>>");
3636
if (s == null
37-
// || s.equals("[]")
38-
// || s.equals("{}")
37+
// || s.equals("[]")
38+
// || s.equals("{}")
3939
|| s.equals("")
4040
|| s.equals("[null]")
4141
|| s.equals("{null}")
@@ -55,13 +55,21 @@ public static String getCorrectJson(String s) {
5555
}
5656

5757
/**json转JSONObject
58-
* @param s
58+
* @param json
5959
* @return
6060
*/
6161
public static JSONObject parseObject(String json) {
62+
int features = com.alibaba.fastjson.JSON.DEFAULT_PARSER_FEATURE;
63+
features |= Feature.OrderedField.getMask();
64+
return parseObject(json, features);
65+
}
66+
/**json转JSONObject
67+
* @param json
68+
* @param features
69+
* @return
70+
*/
71+
public static JSONObject parseObject(String json, int features) {
6272
try {
63-
int features = com.alibaba.fastjson.JSON.DEFAULT_PARSER_FEATURE;
64-
features |= Feature.SortFeidFastMatch.getMask();
6573
return com.alibaba.fastjson.JSON.parseObject(getCorrectJson(json), JSONObject.class, features);
6674
} catch (Exception e) {
6775
System.out.println(TAG + "parseObject catch \n" + e.getMessage());
@@ -70,14 +78,14 @@ public static JSONObject parseObject(String json) {
7078
}
7179

7280
/**json转实体类
73-
* @param s
81+
* @param json
7482
* @param clazz
7583
* @return
7684
*/
7785
public static <T> T parseObject(String json, Class<T> clazz) {
7886
try {
7987
int features = com.alibaba.fastjson.JSON.DEFAULT_PARSER_FEATURE;
80-
features |= Feature.SortFeidFastMatch.getMask();
88+
features |= Feature.OrderedField.getMask();
8189
return com.alibaba.fastjson.JSON.parseObject(getCorrectJson(json), clazz, features);
8290
} catch (Exception e) {
8391
System.out.println(TAG + "parseObject catch \n" + e.getMessage());
@@ -93,7 +101,7 @@ public static JSONArray parseArray(String json) {
93101
return com.alibaba.fastjson.JSON.parseArray(json);
94102
}
95103
/**json转实体类列表
96-
* @param s
104+
* @param json
97105
* @param clazz
98106
* @return
99107
*/
@@ -112,12 +120,34 @@ public static <T> List<T> parseArray(String json, Class<T> clazz) {
112120
*/
113121
public static String toJSONString(Object obj) {
114122
try {
115-
return com.alibaba.fastjson.JSON.toJSONString(obj, SerializerFeature.SortField);
123+
return com.alibaba.fastjson.JSON.toJSONString(obj);
124+
} catch (Exception e) {
125+
System.out.println(TAG + "toJSONString catch \n" + e.getMessage());
126+
}
127+
return null;
128+
}
129+
130+
/**实体类转json
131+
* @param obj
132+
* @param features
133+
* @return
134+
*/
135+
public static String toJSONString(Object obj, SerializerFeature... features) {
136+
try {
137+
return com.alibaba.fastjson.JSON.toJSONString(obj, features);
116138
} catch (Exception e) {
117139
System.out.println(TAG + "toJSONString catch \n" + e.getMessage());
118140
}
119141
return null;
120142
}
121143

144+
/**格式化,显示更好看
145+
* @param json
146+
* @return
147+
*/
148+
public static String format(String json) {
149+
return toJSONString(parseObject(json), SerializerFeature.PrettyFormat);
150+
}
151+
122152

123153
}

APIJSON(Android)/APIJSON(AndroidStudio)/app/src/main/java/apijson/demo/RequestUtil.java

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -31,41 +31,45 @@
3131
public class RequestUtil {
3232

3333
public static JSONObject newPostRequest() {
34-
User data = new User();
35-
data.setName("Tommy");
34+
User data = new User();//10000);// 测试disallowColumns = [id]通过
35+
data.setName("Tommy");// 测试necessaryColumns = [name,phone]通过
3636
data.setSex(0);
37-
data.setPhone("1234567890");
37+
data.setPhone("1234567890");// 测试necessaryColumns = [name,phone]通过
38+
// data.setHead("http://common.cnblogs.com/images/icon_weibo_24.png");
3839
JSONRequest request = new JSONRequest();
3940
request.put(data);
40-
return request.setTag("post_user");
41+
return request.setTag(User.class.getSimpleName());//;// 测试必须指定tag通过
4142
}
42-
43+
4344
public static JSONObject newDeleteRequest() {
44-
return new JSONRequest(new User(93794)).setTag("delete_user");
45+
// 测试necessaryColumns = [id]通过
46+
// 测试对象不存在通过,存在返回success通过
47+
return new JSONRequest(new User(10000)).setTag(User.class.getSimpleName());//;// 测试必须指定tag通过
4548
}
46-
49+
4750
public static JSONObject newPutRequest() {
48-
User data = new User(38710);
51+
User data = new User(38710);//);// 测试necessaryColumns = [id]通过
4952
data.setName("Lemon");
50-
53+
//测试disallowColumns = [phone]通过 data.setPhone("1234567890");
54+
5155
List<String> list = new ArrayList<String>();
5256
list.add("http://static.oschina.net/uploads/user/1218/2437072_100.jpg?t=1461076033000");
5357
list.add("http://common.cnblogs.com/images/icon_weibo_24.png");
5458
list.add("http://static.oschina.net/uploads/user/585/1170143_50.jpg?t=1390226446000");
55-
data.setPicture(JSON.toJSONString(list));
56-
57-
return new JSONRequest(data).setTag("put_user");
59+
data.setPicture(JSON.toJSONString(list));//"\"" + JSON.toJSONString(new JSONObject("{\"id\":2}")) + "\"");//
60+
61+
return new JSONRequest(data).setTag(User.class.getSimpleName());
5862
}
5963

60-
61-
64+
65+
6266
public static JSONObject newSingleRequest() {
6367
return new JSONRequest(new User(38710));
6468
}
65-
69+
6670
public static JSONObject newColumnsRequest() {
6771
JSONObject object = new JSONObject(new User(38710));
68-
object.setColumns("id,name,phone");//StringUtil.getString(new String[]{"id", "name", "phone"}));//
72+
object.setColumns("id,name,phone");//测试排序通过 //StringUtil.getString(new String[]{"id", "name", "phone"}));//
6973
return new JSONRequest(User.class.getSimpleName(), object);
7074
}
7175

@@ -75,7 +79,7 @@ public static JSONObject newRelyRequest() {
7579
request.put(Work.class.getSimpleName(), new JSONRequest("userId", "User/id"));
7680
return request;
7781
}
78-
82+
7983
public static JSONObject newArrayRequest() {
8084
return new JSONRequest(new User()).toArray(5, 1, User.class.getSimpleName());
8185
}
@@ -94,7 +98,7 @@ public static JSONObject newComplexRequest() {
9498
public static JSONObject newAccessErrorRequest() {
9599
return new JSONRequest(new Wallet(38710));
96100
}
97-
101+
98102
public static JSONObject newAccessPermittedRequest() {
99103
JSONRequest request = new JSONRequest();
100104
request.put(new Wallet().setUserId((long) 38710));

APIJSON(Android)/APIJSON(AndroidStudio)/app/src/main/java/apijson/demo/ui/QueryActivity.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ private void query() {
153153

154154
final String fullUrl = getUrl(type);
155155

156-
tvQueryResult.setText("requesting...\n\n url = " + fullUrl + "\n\n request = \n" + request + "\n\n\n" + error);
156+
tvQueryResult.setText("requesting...\n\n url = " + fullUrl + "\n\n request = \n" + JSON.format(request) + "\n\n\n" + error);
157157
pbQuery.setVisibility(View.VISIBLE);
158158

159159
if (type < 10) {
@@ -282,8 +282,7 @@ public void run() {
282282
Toast.makeText(context, R.string.received_result, Toast.LENGTH_SHORT).show();
283283

284284
tvQueryResult.setText(e == null || JSON.isJsonCorrect(resultJson)
285-
? StringUtil.getTrimedString(resultJson) : e.getMessage() + "\n\n\n" + error);
286-
285+
? JSON.format(resultJson) : e.getMessage() + "\n\n\n" + error);
287286
}
288287
}
289288
});

APIJSON(Android)/APIJSON(AndroidStudio)/app/src/main/res/values/strings.xml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@
1818
<string name="received_result">received result!</string>
1919
<string name="browser_can_only_receive_get_response">Browsers can only receive GET responses</string>
2020
<string name="query_error">There may be something wrong,you can follow by the steps:\n\n1.Check your net connection\n\n2.Check the url whether it\'s an available ipv4 address\n\n3.Long click the [ %1$s ] button to open the request by web browser\n\n4.Check logs outputed on the target server\n\n5.Try again</string>
21-
<string name="demo_post">{\n&#160;&#160;&#160;\"tag\":\"post_user\",\n&#160;&#160;&#160;\"User\":{\n&#160;&#160;&#160;&#160;&#160;&#160;\"name\":\"Tommy\",\n&#160;&#160;&#160;&#160;&#160;&#160;\"sex\":0,\n&#160;&#160;&#160;&#160;&#160;&#160;\"phone\":\"1234567890\"\n&#160;&#160;&#160;}\n}</string>
22-
<string name="demo_delete">{\n&#160;&#160;&#160;\"tag\":\"delete_user\",\n&#160;&#160;&#160;\"User\":{\n&#160;&#160;&#160;&#160;&#160;&#160;\"id\":93794\n&#160;&#160;&#160;}\n}</string>
23-
<string name="demo_put">{\n&#160;&#160;&#160;\"tag\":\"put_user\",\n&#160;&#160;&#160;\"User\":{\n&#160;&#160;&#160;&#160;&#160;&#160;\"id\":38710,\n&#160;&#160;&#160;&#160;&#160;&#160;\"name\":\"Lemon\",\n&#160;&#160;&#160;&#160;&#160;&#160;\"picture\":\"[\"http://static.oschina.net/uploads/user/1218/2437072_100.jpg?t=1461076033000\", \"http://common.cnblogs.com/images/icon_weibo_24.png\", \"http://static.oschina.net/uploads/user/585/1170143_50.jpg?t=1390226446000\"]\"\n&#160;&#160;&#160;}\n}</string>
21+
<string name="demo_post">{\n&#160;&#160;&#160;\"tag\":\"User\",\n&#160;&#160;&#160;\"User\":{\n&#160;&#160;&#160;&#160;&#160;&#160;\"name\":\"Tommy\",\n&#160;&#160;&#160;&#160;&#160;&#160;\"sex\":0,\n&#160;&#160;&#160;&#160;&#160;&#160;\"phone\":\"1234567890\"\n&#160;&#160;&#160;}\n}</string>
22+
<string name="demo_delete">{\n&#160;&#160;&#160;\"tag\":\"User\",\n&#160;&#160;&#160;\"User\":{\n&#160;&#160;&#160;&#160;&#160;&#160;\"id\":10000\n&#160;&#160;&#160;}\n}</string>
23+
<string name="demo_put">{\n&#160;&#160;&#160;\"tag\":\"User\",\n&#160;&#160;&#160;\"User\":{\n&#160;&#160;&#160;&#160;&#160;&#160;\"id\":38710,\n&#160;&#160;&#160;&#160;&#160;&#160;\"name\":\"Lemon\",\n&#160;&#160;&#160;&#160;&#160;&#160;\"picture\":\"[\"http://static.oschina.net/uploads/user/1218/2437072_100.jpg?t=1461076033000\", \"http://common.cnblogs.com/images/icon_weibo_24.png\", \"http://static.oschina.net/uploads/user/585/1170143_50.jpg?t=1390226446000\"]\"\n&#160;&#160;&#160;}\n}</string>
2424
<string name="demo_single">{\n&#160;&#160;&#160;\"User\":{\n&#160;&#160;&#160;&#160;&#160;&#160;\"id\":38710\n&#160;&#160;&#160;}\n}</string>
2525
<string name="demo_columns">{\n&#160;&#160;&#160;\"User\":{\n&#160;&#160;&#160;&#160;&#160;&#160;\"columns\":\"id,name,phone\",\n&#160;&#160;&#160;&#160;&#160;&#160;\"id\":38710\n&#160;&#160;&#160;}\n}</string>
2626
<string name="demo_rely">{\n&#160;&#160;&#160;\"User\":{\n&#160;&#160;&#160;&#160;&#160;&#160;\"id\":70793\n&#160;&#160;&#160;},\n&#160;&#160;&#160;\"Work\":{\n&#160;&#160;&#160;&#160;&#160;&#160;\"userId\":\"User/id\"\n&#160;&#160;&#160;}\n}</string>

APIJSON(Server)/APIJSON(Idea)/src/main/java/zuo/biao/apijson/JSON.java

Lines changed: 38 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,13 @@
1414

1515
package zuo.biao.apijson;
1616

17+
import java.util.List;
18+
1719
import com.alibaba.fastjson.JSONArray;
1820
import com.alibaba.fastjson.JSONObject;
1921
import com.alibaba.fastjson.parser.Feature;
2022
import com.alibaba.fastjson.serializer.SerializerFeature;
2123

22-
import java.util.List;
23-
2424
/**阿里json封装类 防止解析时异常
2525
* @author Lemon
2626
*/
@@ -34,8 +34,8 @@ public class JSON {
3434
public static boolean isJsonCorrect(String s) {
3535
System.out.println(TAG + "isJsonCorrect <<<< " + s + " >>>>>>>");
3636
if (s == null
37-
// || s.equals("[]")
38-
// || s.equals("{}")
37+
// || s.equals("[]")
38+
// || s.equals("{}")
3939
|| s.equals("")
4040
|| s.equals("[null]")
4141
|| s.equals("{null}")
@@ -59,9 +59,17 @@ public static String getCorrectJson(String s) {
5959
* @return
6060
*/
6161
public static JSONObject parseObject(String json) {
62+
int features = com.alibaba.fastjson.JSON.DEFAULT_PARSER_FEATURE;
63+
features |= Feature.OrderedField.getMask();
64+
return parseObject(json, features);
65+
}
66+
/**json转JSONObject
67+
* @param json
68+
* @param features
69+
* @return
70+
*/
71+
public static JSONObject parseObject(String json, int features) {
6272
try {
63-
int features = com.alibaba.fastjson.JSON.DEFAULT_PARSER_FEATURE;
64-
features |= Feature.SortFeidFastMatch.getMask();
6573
return com.alibaba.fastjson.JSON.parseObject(getCorrectJson(json), JSONObject.class, features);
6674
} catch (Exception e) {
6775
System.out.println(TAG + "parseObject catch \n" + e.getMessage());
@@ -77,7 +85,7 @@ public static JSONObject parseObject(String json) {
7785
public static <T> T parseObject(String json, Class<T> clazz) {
7886
try {
7987
int features = com.alibaba.fastjson.JSON.DEFAULT_PARSER_FEATURE;
80-
features |= Feature.SortFeidFastMatch.getMask();
88+
features |= Feature.OrderedField.getMask();
8189
return com.alibaba.fastjson.JSON.parseObject(getCorrectJson(json), clazz, features);
8290
} catch (Exception e) {
8391
System.out.println(TAG + "parseObject catch \n" + e.getMessage());
@@ -112,12 +120,34 @@ public static <T> List<T> parseArray(String json, Class<T> clazz) {
112120
*/
113121
public static String toJSONString(Object obj) {
114122
try {
115-
return com.alibaba.fastjson.JSON.toJSONString(obj, SerializerFeature.SortField);
123+
return com.alibaba.fastjson.JSON.toJSONString(obj);
124+
} catch (Exception e) {
125+
System.out.println(TAG + "toJSONString catch \n" + e.getMessage());
126+
}
127+
return null;
128+
}
129+
130+
/**实体类转json
131+
* @param obj
132+
* @param features
133+
* @return
134+
*/
135+
public static String toJSONString(Object obj, SerializerFeature... features) {
136+
try {
137+
return com.alibaba.fastjson.JSON.toJSONString(obj, features);
116138
} catch (Exception e) {
117139
System.out.println(TAG + "toJSONString catch \n" + e.getMessage());
118140
}
119141
return null;
120142
}
121143

144+
/**格式化,显示更好看
145+
* @param json
146+
* @return
147+
*/
148+
public static String format(String json) {
149+
return toJSONString(parseObject(json), SerializerFeature.PrettyFormat);
150+
}
151+
122152

123153
}

APIJSON(Server)/APIJSON(Idea)/src/main/java/zuo/biao/apijson/StringUtil.java

Lines changed: 35 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -379,20 +379,49 @@ public static boolean isFilePathExist(String path) {
379379
}
380380

381381
public static final String SEPARATOR = "/";
382+
/**判断是否为路径
383+
* @param path
384+
* @return
385+
*/
382386
public static boolean isPath(String path) {
383387
return StringUtil.isNotEmpty(path, true) && path.contains(SEPARATOR)
384388
&& path.contains(SEPARATOR + SEPARATOR) == false && path.endsWith(SEPARATOR) == false;
385389
}
386390

391+
/**分割路径
392+
* @param path
393+
* @return
394+
*/
387395
public static String[] splitPath(String path) {
388-
path = getString(path);
389-
if(path.startsWith("/")){
390-
path = path.substring(1);
396+
return isPath(path) ? split(path, SEPARATOR) : new String[] {path};
397+
}
398+
/**将s分割成String[]
399+
* @param s
400+
* @return
401+
*/
402+
public static String[] split(String s) {
403+
return split(s, null);
404+
}
405+
/**将s用split分割成String[]
406+
* @param s
407+
* @param split
408+
* @return
409+
*/
410+
public static String[] split(String s, String split) {
411+
s = getString(s);
412+
if (s.isEmpty()) {
413+
return null;
414+
}
415+
if (isNotEmpty(split, false) == false) {
416+
split = ",";
391417
}
392-
if(path.endsWith("/")){
393-
path = path.substring(0, path.length() - 1);
418+
while (s.startsWith(split)) {
419+
s = s.substring(split.length());
394420
}
395-
return isPath(path) ? path.split(SEPARATOR) : new String[] {path};
421+
while (s.endsWith(split)) {
422+
s = s.substring(0, s.length() - split.length());
423+
}
424+
return s.contains(split) ? s.split(split) : new String[]{s};
396425
}
397426

398427

@@ -619,7 +648,6 @@ public static String getPrice(double price, int formatType) {
619648
}
620649
}
621650

622-
623651
//校正(自动补全等)字符串>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
624652

625653
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package zuo.biao.apijson;
2+
3+
public class Table {
4+
5+
public static final String ID = "id";
6+
public static final String NAME = "name";
7+
public static final String SEX = "sex";
8+
public static final String PHONE = "phone";
9+
public static final String HEAD = "head";
10+
public static final String PICTURE = "picture";
11+
public static final String PASSWORD = "password";
12+
public static final String TITLE = "title";
13+
public static final String CONTENT = "content";
14+
15+
}

0 commit comments

Comments
 (0)