Skip to content

Commit 33f3fc6

Browse files
committed
Client:优化错误提示;Server:Controller中update方法改名为put
1 parent 02049fc commit 33f3fc6

4 files changed

Lines changed: 34 additions & 27 deletions

File tree

APIJSON(Android)/APIJSON(ADT)/APIJSONDemoApp/res/values-zh/strings.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,6 @@
1515
<string name="complex">复杂结构</string>
1616
<string name="access_error">没有访问权限</string>
1717
<string name="access_permitted">允许使用权限</string>
18-
<string name="query_error">可能出现了一些问题,可以按照以下步骤解决:\n\n1.检查网络连接\n\n2.检查url是否为一个可用的IPV4地址\n\n3.长按[查询]按钮用浏览器发送请求\n\n4.查看目标服务器上的log\n\n5.再试一次</string>
18+
<string name="query_error">可能出现了一些问题,可以按照以下步骤解决:\n\n1.检查网络连接\n\n2.检查url是否为一个可用的IPV4地址\n\n3.长按[ %1$s ]按钮用浏览器发送请求\n\n4.查看目标服务器上的log\n\n5.再试一次</string>
1919

2020
</resources>

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
<string name="complex">Complex</string>
1616
<string name="access_error">Access Error</string>
1717
<string name="access_permitted">Access Permitted</string>
18-
<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 [get] button to open the request by web browser\n\n4.Check logs outputed on the target server\n\n5.Try again</string>
18+
<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>
1919
<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>
2020
<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>
2121
<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>

APIJSON(Android)/APIJSON(ADT)/APIJSONDemoApp/src/apijson/demo/ui/QueryActivity.java

