Skip to content

Commit 5a92a03

Browse files
committed
Android:APIJSONTest新增登录和退出
1 parent b55688a commit 5a92a03

File tree

6 files changed

+180
-102
lines changed

6 files changed

+180
-102
lines changed

APIJSON-Android/APIJSONTest/app/src/main/java/apijson/demo/RequestUtil.java

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,18 @@
1414

1515
package apijson.demo;
1616

17+
import android.content.Context;
18+
1719
import java.util.ArrayList;
1820
import java.util.List;
1921

20-
import zuo.biao.apijson.JSONObject;
21-
import zuo.biao.apijson.JSONRequest;
22-
import android.content.Context;
2322
import apijson.demo.application.DemoApplication;
2423
import apijson.demo.model.Comment;
2524
import apijson.demo.model.Moment;
2625
import apijson.demo.model.Privacy;
2726
import apijson.demo.model.User;
27+
import zuo.biao.apijson.JSONObject;
28+
import zuo.biao.apijson.JSONRequest;
2829

2930
/**请求工具类
3031
* @author Lemon
@@ -37,9 +38,21 @@ public class RequestUtil {
3738
}
3839

3940
private static final long DEFAULT_MOMENT_ID = 15;
40-
private static final long DEFAULT_USER_ID = 38710;
41+
private static final long DEFAULT_USER_ID = 82001;
4142

43+
public static boolean isLogMethod(String method) {
44+
return "login".equals(method) || "logout".equals(method);
45+
}
4246

47+
public static JSONObject newLoginRequest() {
48+
JSONRequest request = new JSONRequest();
49+
request.put("phone", "13000082001");
50+
request.put("password", "123456");
51+
return request;
52+
}
53+
public static JSONObject newLogoutRequest() {
54+
return new JSONRequest();
55+
}
4356

4457
public static JSONObject newPostRequest() {
4558
Moment data = new Moment();
@@ -56,8 +69,8 @@ public static JSONObject newPutRequest(long id) {
5669
Moment data = new Moment(id <= 0 ? DEFAULT_MOMENT_ID : id);
5770
// data.setContent(context.getString(R.string.apijson_info));//一般可用这种方式,encode是为了展示方便
5871
List<Long> list = new ArrayList<Long>();
59-
list.add((long) 82001);
6072
list.add((long) 82002);
73+
list.add((long) 82003);
6174
JSONObject momentObject = new JSONObject(data);
6275
momentObject.put("praiseUserIdList+", list);
6376
momentObject.put("content", context.getString(R.string.apijson_info));
@@ -121,4 +134,5 @@ public static JSONObject newAccessPermittedRequest() {
121134
return request.setTag(Privacy.class.getSimpleName());
122135
}
123136

137+
124138
}

APIJSON-Android/APIJSONTest/app/src/main/java/apijson/demo/ui/RequestActivity.java

Lines changed: 39 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,6 @@
1414

1515
package apijson.demo.ui;
1616

17-
import static zuo.biao.apijson.StringUtil.UTF_8;
18-
19-
import java.io.UnsupportedEncodingException;
20-
import java.net.URLEncoder;
21-
22-
import zuo.biao.apijson.JSON;
23-
import zuo.biao.apijson.JSONResponse;
2417
import android.app.Activity;
2518
import android.content.Context;
2619
import android.content.Intent;
@@ -35,15 +28,25 @@
3528
import android.widget.ProgressBar;
3629
import android.widget.TextView;
3730
import android.widget.Toast;
31+
32+
import com.alibaba.fastjson.JSONObject;
33+
34+
import java.io.UnsupportedEncodingException;
35+
import java.net.URLEncoder;
36+
3837
import apijson.demo.HttpManager;
3938
import apijson.demo.HttpManager.OnHttpResponseListener;
4039
import apijson.demo.R;
40+
import apijson.demo.RequestUtil;
4141
import apijson.demo.StringUtil;
4242
import apijson.demo.model.BaseModel;
4343
import apijson.demo.model.Moment;
4444
import apijson.demo.model.Privacy;
45+
import apijson.demo.model.User;
46+
import zuo.biao.apijson.JSON;
47+
import zuo.biao.apijson.JSONResponse;
4548

46-
import com.alibaba.fastjson.JSONObject;
49+
import static zuo.biao.apijson.StringUtil.UTF_8;
4750

4851
/**请求Activity
4952
* 向服务器发起请求查询或操作相应数据
@@ -60,6 +63,8 @@ public class RequestActivity extends Activity implements OnHttpResponseListener
6063

6164
public static final String RESULT_ID = "RESULT_ID";
6265
public static final String RESULT_URL = "RESULT_URL";
66+
public static final String RESULT_METHOD = "RESULT_METHOD";
67+
public static final String RESULT_NAME = "RESULT_NAME";
6368
public static final String RESULT_RESPONSE = "RESULT_RESPONSE";
6469

6570
/**
@@ -86,9 +91,9 @@ public static Intent createIntent(Context context, long id, String url, String m
8691
private boolean isAlive;
8792

8893
private long id;
89-
private String url;
90-
private String method;
91-
private String request;
94+
private String url;
95+
private String method;
96+
private String request;
9297

9398
private TextView tvRequestResult;
9499
private ProgressBar pbRequest;
@@ -212,27 +217,36 @@ public void onHttpResponse(int requestCode, final String resultJson, final Excep
212217
}
213218
JSONResponse response = new JSONResponse(resultJson);
214219

215-
if ("post".equals(method)) {
220+
if (RequestUtil.isLogMethod(method)) {
221+
User user = response.getObject(User.class);
222+
name = user == null ? null : user.getName();
223+
Log.d(TAG, "onHttpResponse login.equals(method) || logout.equals(method) >> name = " + name);
224+
}
225+
else if ("post".equals(method)) {
216226
Moment moment = response.getObject(Moment.class);
217227
id = moment == null ? 0 : BaseModel.value(moment.getId());
218228
Log.d(TAG, "onHttpResponse post.equals(method) >> id = " + id);
219-
220-
} else if ("put".equals(method)) {
229+
}
230+
else if ("put".equals(method)) {
221231
response.getJSONResponse(Moment.class.getSimpleName());
222232
Log.d(TAG, "onHttpResponse put.equals(method) >> moment = " + JSON.toJSONString(response));
223233

224-
} else if ("delete".equals(method)) {
234+
}
235+
else if ("delete".equals(method)) {
225236
response = response.getJSONResponse(Moment.class.getSimpleName());
226237
// if (JSONResponse.isSuccess(response)) {//delete succeed
227238
id = 0;//reuse default value
228239
// }
229240
Log.d(TAG, "onHttpResponse delete.equals(method) >> id = " + id
230241
+ "; isSucceed = " + JSONResponse.isSuccess(response));
231-
232-
} else if ("gets".equals(method)) {
242+
}
243+
else if ("gets".equals(method)) {
233244
Privacy privacy = response.getObject(Privacy.class);
234245
Log.d(TAG, "onHttpResponse gets.equals(method) >> privacy = \n" + JSON.toJSONString(privacy));
235246
}
247+
else {
248+
//do nothing
249+
}
236250

237251

238252
runOnUiThread(new Runnable() {
@@ -252,13 +266,17 @@ public void run() {
252266

253267

254268

269+
private String name;
255270

256271
@Override
257272
public void finish() {
258-
setResult(RESULT_OK, new Intent().
259-
putExtra(RESULT_ID, id).
260-
putExtra(RESULT_URL, url).
261-
putExtra(RESULT_RESPONSE, resultJson));
273+
setResult(RESULT_OK, new Intent()
274+
.putExtra(RESULT_ID, id)
275+
.putExtra(RESULT_URL, url)
276+
.putExtra(RESULT_METHOD, method)
277+
.putExtra(RESULT_NAME, name)
278+
.putExtra(RESULT_RESPONSE, resultJson)
279+
);
262280

263281
super.finish();
264282
}

0 commit comments

Comments
 (0)