Skip to content

Commit 61719ca

Browse files
committed
新增JSONRequest简化请求json的拼接;优化注释
1 parent 3951ff2 commit 61719ca

File tree

19 files changed

+475
-177
lines changed

19 files changed

+475
-177
lines changed

APIJSON(Android)/APIJSON(ADT)/AndroidManifest.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
android:label="@string/app_name"
2020
android:theme="@style/AppTheme" >
2121
<activity
22-
android:name="zuo.biao.apijson.client.SelectActivity"
22+
android:name="zuo.biao.apijson.client.ui.SelectActivity"
2323
android:label="@string/app_name" >
2424
<intent-filter>
2525
<action android:name="android.intent.action.MAIN" />
@@ -28,7 +28,7 @@
2828
</intent-filter>
2929
</activity>
3030
<activity
31-
android:name="zuo.biao.apijson.client.QueryActivity"
31+
android:name="zuo.biao.apijson.client.ui.QueryActivity"
3232
android:label="Query" >
3333
</activity>
3434
</application>

APIJSON(Android)/APIJSON(ADT)/res/values/strings.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
<string name="hello_world">Hello world!</string>
77
<string name="action_settings">Settings</string>
88
<string name="demo_single">{\n&#160;&#160;&#160;\"User\":{\n&#160;&#160;&#160;&#160;&#160;&#160;\"id\":38710\n&#160;&#160;&#160;}\n}</string>
9-
<string name="demo_rely">{\n&#160;&#160;&#160;\"User\":{\n&#160;&#160;&#160;&#160;&#160;&#160;\"id\":38710\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>
9+
<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>
1010
<string name="demo_array">{\n&#160;&#160;&#160;\"User[]\":{\n&#160;&#160;&#160;&#160;&#160;&#160;\"count\":10,\n&#160;&#160;&#160;&#160;&#160;&#160;\"User\":{\n&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;\"sex\":0\n&#160;&#160;&#160;&#160;&#160;&#160;}\n&#160;&#160;&#160;}\n}</string>
1111
<string name="demo_complex">{\n&#160;&#160;&#160;\"[]\":{\n&#160;&#160;&#160;&#160;&#160;&#160;\"count\":5,\n&#160;&#160;&#160;&#160;&#160;&#160;\"User\":{\n&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;\"sex\":0\n&#160;&#160;&#160;&#160;&#160;&#160;},\n&#160;&#160;&#160;&#160;&#160;&#160;\"Work\":{\n&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;\"userId\":\"/User/id\"\n&#160;&#160;&#160;&#160;&#160;&#160;},\n&#160;&#160;&#160;&#160;&#160;&#160;\"Comment[]\":{\n&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;\"count\":3,\n&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;\"Comment\":{\n&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;\"workId\":\"[]/Work/id\"\n&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;}\n&#160;&#160;&#160;&#160;&#160;&#160;}\n&#160;&#160;&#160;}\n}</string>
1212

APIJSON(Android)/APIJSON(ADT)/src/zuo/biao/apijson/client/JSON.java renamed to APIJSON(Android)/APIJSON(ADT)/src/zuo/biao/apijson/JSON.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,12 @@
1212
See the License for the specific language governing permissions and
1313
limitations under the License.*/
1414

15-
package zuo.biao.apijson.client;
15+
package zuo.biao.apijson;
1616

1717
import java.util.List;
1818

19+
import zuo.biao.apijson.client.StringUtil;
20+
1921
import com.alibaba.fastjson.JSONObject;
2022
import com.alibaba.fastjson.parser.Feature;
2123
import com.alibaba.fastjson.serializer.SerializerFeature;

APIJSON(Android)/APIJSON(ADT)/src/zuo/biao/apijson/client/JSONObject.java renamed to APIJSON(Android)/APIJSON(ADT)/src/zuo/biao/apijson/JSONObject.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@
1212
See the License for the specific language governing permissions and
1313
limitations under the License.*/
1414

15-
package zuo.biao.apijson.client;
15+
package zuo.biao.apijson;
1616

17-
/**
17+
/**use this class instead of com.alibaba.fastjson.JSONObject
1818
* @author Lemon
1919
*/
2020
public class JSONObject extends com.alibaba.fastjson.JSONObject {

APIJSON(Android)/APIJSON(ADT)/src/zuo/biao/apijson/client/DemoApplication.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
import android.app.Application;
1818
import android.util.Log;
1919

20-
/**基础Application
20+
/**Application
2121
* @author Lemon
2222
* @see #init
2323
*/

APIJSON(Android)/APIJSON(ADT)/src/zuo/biao/apijson/client/HttpManager.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ public synchronized static HttpManager getInstance() {
8181

8282
public static final String KEY_TOKEN = "token";
8383
public static final String KEY_COOKIE = "cookie";
84-
protected static final String UTF_8 = "utf-8";
84+
public static final String UTF_8 = "utf-8";
8585

8686

8787
/**GET请求
Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
/*Copyright ©2015 TommyLemon(https://github.com/TommyLemon)
2+
3+
Licensed under the Apache License, Version 2.0 (the "License");
4+
you may not use this file except in compliance with the License.
5+
You may obtain a copy of the License at
6+
7+
http://www.apache.org/licenses/LICENSE-2.0
8+
9+
Unless required by applicable law or agreed to in writing, software
10+
distributed under the License is distributed on an "AS IS" BASIS,
11+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
See the License for the specific language governing permissions and
13+
limitations under the License.*/
14+
15+
package zuo.biao.apijson.client;
16+
17+
import static zuo.biao.apijson.client.HttpManager.UTF_8;
18+
19+
import java.io.UnsupportedEncodingException;
20+
import java.net.URLEncoder;
21+
import java.util.Set;
22+
23+
import zuo.biao.apijson.JSONObject;
24+
25+
/**auto formatted request JSONObject
26+
* @author Lemon
27+
* @use JSONRequest request = new JSONRequest(...);
28+
* <br> request.put(...);//not a must
29+
* <br> request.toArray(...);//not a must,you can use new JSONRequest(...).put("[]", request); instead
30+
* @see #toArray
31+
*/
32+
public class JSONRequest extends JSONObject {
33+
34+
private static final long serialVersionUID = -2223023180338466812L;
35+
36+
public JSONRequest() {
37+
super();
38+
}
39+
public JSONRequest(Object object) {
40+
this(null, object);
41+
}
42+
public JSONRequest(String name, Object object) {
43+
this();
44+
put(name, object);
45+
}
46+
47+
48+
@SuppressWarnings("unchecked")
49+
public <T> T get(String key) {
50+
return (T) super.get(key);
51+
}
52+
53+
/**put(value.getClass().getSimpleName(), value);
54+
* @param value
55+
* @return
56+
*/
57+
public Object put(Object value) {
58+
return put(null, value);
59+
}
60+
@Override
61+
public Object put(String key, Object value) {
62+
try {
63+
if (value instanceof String) {
64+
value = URLEncoder.encode((String) value, UTF_8);
65+
}
66+
return super.put(StringUtil.isNotEmpty(key, true) ? key : value.getClass().getSimpleName(), value);
67+
//just encode /, not need to encode [] ? URLEncoder.encode(key, UTF_8)
68+
} catch (UnsupportedEncodingException e) {
69+
e.printStackTrace();
70+
}
71+
return null;
72+
}
73+
74+
75+
/**put key-value in object into this
76+
* @param object
77+
*/
78+
public void add(JSONObject object) {
79+
Set<String> set = object == null ? null : object.keySet();
80+
if (set != null) {
81+
for (String key : set) {
82+
put(key, object.get(key));
83+
}
84+
}
85+
}
86+
87+
88+
89+
90+
public static final String KEY_COUNT = "count";
91+
public static final String KEY_PAGE = "page";
92+
93+
public JSONRequest setCount(Integer count) {
94+
put(KEY_COUNT, count);
95+
return this;
96+
}
97+
public JSONRequest setPage(Integer page) {
98+
put(KEY_PAGE, page);
99+
return this;
100+
}
101+
102+
/**create a parent JSONObject named []
103+
* @param count
104+
* @param page
105+
* @return {"[]":this}
106+
*/
107+
public JSONRequest toArray(int count, int page) {
108+
return toArray(count, page, null);
109+
}
110+
/**create a parent JSONObject named name+[]
111+
* @param count
112+
* @param page
113+
* @param name
114+
* @return {name+"[]" : this}
115+
*/
116+
public JSONRequest toArray(int count, int page, String name) {
117+
return new JSONRequest(StringUtil.getString(name) + "[]", this.setCount(count).setPage(page));
118+
}
119+
120+
}

APIJSON(Android)/APIJSON(ADT)/src/zuo/biao/apijson/client/RequestUtil.java

Lines changed: 19 additions & 138 deletions
Original file line numberDiff line numberDiff line change
@@ -14,160 +14,41 @@
1414

1515
package zuo.biao.apijson.client;
1616

17-
import static zuo.biao.apijson.client.HttpManager.UTF_8;
18-
19-
import java.io.UnsupportedEncodingException;
20-
import java.net.URLEncoder;
17+
import zuo.biao.apijson.JSONObject;
18+
import zuo.biao.apijson.client.model.Comment;
19+
import zuo.biao.apijson.client.model.User;
20+
import zuo.biao.apijson.client.model.Work;
2121

2222
/**
2323
* @author Lemon
2424
*/
2525
public class RequestUtil {
2626

27-
28-
// private static final String SEPRATOR = "/";
29-
30-
/**
31-
* @param object
32-
* @return
33-
*/
34-
public static String getString(JSONObject object) {
35-
try {
36-
return URLEncoder.encode(JSON.toJSONString(object), UTF_8);
37-
} catch (UnsupportedEncodingException e) {
38-
e.printStackTrace();
39-
}
40-
return null;
41-
}
42-
43-
/**
44-
* name = null, {"[]":object}
45-
* @param object
46-
* @return NonNull
47-
*/
48-
public static JSONObject getArrayRequest(JSONObject object) {
49-
return getArrayRequest(null, object);
50-
}
51-
/**
52-
* name = null, {"name+"[]"":object}
53-
* @param object
54-
* @return NonNull
55-
*/
56-
public static JSONObject getArrayRequest(String name, JSONObject object) {
57-
return getObjectRequest((name == null ? "" : name) + "[]", object);
58-
}
59-
60-
/**
61-
* @param object
62-
* @return
63-
*/
64-
public static <T> JSONObject getObjectRequest(T object) {
65-
return getObjectRequest(null, object);
66-
}
67-
/**
68-
* @param name
69-
* @param object
70-
* @return NonNull
71-
*/
72-
public static <T> JSONObject getObjectRequest(String name, T object) {
73-
JSONObject jsonObject = new JSONObject();
74-
if (object != null) {
75-
jsonObject.put(StringUtil.isNotEmpty(name, true) ? name : object.getClass().getSimpleName(), object);
76-
}
77-
return jsonObject;
78-
}
79-
80-
81-
/**
82-
* @param parent
83-
* @param child
84-
* @return
85-
*/
86-
public static <T> JSONObject put(JSONObject parent, T child) {
87-
if (parent == null) {
88-
parent = new JSONObject();
89-
}
90-
if (child != null) {
91-
parent.put(child.getClass().getSimpleName(), child);
92-
}
93-
return parent;
94-
}
95-
96-
97-
/**
98-
* @return
99-
*/
10027
public static JSONObject newSingleRequest() {
101-
return getObjectRequest(new User((long) 38710));
28+
return new JSONRequest(new User((long) 38710));
10229
}
103-
/**
104-
* @return
105-
*/
106-
public static JSONObject newRelyRequest() {
107-
JSONObject object = new JSONObject();
108-
109-
object.put(User.TAG, new User((long) 38710));
110-
111-
JSONObject workObject = new JSONObject();
112-
try {
113-
workObject.put("userId", URLEncoder.encode("User/id", UTF_8));//User.getFieldPath("id"));
114-
} catch (UnsupportedEncodingException e) {
115-
e.printStackTrace();
116-
return null;
117-
}
118-
object.put(Work.TAG, workObject);
11930

120-
return object;
31+
public static JSONObject newRelyRequest() {
32+
JSONRequest request = new JSONRequest();
33+
request.put(new User((long) 70793));
34+
request.put(Work.class.getSimpleName(), new JSONRequest("userId", "User/id"));
35+
return request;
12136
}
122-
/**
123-
* @return
124-
*/
37+
12538
public static JSONObject newArrayRequest() {
126-
JSONObject object = new JSONObject();
127-
object.put("count", 10);
128-
object = put(object, new User().setSex(0));
129-
return getArrayRequest(object);
39+
return new JSONRequest(new User().setSex(0)).toArray(10, 0, User.class.getSimpleName());
13040
}
131-
/**
132-
* @return
133-
*/
134-
public static JSONObject newComplexRequest() {
135-
JSONObject userObject = new JSONObject();
136-
userObject.put("sex", 0);
13741

138-
JSONObject workObject = new JSONObject();
139-
// workObject.put("userId", "/User/id");
140-
try {
141-
workObject.put("userId", URLEncoder.encode("/User/id", HttpManager.UTF_8));
142-
} catch (Exception e) {
143-
e.printStackTrace();
144-
}
145-
146-
JSONObject commentObject = new JSONObject();
147-
// commentObject.put("workId", "[]/Work/id");
148-
try {
149-
commentObject.put("workId", URLEncoder.encode("[]/Work/id", HttpManager.UTF_8));
150-
} catch (Exception e) {
151-
e.printStackTrace();
152-
}
42+
public static JSONObject newComplexRequest() {
15343

154-
JSONObject commentArrayObject = new JSONObject();
155-
commentArrayObject.put("page", 0);
156-
commentArrayObject.put("count", 3);
157-
commentArrayObject.put("Comment", commentObject);
44+
JSONRequest request = new JSONRequest();
45+
request.put(new User().setSex(0));
46+
request.put(Work.class.getSimpleName(), new JSONRequest("userId", "/User/id"));
15847

159-
JSONObject arrayObject = new JSONObject();
160-
arrayObject.put("page", 1);
161-
arrayObject.put("count", 10);
162-
arrayObject.put("User", userObject);
163-
arrayObject.put("Work", workObject);
164-
try {
165-
arrayObject.put(URLEncoder.encode("Comment[]", HttpManager.UTF_8), commentArrayObject);
166-
} catch (UnsupportedEncodingException e1) {
167-
e1.printStackTrace();
168-
}
48+
request.add(new JSONRequest(Comment.class.getSimpleName(), new JSONRequest("workId", "[]/Work/id")).
49+
toArray(3, 0, Comment.class.getSimpleName()));
16950

170-
return getArrayRequest(arrayObject);
51+
return request.toArray(10, 1);
17152
}
17253

17354
}

0 commit comments

Comments
 (0)