Skip to content

Commit 3d6606d

Browse files
committed
完成指定非GET请求的JSON结构,避免不规范请求导致数据被乱改
1 parent fe0029b commit 3d6606d

6 files changed

Lines changed: 432 additions & 38 deletions

File tree

APIJSON(Android)/APIJSON(ADT)/APIJSONDemoApp/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(Android)/APIJSON(ADT)/APIJSONDemoApp/src/apijson/demo/RequestUtil.java

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,30 +31,34 @@
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
}
4243

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
}
4649

4750
public static JSONObject newPutRequest() {
48-
User data = new User(38710);
51+
User data = new User(38710);//);// 测试necessaryColumns = [id]通过
4952
data.setName("Lemon");
53+
//测试disallowColumns = [phone]通过 data.setPhone("1234567890");
5054

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");
5559
data.setPicture(JSON.toJSONString(list));
5660

57-
return new JSONRequest(data).setTag("put_user");
61+
return new JSONRequest(data).setTag(User.class.getSimpleName());
5862
}
5963

6064

APIJSON(Server)/APIJSON(Eclipse_JEE)/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(1);
394420
}
395-
return isPath(path) ? path.split(SEPARATOR) : new String[] {path};
421+
while (s.endsWith(split)) {
422+
s = s.substring(0, s.length() - 1);
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
}

APIJSON(Server)/APIJSON(Eclipse_JEE)/src/main/java/zuo/biao/apijson/server/QueryConfig.java

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
public class QueryConfig {
3333

3434
public static final String KEY_COLUMNS = "columns";
35-
35+
3636
private RequestMethod method;
3737
private String table;
3838
private String columns;
@@ -185,10 +185,10 @@ public static String getWhereString(Map<String, Object> where) {
185185
if (key == null) {
186186
continue;
187187
}
188-
whereString += (key + "=" + where.get(key) + " &");
188+
whereString += (key + "='" + where.get(key) + "' and ");
189189
}
190-
if (whereString.endsWith("&")) {
191-
whereString = whereString.substring(0, whereString.length() - 1);
190+
if (whereString.endsWith("and ")) {
191+
whereString = whereString.substring(0, whereString.length() - "and ".length());
192192
}
193193
if (whereString.trim().endsWith("where") == false) {
194194
return whereString;
@@ -224,7 +224,7 @@ public static String getSetString(Map<String, Object> where) {
224224
setString = setString.substring(0, setString.length() - 1);
225225
}
226226
if (setString.trim().endsWith("set") == false) {
227-
return setString + " where id=" + where.get("id");
227+
return setString + " where id='" + where.get("id") + "' ";
228228
}
229229
}
230230
return "";
@@ -247,13 +247,15 @@ public static synchronized QueryConfig getQueryConfig(RequestMethod method, Stri
247247
String valuesString = "";
248248
Collection<Object> valueCollection = request.values();
249249
Object[] values = valueCollection == null || valueCollection.isEmpty() ? null : valueCollection.toArray(new String[]{});
250-
for (int i = 0; i < values.length; i++) {
251-
valuesString += ((i > 0 ? "," : "") + "'" + values[i] + "'");
250+
if (values != null) {
251+
for (int i = 0; i < values.length; i++) {
252+
valuesString += ((i > 0 ? "," : "") + "'" + values[i] + "'");
253+
}
252254
}
253255
config.setValues("(" + valuesString + ")");
254256
} else {
255257
request.remove(KEY_COLUMNS);
256-
258+
257259
Map<String, Object> transferredRequest = new HashMap<String, Object>();
258260
for (String key : set) {
259261
if (JSON.parseObject(request.getString(key)) == null) {//非key-value
@@ -262,8 +264,8 @@ public static synchronized QueryConfig getQueryConfig(RequestMethod method, Stri
262264
}
263265
config.setWhere(transferredRequest);
264266
}
265-
266-
267+
268+
267269
if (StringUtil.isNotEmpty(columns, true)) {
268270
config.setColumns(columns);
269271
}

0 commit comments

Comments
 (0)