4747import apijson .demo .model .Work ;
4848
4949import 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
0 commit comments