@@ -51,20 +51,20 @@ public class QueryActivity extends Activity implements OnHttpResponseListener {
5151
5252
5353 public static final String INTENT_TYPE = "INTENT_TYPE" ;
54- public static final String INTENT_URI = "INTENT_URI " ;
54+ public static final String INTENT_URL = "INTENT_URL " ;
5555
56- public static final String RESULT_URI = "RESULT_URI " ;
56+ public static final String RESULT_URL = "RESULT_URL " ;
5757
5858 /**
5959 * @param context
6060 * @param type
61- * @param uri
61+ * @param url
6262 * @return
6363 */
64- public static Intent createIntent (Context context , int type , String uri ) {
64+ public static Intent createIntent (Context context , int type , String url ) {
6565 return new Intent (context , QueryActivity .class )
6666 .putExtra (QueryActivity .INTENT_TYPE , type )
67- .putExtra (QueryActivity .INTENT_URI , uri );
67+ .putExtra (QueryActivity .INTENT_URL , url );
6868 }
6969
7070
@@ -75,7 +75,7 @@ public static Intent createIntent(Context context, int type, String uri) {
7575
7676
7777 private int type = TYPE_SINGLE ;
78- private String uri ;
78+ private String url ;
7979
8080 private Activity context ;
8181 private boolean isAlive ;
@@ -92,15 +92,15 @@ protected void onCreate(Bundle savedInstanceState) {
9292
9393 Intent intent = getIntent ();
9494 type = intent .getIntExtra (INTENT_TYPE , type );
95- uri = intent .getStringExtra (INTENT_URI );
95+ url = intent .getStringExtra (INTENT_URL );
9696
9797
9898 tvQueryResult = (TextView ) findViewById (R .id .tvQueryResult );
9999 pbQuery = (ProgressBar ) findViewById (R .id .pbQuery );
100100 etQueryUri = (EditText ) findViewById (R .id .etQueryUri );
101101
102- etQueryUri .setText (StringUtil .getString (StringUtil .isNotEmpty (uri , true )
103- ? uri : "http://192.168.1.104:8080/get/" ));
102+ etQueryUri .setText (StringUtil .getString (StringUtil .isNotEmpty (url , true )
103+ ? url : "http://192.168.1.104:8080/get/" ));//TODO my computer ipv4 address,you need to change it
104104
105105
106106 query ();
@@ -133,7 +133,7 @@ public void query(View v) {
133133
134134 private String request ;
135135 public void setRequest () {
136- uri = StringUtil .getNoBlankString (etQueryUri );
136+ url = StringUtil .getNoBlankString (etQueryUri );
137137 switch (type ) {
138138 case TYPE_SINGLE :
139139 request = JSON .toJSONString (RequestUtil .newSingleRequest ());
@@ -154,10 +154,11 @@ public void setRequest() {
154154 private void query () {
155155 setRequest ();
156156
157- tvQueryResult .setText ("requesting...\n \n uri = " + uri + "\n \n request = \n " + request );
157+ tvQueryResult .setText ("requesting...\n \n url = " + url + "\n \n request = \n " + request
158+ + "\n \n \n " + getResources ().getString (R .string .query_error ));
158159 pbQuery .setVisibility (View .VISIBLE );
159160
160- HttpManager .getInstance ().get (uri , request , this );
161+ HttpManager .getInstance ().get (url , request , this );
161162 }
162163
163164
@@ -171,29 +172,17 @@ public void onHttpResponse(int requestCode, final String resultJson, final Excep
171172 }
172173 JSONResponse response = new JSONResponse (resultJson );
173174 if (type == TYPE_ARRAY ) {
174- List <User > list = JSONResponse .parseList (response .getJSONObject ("User[]" ), User .class );
175- if (list == null || list .isEmpty ()) {
176- Log .e (TAG , "onHttpResponse type == TYPE_ARRAY >> list == null || list.isEmpty()" );
177- return ;
178- }
179- for (User user : list ) {
180- Log .d (TAG , "\n onHttpResponse type == TYPE_ARRAY >> user = \n " + JSON .toJSONString (user ));
181- }
175+ logList (JSONResponse .parseList (response .getJSONObject ("User[]" ), User .class ));
182176 } else if (type == TYPE_COMPLEX ) {
183177 JSONArray array = JSONResponse .toJSONArray (response .getJSONObject ("[]" ));//, "Comment[]");//
184178 if (array == null || array .isEmpty ()) {
185179 Log .e (TAG , "onHttpResponse type == TYPE_COMPLEX >> array == null || array.isEmpty() >> return;" );
186180 return ;
187181 }
188182 response = new JSONResponse (array .getJSONObject (0 ));
189- List <Comment > list = JSONResponse .parseList (response .getJSONObject ("Comment[]" ), Comment .class );//, Comment.class);//
190- if (list == null || list .isEmpty ()) {
191- Log .e (TAG , "onHttpResponse type == TYPE_COMPLEX >> list == null || list.isEmpty() >> return;" );
192- return ;
193- }
194- for (Comment comment : list ) {
195- Log .d (TAG , "\n onHttpResponse type == TYPE_COMPLEX >> comment = \n " + JSON .toJSONString (comment ));
196- }
183+
184+ logList (JSONResponse .parseList (response , User .class ));
185+ logList (JSONResponse .parseList (response == null ? null : response .getJSONObject ("Comment[]" ), Comment .class ));
197186 }
198187
199188 runOnUiThread (new Runnable () {
@@ -205,21 +194,35 @@ public void run() {
205194 Toast .makeText (context , "received result!" , Toast .LENGTH_SHORT ).show ();
206195
207196 tvQueryResult .setText (e == null || JSON .isJsonCorrect (resultJson )
208- ? StringUtil .getTrimedString (resultJson ) : e .getMessage ());
197+ ? StringUtil .getTrimedString (resultJson )
198+ : e .getMessage () + "\n \n \n " + getResources ().getString (R .string .query_error ));
209199
210200 }
211201 }
212202 });
213203 }
214204
215205
206+ private <T > void logList (List <T > list ) {
207+ if (list == null || list .isEmpty ()) {
208+ Log .e (TAG , "logList list == null || list.isEmpty() >> return;" );
209+ return ;
210+ }
211+ for (T data : list ) {
212+ Log .d (TAG , "\n logList " + (data == null ? "data" : data .getClass ().getSimpleName ())
213+ + " = \n " + JSON .toJSONString (data ));
214+ }
215+ }
216+
217+
218+
216219 /**打开网站
217220 */
218221 public void openWebSite () {
219222 setRequest ();
220223 String webSite = null ;
221224 try {
222- webSite = StringUtil .getNoBlankString (uri )
225+ webSite = StringUtil .getNoBlankString (url )
223226 + URLEncoder .encode (StringUtil .getNoBlankString (request ), HttpManager .UTF_8 );
224227 } catch (UnsupportedEncodingException e ) {
225228 e .printStackTrace ();
@@ -236,7 +239,7 @@ public void openWebSite() {
236239
237240 @ Override
238241 public void finish () {
239- setResult (RESULT_OK , new Intent ().putExtra (RESULT_URI , uri ));
242+ setResult (RESULT_OK , new Intent ().putExtra (RESULT_URL , url ));
240243 super .finish ();
241244 }
242245
0 commit comments