Lines changed: 31 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -87,15 +87,19 @@ public static Intent createIntent(Context context, int type, String url) {
8787
public static final int TYPE_ACCESS_PERMITTED = 16;
8888

8989

90-
private int type = TYPE_SINGLE;
91-
private String url;
9290

9391
private Activity context;
9492
private boolean isAlive;
9593

94+
private int type = TYPE_COMPLEX;
95+
private String url;
96+
private String error;
97+
9698
private TextView tvQueryResult;
9799
private ProgressBar pbQuery;
98100
private EditText etQueryUrl;
101+
private Button btnQueryQuery;
102+
99103
@Override
100104
protected void onCreate(Bundle savedInstanceState) {
101105
super.onCreate(savedInstanceState);
@@ -111,15 +115,18 @@ protected void onCreate(Bundle savedInstanceState) {
111115
tvQueryResult = (TextView) findViewById(R.id.tvQueryResult);
112116
pbQuery = (ProgressBar) findViewById(R.id.pbQuery);
113117
etQueryUrl = (EditText) findViewById(R.id.etQueryUrl);
118+
btnQueryQuery = (Button) findViewById(R.id.btnQueryQuery);
119+
114120

115121
etQueryUrl.setText(StringUtil.getString(StringUtil.isNotEmpty(url, true)
116122
? url : "http://139.196.140.118:8080/"));//TODO my server ipv4 address, edit it to your server url
123+
btnQueryQuery.setText(getMethod(type));
117124

118-
query();
119125

126+
error = String.format(getResources().getString(R.string.query_error), StringUtil.getTrimedString(btnQueryQuery));
127+
128+
query();
120129

121-
Button btnQueryQuery = (Button) findViewById(R.id.btnQueryQuery);
122-
btnQueryQuery.setText(getMethod(type));
123130

124131
btnQueryQuery.setOnClickListener(new OnClickListener() {
125132

@@ -144,14 +151,15 @@ public boolean onLongClick(View v) {
144151
private void query() {
145152
setRequest();
146153

147-
tvQueryResult.setText("requesting...\n\n url = " + url + "\n\n request = \n" + request
148-
+ "\n\n\n" + getResources().getString(R.string.query_error));
154+
final String fullUrl = getUrl(type);
155+
156+
tvQueryResult.setText("requesting...\n\n url = " + fullUrl + "\n\n request = \n" + request + "\n\n\n" + error);
149157
pbQuery.setVisibility(View.VISIBLE);
150158

151159
if (type < 10) {
152-
HttpManager.getInstance().post(getUrl(type), request, this);
160+
HttpManager.getInstance().post(fullUrl, request, this);
153161
} else {
154-
HttpManager.getInstance().get(getUrl(type), request, this);
162+
HttpManager.getInstance().get(fullUrl, request, this);
155163
}
156164
}
157165

@@ -175,12 +183,12 @@ public void openWebSite() {
175183
}
176184

177185

178-
179-
186+
187+
180188
private String getUrl(int type) {
181-
return url + getMethod(type) + "/";
189+
return url + StringUtil.getTrimedString(btnQueryQuery) + "/";
182190
}
183-
191+
184192
private String getMethod(int type) {
185193
switch (type) {
186194
case TYPE_POST:
@@ -193,7 +201,7 @@ private String getMethod(int type) {
193201
return "get";
194202
}
195203
}
196-
204+
197205
private String request;
198206
public void setRequest() {
199207
url = StringUtil.getNoBlankString(etQueryUrl);
@@ -247,15 +255,15 @@ public void onHttpResponse(int requestCode, final String resultJson, final Excep
247255
JSONArray array = JSONResponse.getJSONArray(response.getJSONObject("[]"));//, "Comment[]");//
248256
if (array == null || array.isEmpty()) {
249257
Log.e(TAG, "onHttpResponse type == TYPE_COMPLEX >> array == null || array.isEmpty() >> return;");
250-
return;
258+
} else {
259+
response = new JSONResponse(array.getJSONObject(0));
260+
261+
User user = JSONResponse.getObject(response, User.class);
262+
Log.d(TAG, "onHttpResponse type == TYPE_COMPLEX >> user = " + JSON.toJSONString(user));
263+
Work work = JSONResponse.getObject(response, Work.class);
264+
Log.d(TAG, "onHttpResponse type == TYPE_COMPLEX >> work = " + JSON.toJSONString(work));
265+
logList(JSONResponse.getList(response == null ? null : response.getJSONObject("Comment[]"), Comment.class));
251266
}
252-
response = new JSONResponse(array.getJSONObject(0));
253-
254-
User user = JSONResponse.getObject(response, User.class);
255-
Log.d(TAG, "onHttpResponse type == TYPE_COMPLEX >> user = " + JSON.toJSONString(user));
256-
Work work = JSONResponse.getObject(response, Work.class);
257-
Log.d(TAG, "onHttpResponse type == TYPE_COMPLEX >> work = " + JSON.toJSONString(work));
258-
logList(JSONResponse.getList(response == null ? null : response.getJSONObject("Comment[]"), Comment.class));
259267
} else if (type == TYPE_ACCESS_PERMITTED) {
260268
response = new JSONResponse(resultJson);
261269
Wallet wallet = JSONResponse.getObject(response, Wallet.class);
@@ -271,8 +279,7 @@ public void run() {
271279
Toast.makeText(context, "received result!", Toast.LENGTH_SHORT).show();
272280

273281
tvQueryResult.setText(e == null || JSON.isJsonCorrect(resultJson)
274-
? StringUtil.getTrimedString(resultJson)
275-
: e.getMessage() + "\n\n\n" + getResources().getString(R.string.query_error));
282+
? StringUtil.getTrimedString(resultJson) : e.getMessage() + "\n\n\n" + error);
276283

277284
}
278285
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ public String delete(@RequestBody String request) {
5959
}
6060

6161
@RequestMapping(value="put", method = RequestMethod.POST)
62-
public String update(@RequestBody String request) {
62+
public String put(@RequestBody String request) {
6363
System.out.println("put/request = " + request);
6464
JSONObject object = new RequestParser(RequestMethod.PUT).parse(request);
6565
System.out.println("put/return " + JSON.toJSONString(object));

0 commit comments

Comments
 (0)