Client:优化错误提示;Server:Controller中update方法改名为put

This commit is contained in:
TommyLemon 2017-01-08 17:19:54 +08:00
parent 02049fc569
commit 33f3fc64e7
4 changed files with 34 additions and 27 deletions

View File

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

View File

@ -15,7 +15,7 @@
<string name="complex">Complex</string> <string name="complex">Complex</string>
<string name="access_error">Access Error</string> <string name="access_error">Access Error</string>
<string name="access_permitted">Access Permitted</string> <string name="access_permitted">Access Permitted</string>
<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> <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>
<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> <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>
<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> <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>
<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> <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>

View File

@ -87,15 +87,19 @@ public class QueryActivity extends Activity implements OnHttpResponseListener {
public static final int TYPE_ACCESS_PERMITTED = 16; public static final int TYPE_ACCESS_PERMITTED = 16;
private int type = TYPE_SINGLE;
private String url;
private Activity context; private Activity context;
private boolean isAlive; private boolean isAlive;
private int type = TYPE_COMPLEX;
private String url;
private String error;
private TextView tvQueryResult; private TextView tvQueryResult;
private ProgressBar pbQuery; private ProgressBar pbQuery;
private EditText etQueryUrl; private EditText etQueryUrl;
private Button btnQueryQuery;
@Override @Override
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
@ -111,16 +115,19 @@ public class QueryActivity extends Activity implements OnHttpResponseListener {
tvQueryResult = (TextView) findViewById(R.id.tvQueryResult); tvQueryResult = (TextView) findViewById(R.id.tvQueryResult);
pbQuery = (ProgressBar) findViewById(R.id.pbQuery); pbQuery = (ProgressBar) findViewById(R.id.pbQuery);
etQueryUrl = (EditText) findViewById(R.id.etQueryUrl); etQueryUrl = (EditText) findViewById(R.id.etQueryUrl);
btnQueryQuery = (Button) findViewById(R.id.btnQueryQuery);
etQueryUrl.setText(StringUtil.getString(StringUtil.isNotEmpty(url, true) etQueryUrl.setText(StringUtil.getString(StringUtil.isNotEmpty(url, true)
? url : "http://139.196.140.118:8080/"));//TODO my server ipv4 address, edit it to your server url ? url : "http://139.196.140.118:8080/"));//TODO my server ipv4 address, edit it to your server url
btnQueryQuery.setText(getMethod(type));
error = String.format(getResources().getString(R.string.query_error), StringUtil.getTrimedString(btnQueryQuery));
query(); query();
Button btnQueryQuery = (Button) findViewById(R.id.btnQueryQuery);
btnQueryQuery.setText(getMethod(type));
btnQueryQuery.setOnClickListener(new OnClickListener() { btnQueryQuery.setOnClickListener(new OnClickListener() {
@Override @Override
@ -144,14 +151,15 @@ public class QueryActivity extends Activity implements OnHttpResponseListener {
private void query() { private void query() {
setRequest(); setRequest();
tvQueryResult.setText("requesting...\n\n url = " + url + "\n\n request = \n" + request final String fullUrl = getUrl(type);
+ "\n\n\n" + getResources().getString(R.string.query_error));
tvQueryResult.setText("requesting...\n\n url = " + fullUrl + "\n\n request = \n" + request + "\n\n\n" + error);
pbQuery.setVisibility(View.VISIBLE); pbQuery.setVisibility(View.VISIBLE);
if (type < 10) { if (type < 10) {
HttpManager.getInstance().post(getUrl(type), request, this); HttpManager.getInstance().post(fullUrl, request, this);
} else { } else {
HttpManager.getInstance().get(getUrl(type), request, this); HttpManager.getInstance().get(fullUrl, request, this);
} }
} }
@ -175,12 +183,12 @@ public class QueryActivity extends Activity implements OnHttpResponseListener {
} }
private String getUrl(int type) { private String getUrl(int type) {
return url + getMethod(type) + "/"; return url + StringUtil.getTrimedString(btnQueryQuery) + "/";
} }
private String getMethod(int type) { private String getMethod(int type) {
switch (type) { switch (type) {
case TYPE_POST: case TYPE_POST:
@ -193,7 +201,7 @@ public class QueryActivity extends Activity implements OnHttpResponseListener {
return "get"; return "get";
} }
} }
private String request; private String request;
public void setRequest() { public void setRequest() {
url = StringUtil.getNoBlankString(etQueryUrl); url = StringUtil.getNoBlankString(etQueryUrl);
@ -247,15 +255,15 @@ public class QueryActivity extends Activity implements OnHttpResponseListener {
JSONArray array = JSONResponse.getJSONArray(response.getJSONObject("[]"));//, "Comment[]");// JSONArray array = JSONResponse.getJSONArray(response.getJSONObject("[]"));//, "Comment[]");//
if (array == null || array.isEmpty()) { if (array == null || array.isEmpty()) {
Log.e(TAG, "onHttpResponse type == TYPE_COMPLEX >> array == null || array.isEmpty() >> return;"); Log.e(TAG, "onHttpResponse type == TYPE_COMPLEX >> array == null || array.isEmpty() >> return;");
return; } else {
} response = new JSONResponse(array.getJSONObject(0));
response = new JSONResponse(array.getJSONObject(0));
User user = JSONResponse.getObject(response, User.class); User user = JSONResponse.getObject(response, User.class);
Log.d(TAG, "onHttpResponse type == TYPE_COMPLEX >> user = " + JSON.toJSONString(user)); Log.d(TAG, "onHttpResponse type == TYPE_COMPLEX >> user = " + JSON.toJSONString(user));
Work work = JSONResponse.getObject(response, Work.class); Work work = JSONResponse.getObject(response, Work.class);
Log.d(TAG, "onHttpResponse type == TYPE_COMPLEX >> work = " + JSON.toJSONString(work)); Log.d(TAG, "onHttpResponse type == TYPE_COMPLEX >> work = " + JSON.toJSONString(work));
logList(JSONResponse.getList(response == null ? null : response.getJSONObject("Comment[]"), Comment.class)); logList(JSONResponse.getList(response == null ? null : response.getJSONObject("Comment[]"), Comment.class));
}
} else if (type == TYPE_ACCESS_PERMITTED) { } else if (type == TYPE_ACCESS_PERMITTED) {
response = new JSONResponse(resultJson); response = new JSONResponse(resultJson);
Wallet wallet = JSONResponse.getObject(response, Wallet.class); Wallet wallet = JSONResponse.getObject(response, Wallet.class);
@ -271,8 +279,7 @@ public class QueryActivity extends Activity implements OnHttpResponseListener {
Toast.makeText(context, "received result!", Toast.LENGTH_SHORT).show(); Toast.makeText(context, "received result!", Toast.LENGTH_SHORT).show();
tvQueryResult.setText(e == null || JSON.isJsonCorrect(resultJson) tvQueryResult.setText(e == null || JSON.isJsonCorrect(resultJson)
? StringUtil.getTrimedString(resultJson) ? StringUtil.getTrimedString(resultJson) : e.getMessage() + "\n\n\n" + error);
: e.getMessage() + "\n\n\n" + getResources().getString(R.string.query_error));
} }
} }

View File

@ -59,7 +59,7 @@ public class Controller {
} }
@RequestMapping(value="put", method = RequestMethod.POST) @RequestMapping(value="put", method = RequestMethod.POST)
public String update(@RequestBody String request) { public String put(@RequestBody String request) {
System.out.println("put/request = " + request); System.out.println("put/request = " + request);
JSONObject object = new RequestParser(RequestMethod.PUT).parse(request); JSONObject object = new RequestParser(RequestMethod.PUT).parse(request);
System.out.println("put/return " + JSON.toJSONString(object)); System.out.println("put/return " + JSON.toJSONString(object));