Skip to content

Commit f80ccdc

Browse files
committed
Client:同步adt版至studio版
1 parent 08163b2 commit f80ccdc

File tree

6 files changed

+102
-66
lines changed

6 files changed

+102
-66
lines changed

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

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -41,41 +41,41 @@ public static JSONObject newPostRequest() {
4141
return request.setTag(User.class.getSimpleName());//;// 测试必须指定tag通过
4242
}
4343

44-
public static JSONObject newDeleteRequest() {
45-
// 测试necessaryColumns = [id]通过
46-
// 测试对象不存在通过,存在返回success通过
47-
return new JSONRequest(new User(10000)).setTag(User.class.getSimpleName());//;// 测试必须指定tag通过
48-
}
49-
50-
public static JSONObject newPutRequest() {
51-
User data = new User(38710);//);// 测试necessaryColumns = [id]通过
44+
public static JSONObject newPutRequest(long id) {
45+
User data = new User(id <= 0 ? 38710 : id);//);// 测试necessaryColumns = [id]通过
5246
data.setName("Lemon");
5347
//测试disallowColumns = [phone]通过 data.setPhone("1234567890");
5448

5549
List<String> list = new ArrayList<String>();
5650
list.add("http://static.oschina.net/uploads/user/1218/2437072_100.jpg?t=1461076033000");
5751
list.add("http://common.cnblogs.com/images/icon_weibo_24.png");
58-
list.add("http://static.oschina.net/uploads/user/585/1170143_50.jpg?t=1390226446000");
5952
data.setPicture(JSON.toJSONString(list));//"\"" + JSON.toJSONString(new JSONObject("{\"id\":2}")) + "\"");//
6053

6154
return new JSONRequest(data).setTag(User.class.getSimpleName());
6255
}
56+
57+
public static JSONObject newDeleteRequest(long id) {
58+
// 测试necessaryColumns = [id]通过
59+
// 测试对象不存在通过,存在返回success通过
60+
return new JSONRequest(new User(id <= 0 ? 10000 : id)).setTag(User.class.getSimpleName());//;// 测试必须指定tag通过
61+
}
6362

63+
64+
6465

65-
66-
public static JSONObject newSingleRequest() {
67-
return new JSONRequest(new User(38710));
66+
public static JSONObject newSingleRequest(long id) {
67+
return new JSONRequest(new User(id <= 0 ? 38710 : id));
6868
}
6969

70-
public static JSONObject newColumnsRequest() {
71-
JSONObject object = new JSONObject(new User(38710));
70+
public static JSONObject newColumnsRequest(long id) {
71+
JSONObject object = new JSONObject(new User(id <= 0 ? 38710 : id));
7272
object.setColumns("id,name,phone");//测试排序通过 //StringUtil.getString(new String[]{"id", "name", "phone"}));//
7373
return new JSONRequest(User.class.getSimpleName(), object);
7474
}
7575

76-
public static JSONObject newRelyRequest() {
76+
public static JSONObject newRelyRequest(long id) {
7777
JSONRequest request = new JSONRequest();
78-
request.put(new User(70793));
78+
request.put(new User(id <= 0 ? 70793 : id));
7979
request.put(Work.class.getSimpleName(), new JSONRequest("userId", "User/id"));
8080
return request;
8181
}
@@ -95,13 +95,13 @@ public static JSONObject newComplexRequest() {
9595
return request.toArray(2, 0);
9696
}
9797

98-
public static JSONObject newAccessErrorRequest() {
99-
return new JSONRequest(new Wallet(38710));
98+
public static JSONObject newAccessErrorRequest(long id) {
99+
return new JSONRequest(new Wallet().setUserId(id <= 0 ? 38710 : id));
100100
}
101101

102-
public static JSONObject newAccessPermittedRequest() {
102+
public static JSONObject newAccessPermittedRequest(long id) {
103103
JSONRequest request = new JSONRequest();
104-
request.put(new Wallet().setUserId((long) 38710));
104+
request.put(new Wallet().setUserId(id <= 0 ? 38710 : id));
105105
request.put("currentUserId", 38710);
106106
request.put("payPassword", "123456");
107107
return request;

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

Lines changed: 55 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
import apijson.demo.model.Work;
4848

4949
import com.alibaba.fastjson.JSONArray;
50+
import com.alibaba.fastjson.JSONObject;
5051

5152
/**activity for requesting a query in Server
5253
* @author Lemon
@@ -57,26 +58,30 @@ public class QueryActivity extends Activity implements OnHttpResponseListener {
5758

5859
public static final String INTENT_TYPE = "INTENT_TYPE";
5960
public static final String INTENT_URL = "INTENT_URL";
61+
public static final String INTENT_ID = "INTENT_ID";
6062

6163
public static final String RESULT_URL = "RESULT_URL";
64+
public static final String RESULT_ID = "RESULT_ID";
6265

6366

6467
/**
6568
* @param context
6669
* @param type
6770
* @param url
71+
* @param id
6872
* @return
6973
*/
70-
public static Intent createIntent(Context context, int type, String url) {
74+
public static Intent createIntent(Context context, int type, String url, long id) {
7175
return new Intent(context, QueryActivity.class)
7276
.putExtra(QueryActivity.INTENT_TYPE, type)
73-
.putExtra(QueryActivity.INTENT_URL, url);
77+
.putExtra(QueryActivity.INTENT_URL, url)
78+
.putExtra(QueryActivity.INTENT_ID, id);
7479
}
7580

7681

7782
public static final int TYPE_POST = 0;
78-
public static final int TYPE_DELETE = 1;
79-
public static final int TYPE_PUT = 2;
83+
public static final int TYPE_PUT = 1;
84+
public static final int TYPE_DELETE = 2;
8085

8186
public static final int TYPE_SINGLE = 10;
8287
public static final int TYPE_COLUMNS = 11;
@@ -93,13 +98,15 @@ public static Intent createIntent(Context context, int type, String url) {
9398

9499
private int type = TYPE_COMPLEX;
95100
private String url;
96-
private String error;
101+
private long id;
97102

98103
private TextView tvQueryResult;
99104
private ProgressBar pbQuery;
100105
private EditText etQueryUrl;
101106
private Button btnQueryQuery;
102107

108+
private String error;
109+
103110
@Override
104111
protected void onCreate(Bundle savedInstanceState) {
105112
super.onCreate(savedInstanceState);
@@ -110,23 +117,26 @@ protected void onCreate(Bundle savedInstanceState) {
110117
Intent intent = getIntent();
111118
type = intent.getIntExtra(INTENT_TYPE, type);
112119
url = intent.getStringExtra(INTENT_URL);
120+
id = intent.getLongExtra(INTENT_ID, id);
113121

114-
122+
123+
115124
tvQueryResult = (TextView) findViewById(R.id.tvQueryResult);
116125
pbQuery = (ProgressBar) findViewById(R.id.pbQuery);
117126
etQueryUrl = (EditText) findViewById(R.id.etQueryUrl);
118127
btnQueryQuery = (Button) findViewById(R.id.btnQueryQuery);
119128

120129

130+
121131
etQueryUrl.setText(StringUtil.getString(StringUtil.isNotEmpty(url, true)
122132
? url : "http://139.196.140.118:8080/"));//TODO my server ipv4 address, edit it to your server url
123133
btnQueryQuery.setText(getMethod(type));
124134

125-
126135
error = String.format(getResources().getString(R.string.query_error), StringUtil.getTrimedString(btnQueryQuery));
127136

128137
query();
129138

139+
130140

131141
btnQueryQuery.setOnClickListener(new OnClickListener() {
132142

@@ -143,6 +153,13 @@ public boolean onLongClick(View v) {
143153
return true;
144154
}
145155
});
156+
157+
158+
159+
if (id > 0) {
160+
Toast.makeText(context, String.format(getResources().getString(R.string.user_id_changed), "" + id)
161+
, Toast.LENGTH_LONG).show();
162+
}
146163
}
147164

148165

@@ -196,10 +213,10 @@ private String getMethod(int type) {
196213
switch (type) {
197214
case TYPE_POST:
198215
return "post";
199-
case TYPE_DELETE:
200-
return "delete";
201216
case TYPE_PUT:
202217
return "put";
218+
case TYPE_DELETE:
219+
return "delete";
203220
default:
204221
return "get";
205222
}
@@ -212,30 +229,30 @@ public void setRequest() {
212229
case TYPE_POST:
213230
request = JSON.toJSONString(RequestUtil.newPostRequest());
214231
break;
215-
case TYPE_DELETE:
216-
request = JSON.toJSONString(RequestUtil.newDeleteRequest());
217-
break;
218232
case TYPE_PUT:
219-
request = JSON.toJSONString(RequestUtil.newPutRequest());
233+
request = JSON.toJSONString(RequestUtil.newPutRequest(id));
234+
break;
235+
case TYPE_DELETE:
236+
request = JSON.toJSONString(RequestUtil.newDeleteRequest(id));
220237
break;
221238

222239
case TYPE_SINGLE:
223-
request = JSON.toJSONString(RequestUtil.newSingleRequest());
240+
request = JSON.toJSONString(RequestUtil.newSingleRequest(id));
224241
break;
225242
case TYPE_COLUMNS:
226-
request = JSON.toJSONString(RequestUtil.newColumnsRequest());
243+
request = JSON.toJSONString(RequestUtil.newColumnsRequest(id));
227244
break;
228245
case TYPE_RELY:
229-
request = JSON.toJSONString(RequestUtil.newRelyRequest());
246+
request = JSON.toJSONString(RequestUtil.newRelyRequest(id));
230247
break;
231248
case TYPE_ARRAY:
232249
request = JSON.toJSONString(RequestUtil.newArrayRequest());
233250
break;
234251
case TYPE_ACCESS_ERROR:
235-
request = JSON.toJSONString(RequestUtil.newAccessErrorRequest());
252+
request = JSON.toJSONString(RequestUtil.newAccessErrorRequest(id));
236253
break;
237254
case TYPE_ACCESS_PERMITTED:
238-
request = JSON.toJSONString(RequestUtil.newAccessPermittedRequest());
255+
request = JSON.toJSONString(RequestUtil.newAccessPermittedRequest(id));
239256
break;
240257
default:
241258
request = JSON.toJSONString(RequestUtil.newComplexRequest());
@@ -252,9 +269,22 @@ public void onHttpResponse(int requestCode, final String resultJson, final Excep
252269
Log.e(TAG, "onHttpResponse e = " + e.getMessage());
253270
}
254271
JSONResponse response = new JSONResponse(resultJson);
255-
if (type == TYPE_ARRAY) {
272+
switch (type) {
273+
case TYPE_POST:
274+
User postedUser = JSONResponse.getObject(response, User.class);
275+
id = postedUser == null ? 0 : postedUser.getId();
276+
Log.d(TAG, "onHttpResponse id = " + id);
277+
break;
278+
case TYPE_DELETE:
279+
JSONObject result = response.getJSONObject(User.class.getSimpleName());
280+
if (result != null && result.getIntValue("status") == 200) {//delete succeed
281+
id = 0;//reuse default value
282+
}
283+
break;
284+
case TYPE_ARRAY:
256285
logList(JSONResponse.getList(response.getJSONObject("User[]"), User.class));
257-
} else if (type == TYPE_COMPLEX) {
286+
break;
287+
case TYPE_COMPLEX:
258288
JSONArray array = JSONResponse.getJSONArray(response.getJSONObject("[]"));//, "Comment[]");//
259289
if (array == null || array.isEmpty()) {
260290
Log.e(TAG, "onHttpResponse type == TYPE_COMPLEX >> array == null || array.isEmpty() >> return;");
@@ -267,10 +297,13 @@ public void onHttpResponse(int requestCode, final String resultJson, final Excep
267297
Log.d(TAG, "onHttpResponse type == TYPE_COMPLEX >> work = " + JSON.toJSONString(work));
268298
logList(JSONResponse.getList(response == null ? null : response.getJSONObject("Comment[]"), Comment.class));
269299
}
270-
} else if (type == TYPE_ACCESS_PERMITTED) {
300+
break;
301+
case TYPE_ACCESS_PERMITTED:
271302
response = new JSONResponse(resultJson);
272303
Wallet wallet = JSONResponse.getObject(response, Wallet.class);
273304
Log.d(TAG, "onHttpResponse type == TYPE_ACCESS_PERMITTED >> wallet = " + JSON.toJSONString(wallet));
305+
default:
306+
break;
274307
}
275308

276309
runOnUiThread(new Runnable() {
@@ -304,7 +337,7 @@ private <T> void logList(List<T> list) {
304337

305338
@Override
306339
public void finish() {
307-
setResult(RESULT_OK, new Intent().putExtra(RESULT_URL, url));
340+
setResult(RESULT_OK, new Intent().putExtra(RESULT_URL, url).putExtra(RESULT_ID, id));
308341
super.finish();
309342
}
310343

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

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,14 @@
1414

1515
package apijson.demo.ui;
1616

17+
import zuo.biao.apijson.StringUtil;
1718
import android.app.Activity;
1819
import android.content.Intent;
1920
import android.net.Uri;
2021
import android.os.Bundle;
2122
import android.view.View;
2223
import android.widget.Toast;
23-
2424
import apijson.demo.R;
25-
import zuo.biao.apijson.StringUtil;
2625

2726
/**activity for selecting a request
2827
* @author Lemon
@@ -39,26 +38,26 @@ protected void onCreate(Bundle savedInstanceState) {
3938
context = this;
4039
}
4140

42-
43-
41+
42+
4443
//click event,called form layout android:onClick <<<<<<<<<<<<<<<<
4544
public void selectPost(View v) {
4645
select(QueryActivity.TYPE_POST);
4746
}
48-
49-
public void selectDelete(View v) {
50-
select(QueryActivity.TYPE_DELETE);
51-
}
52-
47+
5348
public void selectPut(View v) {
5449
select(QueryActivity.TYPE_PUT);
5550
}
51+
52+
public void selectDelete(View v) {
53+
select(QueryActivity.TYPE_DELETE);
54+
}
5655

5756
//get <<<<<<<<<<<<<<<<<<<<<<<<<<<
5857
public void selectSingle(View v) {
5958
select(QueryActivity.TYPE_SINGLE);
6059
}
61-
60+
6261
public void selectColumns(View v) {
6362
select(QueryActivity.TYPE_COLUMNS);
6463
}
@@ -70,7 +69,7 @@ public void selectRely(View v) {
7069
public void selectArray(View v) {
7170
select(QueryActivity.TYPE_ARRAY);
7271
}
73-
72+
7473
public void selectComplex(View v) {
7574
select(QueryActivity.TYPE_COMPLEX);
7675
}
@@ -84,21 +83,22 @@ public void selectAccessPermitted(View v) {
8483
}
8584
//get >>>>>>>>>>>>>>>>>>>>>>>>>>>
8685

87-
86+
8887
public void toUpdateLog(View v) {
8988
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(
9089
StringUtil.getCorrectUrl("github.com/TommyLemon/APIJSON/commits/master"))));
9190
}
92-
91+
9392
//click event,called form layout android:onClick >>>>>>>>>>>>>>>>
9493

9594
private String url;
95+
private long id;
9696
private void select(int type) {
97-
startActivityForResult(QueryActivity.createIntent(context, type, url), REQUEST_TO_QUERY);
97+
startActivityForResult(QueryActivity.createIntent(context, type, url, id), REQUEST_TO_QUERY);
9898
}
9999

100-
101-
100+
101+
102102
private static final int REQUEST_TO_QUERY = 1;
103103
@Override
104104
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
@@ -112,6 +112,7 @@ protected void onActivityResult(int requestCode, int resultCode, Intent data) {
112112
Toast.makeText(context, "onActivityResult data == null !!!", Toast.LENGTH_SHORT).show();
113113
} else {
114114
url = data.getStringExtra(QueryActivity.RESULT_URL);
115+
id = data.getLongExtra(QueryActivity.RESULT_ID, id);
115116
}
116117
break;
117118
default:

0 commit comments

Comments
 (0)