Skip to content

Commit 13a0f23

Browse files
committed
Client:RequestActivity新增encode;SelectActivity新增长按复制;StringUtil新增copyText;优化界面结构
1 parent 13c3ee4 commit 13a0f23

7 files changed

Lines changed: 162 additions & 74 deletions

File tree

APIJSON(Android)/APIJSON(ADT)/APIJSONTest/AndroidManifest.xml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,10 @@
2929
</activity>
3030
<activity
3131
android:name="apijson.demo.ui.RequestActivity"
32-
android:label="@string/request" >
33-
</activity>
32+
android:label="@string/request" />
33+
<activity
34+
android:name="apijson.demo.ui.AutoActivity"
35+
android:screenOrientation="landscape" />
3436
</application>
3537

3638
</manifest>

APIJSON(Android)/APIJSON(ADT)/APIJSONTest/res/layout/auto_activity.xml

Lines changed: 34 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,44 @@
99

1010
<LinearLayout
1111
style="@style/match_match"
12-
android:orientation="vertical" >
12+
android:orientation="horizontal" >
1313

1414
<EditText
15-
android:id="@+id/tvAutoResult"
16-
style="@style/match_match"
17-
android:hint="result"
15+
android:id="@+id/tvAutoRequest"
16+
android:layout_width="0dp"
17+
android:layout_height="wrap_content"
18+
android:layout_weight="1"
19+
android:background="@null"
20+
android:gravity="top|left"
21+
android:hint="request"
1822
android:padding="@dimen/activity_horizontal_margin" />
23+
24+
<View
25+
android:layout_width="1dp"
26+
android:layout_height="match_parent"
27+
android:layout_marginLeft="6dp"
28+
android:layout_marginRight="6dp"
29+
android:background="@android:color/white" />
30+
31+
<TextView
32+
android:id="@+id/tvAutoResponse"
33+
android:layout_width="0dp"
34+
android:layout_height="wrap_content"
35+
android:layout_weight="1"
36+
android:background="@null"
37+
android:gravity="top|left"
38+
android:hint="response"
39+
android:onClick="copy"
40+
android:padding="@dimen/activity_horizontal_margin"
41+
android:selectAllOnFocus="true" />
1942
</LinearLayout>
2043
</ScrollView>
2144

45+
<View
46+
android:layout_width="match_parent"
47+
android:layout_height="1dp"
48+
android:background="@android:color/white" />
49+
2250
<LinearLayout
2351
style="@style/match_wrap"
2452
android:layout_height="45dp"
@@ -42,14 +70,12 @@
4270
android:text="uri" />
4371

4472
<Button
45-
android:id="@+id/btnAutoCreate"
4673
android:layout_width="wrap_content"
4774
android:layout_height="match_parent"
48-
android:onClick="create"
49-
android:text="Create" />
75+
android:onClick="query"
76+
android:text="Query" />
5077

5178
<Button
52-
android:id="@+id/btnAutoAuto"
5379
android:layout_width="wrap_content"
5480
android:layout_height="match_parent"
5581
android:onClick="auto"

APIJSON(Android)/APIJSON(ADT)/APIJSONTest/res/layout/select_activity.xml

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,13 @@
9494
style="@style/select_json" />
9595

9696
<Button
97-
android:id="@+id/btnSelectUpdateLog"
97+
style="@style/match_wrap"
98+
android:layout_marginTop="30dp"
99+
android:onClick="toAuto"
100+
android:text="auto" />
101+
102+
<Button
103+
android:onClick="toUpdateLog"
98104
style="@style/match_wrap"
99105
android:layout_marginTop="30dp"
100106
android:text="@string/update_log" />

APIJSON(Android)/APIJSON(ADT)/APIJSONTest/src/apijson/demo/StringUtil.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
package apijson.demo;
22

3+
import android.content.ClipData;
4+
import android.content.ClipboardManager;
5+
import android.content.Context;
6+
import android.util.Log;
37
import android.widget.TextView;
8+
import android.widget.Toast;
49

510
/**String工具类
611
* @author Lemon
@@ -24,4 +29,18 @@ public static boolean isNotEmpty(TextView tv, boolean trim) {
2429
return isNotEmpty(getString(tv), trim);
2530
}
2631

32+
/**
33+
* @param value
34+
*/
35+
public static void copyText(Context context, String value) {
36+
if (context == null || StringUtil.isNotEmpty(value, true) == false) {
37+
Log.e("StringUtil", "copyText context == null || StringUtil.isNotEmpty(value, true) == false >> return;");
38+
return;
39+
}
40+
ClipData cD = ClipData.newPlainText("simple text", value);
41+
ClipboardManager clipboardManager = (ClipboardManager) context.getSystemService(Context.CLIPBOARD_SERVICE);
42+
clipboardManager.setPrimaryClip(cD);
43+
Toast.makeText(context, "已复制\n" + value, Toast.LENGTH_SHORT).show();
44+
}
45+
2746
}

APIJSON(Android)/APIJSON(ADT)/APIJSONTest/src/apijson/demo/ui/AutoActivity.java

Lines changed: 49 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,16 @@
1919
import zuo.biao.apijson.JSON;
2020
import zuo.biao.apijson.JSONRequest;
2121
import zuo.biao.apijson.JSONResponse;
22+
import android.annotation.SuppressLint;
2223
import android.app.Activity;
2324
import android.content.Context;
2425
import android.content.Intent;
2526
import android.os.Bundle;
2627
import android.util.Log;
2728
import android.view.View;
28-
import android.widget.EditText;
29+
import android.view.Window;
30+
import android.widget.TextView;
31+
import android.widget.Toast;
2932
import apijson.demo.R;
3033
import apijson.demo.StringUtil;
3134

@@ -47,72 +50,52 @@ public static Intent createIntent(Context context) {
4750
}
4851

4952

53+
private Activity context;
5054

51-
EditText tvAutoResult;
55+
private TextView tvAutoRequest;
56+
private TextView tvAutoResponse;
5257
@Override
5358
protected void onCreate(Bundle savedInstanceState) {
5459
super.onCreate(savedInstanceState);
60+
requestWindowFeature(Window.FEATURE_NO_TITLE);
5561
setContentView(R.layout.auto_activity);
62+
context = this;
5663

64+
tvAutoRequest = (TextView) findViewById(R.id.tvAutoRequest);
65+
tvAutoResponse = (TextView) findViewById(R.id.tvAutoResponse);
5766

58-
tvAutoResult = (EditText) findViewById(R.id.tvAutoResult);
5967

6068

69+
String request = "{\"User\":{\"id\":38710,\"@column\":\"id,name\"},\"[]\":{\"count\":3,\"page\":1,\"Moment\":{\"userId@\":\"User/id\"}}}";
6170

62-
String request = "{\"User\":{\"id\":1,\"@column\":\"id,name\"},\"[]\":{\"count\":3,\"page\":1,\"Moment\":{\"userId@\":\"User/id\"}}}";
63-
64-
tvAutoResult.setText(StringUtil.getString(JSON.format(request)));
71+
tvAutoRequest.setText(StringUtil.getString(JSON.format(request)));
6572

6673
}
6774

6875

69-
public void create(View v) {
70-
JSONRequest request = new JSONRequest();
71-
72-
//User<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
73-
JSONRequest userRequest = new JSONRequest();
74-
userRequest.put("id", 1);
75-
userRequest.put("@column", "id,name");
76-
77-
request.put("User", userRequest);
78-
//User>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
79-
80-
81-
//[]<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
82-
JSONRequest item = new JSONRequest();
83-
84-
//Moment<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
85-
JSONRequest momentRequest = new JSONRequest();
86-
momentRequest.put("userId@", "User/id");
87-
88-
item.put("Moment", momentRequest);
89-
//Moment>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
90-
91-
92-
request.add(item.toArray(0, 0));
93-
//[]>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
94-
95-
96-
String json = StringUtil.getString(JSON.format(request));
97-
Log.d(TAG, "\n<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<\n request = \n" + json + "\n>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
98-
99-
tvAutoResult.setText(json);
76+
public void query(View v) {
77+
startActivityForResult(RequestActivity.createIntent(context, 0, null, "get"
78+
, JSON.parseObject(StringUtil.getString(tvAutoRequest)), false), REQUEST_TO_REQUEST);
10079
}
10180

10281

10382

10483
public void auto(View v) {
105-
auto(StringUtil.getString(tvAutoResult));
84+
auto(StringUtil.getString(tvAutoRequest));
10685
}
10786
public void auto(String request) {
10887
String response = parse("", JSON.parseObject(request)); //newObjectRequest(request);
10988

11089
Log.d(TAG, "\n<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<\n request = \n" + request + "\n>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
11190
Log.d(TAG, "\n<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<\n response = \n" + response + "\n>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
11291

113-
tvAutoResult.setText(StringUtil.getString(response));
92+
tvAutoResponse.setText(StringUtil.getString(response));
11493
}
11594

95+
public void copy(View v) {
96+
StringUtil.copyText(context, StringUtil.getString(tvAutoResponse));
97+
}
98+
11699

117100
public static final String NEWLINE = "\n";
118101

@@ -220,6 +203,7 @@ private static String firstCase(String key) {
220203
* @param upper
221204
* @return
222205
*/
206+
@SuppressLint("DefaultLocale")
223207
private static String firstCase(String key, boolean upper) {
224208
key = StringUtil.getString(key);
225209
if (key.isEmpty()) {
@@ -233,14 +217,35 @@ private static String firstCase(String key, boolean upper) {
233217
}
234218

235219

236-
237-
private static String getSimpleName(String arrayKey) {
238-
return JSONResponse.getSimpleName(arrayKey);
239-
}
240-
241220
private static boolean isArrayKey(String key) {
242221
return JSONResponse.isArrayKey(key);
243222
}
223+
224+
225+
226+
227+
228+
229+
private static final int REQUEST_TO_REQUEST = 1;
230+
@Override
231+
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
232+
super.onActivityResult(requestCode, resultCode, data);
233+
if (resultCode != RESULT_OK) {
234+
return;
235+
}
236+
switch (requestCode) {
237+
case REQUEST_TO_REQUEST:
238+
if (data == null) {
239+
Toast.makeText(context, "onActivityResult data == null !!!", Toast.LENGTH_SHORT).show();
240+
} else {
241+
tvAutoResponse.setText(StringUtil.getString(JSON.format(
242+
data.getStringExtra(RequestActivity.RESULT_RESPONSE))));
243+
}
244+
break;
245+
default:
246+
break;
247+
}
248+
}
244249

245250

246251
}

APIJSON(Android)/APIJSON(ADT)/APIJSONTest/src/apijson/demo/ui/RequestActivity.java

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -56,24 +56,29 @@ public class RequestActivity extends Activity implements OnHttpResponseListener
5656
public static final String INTENT_URL = "INTENT_URL";
5757
public static final String INTENT_METHOD = "INTENT_METHOD";
5858
public static final String INTENT_REQUEST = "INTENT_REQUEST";
59+
public static final String INTENT_ENCODED = "INTENT_ENCODED";
5960

6061
public static final String RESULT_ID = "RESULT_ID";
6162
public static final String RESULT_URL = "RESULT_URL";
63+
public static final String RESULT_RESPONSE = "RESULT_RESPONSE";
6264

6365
/**
6466
* @param context
6567
* @param id
6668
* @param url
6769
* @param method
6870
* @param request
71+
* @param encoded
6972
* @return
7073
*/
71-
public static Intent createIntent(Context context, long id, String url, String method, JSONObject request) {
74+
public static Intent createIntent(Context context, long id, String url, String method,
75+
JSONObject request, boolean encoded) {
7276
return new Intent(context, RequestActivity.class)
7377
.putExtra(RequestActivity.INTENT_ID, id)
7478
.putExtra(RequestActivity.INTENT_URL, url)
7579
.putExtra(RequestActivity.INTENT_METHOD, method)
76-
.putExtra(RequestActivity.INTENT_REQUEST, JSON.toJSONString(request));
80+
.putExtra(RequestActivity.INTENT_REQUEST, JSON.toJSONString(request))
81+
.putExtra(RequestActivity.INTENT_ENCODED, encoded);
7782
}
7883

7984

@@ -87,6 +92,7 @@ public static Intent createIntent(Context context, long id, String url, String m
8792
private String url;
8893
private String method;
8994
private String request;
95+
private boolean encoded;
9096

9197
private TextView tvRequestResult;
9298
private ProgressBar pbRequest;
@@ -107,9 +113,17 @@ protected void onCreate(Bundle savedInstanceState) {
107113
url = getIntent().getStringExtra(INTENT_URL);
108114
method = getIntent().getStringExtra(INTENT_METHOD);
109115
request = getIntent().getStringExtra(INTENT_REQUEST);
116+
encoded = getIntent().getBooleanExtra(INTENT_ENCODED, false);
110117

111118
method = StringUtil.getTrimedString(method);
112119
url = StringUtil.getCorrectUrl(url);
120+
if (encoded == false) {
121+
try {
122+
request = URLEncoder.encode(request, StringUtil.UTF_8);
123+
} catch (UnsupportedEncodingException e) {
124+
e.printStackTrace();
125+
}
126+
}
113127

114128

115129
tvRequestResult = (TextView) findViewById(R.id.tvRequestResult);
@@ -201,11 +215,14 @@ public void setRequest() {
201215
}
202216

203217

204-
218+
219+
private String resultJson;
220+
205221
/*Http请求响应回调函数,返回数据在这里处理
206222
*/
207223
@Override
208224
public void onHttpResponse(int requestCode, final String resultJson, final Exception e) {
225+
this.resultJson = resultJson;
209226
Log.d(TAG, "onHttpResponse resultJson = " + resultJson);
210227
if (e != null) {
211228
Log.e(TAG, "onHttpResponse e = " + e.getMessage());
@@ -223,9 +240,9 @@ public void onHttpResponse(int requestCode, final String resultJson, final Excep
223240

224241
} else if ("delete".equals(method)) {
225242
response = response.getJSONResponse(Moment.class.getSimpleName());
226-
// if (JSONResponse.isSucceed(response)) {//delete succeed
227-
id = 0;//reuse default value
228-
// }
243+
// if (JSONResponse.isSucceed(response)) {//delete succeed
244+
id = 0;//reuse default value
245+
// }
229246
Log.d(TAG, "onHttpResponse delete.equals(method) >> id = " + id
230247
+ "; isSucceed = " + JSONResponse.isSucceed(response));
231248

@@ -255,7 +272,8 @@ public void run() {
255272

256273
@Override
257274
public void finish() {
258-
setResult(RESULT_OK, new Intent().putExtra(RESULT_ID, id).putExtra(RESULT_URL, url));
275+
setResult(RESULT_OK,
276+
new Intent().putExtra(RESULT_ID, id).putExtra(RESULT_URL, url).putExtra(RESULT_RESPONSE, resultJson));
259277
super.finish();
260278
}
261279

0 commit comments

Comments
 (0)