| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent b55688a commit 5a92a03
6 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -14,17 +14,18 @@ | |||
| 14 | 14 | ||
| 15 | 15 | package apijson.demo; | |
| 16 | 16 | ||
| 17 | + import android.content.Context; | ||
| 18 | + | ||
| 17 | 19 | import java.util.ArrayList; | |
| 18 | 20 | import java.util.List; | |
| 19 | 21 | ||
| 20 | - import zuo.biao.apijson.JSONObject; | ||
| 21 | - import zuo.biao.apijson.JSONRequest; | ||
| 22 | - import android.content.Context; | ||
| 23 | 22 | import apijson.demo.application.DemoApplication; | |
| 24 | 23 | import apijson.demo.model.Comment; | |
| 25 | 24 | import apijson.demo.model.Moment; | |
| 26 | 25 | import apijson.demo.model.Privacy; | |
| 27 | 26 | import apijson.demo.model.User; | |
| 27 | + import zuo.biao.apijson.JSONObject; | ||
| 28 | + import zuo.biao.apijson.JSONRequest; | ||
| 28 | 29 | ||
| 29 | 30 | /**请求工具类 | |
| 30 | 31 | * @author Lemon | |
@@ -37,9 +38,21 @@ public class RequestUtil { | |||
| 37 | 38 | } | |
| 38 | 39 | ||
| 39 | 40 | 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; | ||
| 41 | 42 | ||
| 43 | + public static boolean isLogMethod(String method) { | ||
| 44 | + return "login".equals(method) || "logout".equals(method); | ||
| 45 | + } | ||
| 42 | 46 | ||
| 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 | + } | ||
| 43 | 56 | ||
| 44 | 57 | public static JSONObject newPostRequest() { | |
| 45 | 58 | Moment data = new Moment(); | |
@@ -56,8 +69,8 @@ public static JSONObject newPutRequest(long id) { | |||
| 56 | 69 | Moment data = new Moment(id <= 0 ? DEFAULT_MOMENT_ID : id); | |
| 57 | 70 | // data.setContent(context.getString(R.string.apijson_info));//一般可用这种方式,encode是为了展示方便 | |
| 58 | 71 | List<Long> list = new ArrayList<Long>(); | |
| 59 | - list.add((long) 82001); | ||
| 60 | 72 | list.add((long) 82002); | |
| 73 | + list.add((long) 82003); | ||
| 61 | 74 | JSONObject momentObject = new JSONObject(data); | |
| 62 | 75 | momentObject.put("praiseUserIdList+", list); | |
| 63 | 76 | momentObject.put("content", context.getString(R.string.apijson_info)); | |
@@ -121,4 +134,5 @@ public static JSONObject newAccessPermittedRequest() { | |||
| 121 | 134 | return request.setTag(Privacy.class.getSimpleName()); | |
| 122 | 135 | } | |
| 123 | 136 | ||
| 137 | + | ||
| 124 | 138 | } | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -14,13 +14,6 @@ | |||
| 14 | 14 | ||
| 15 | 15 | package apijson.demo.ui; | |
| 16 | 16 | ||
| 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; | ||
| 24 | 17 | import android.app.Activity; | |
| 25 | 18 | import android.content.Context; | |
| 26 | 19 | import android.content.Intent; | |
@@ -35,15 +28,25 @@ | |||
| 35 | 28 | import android.widget.ProgressBar; | |
| 36 | 29 | import android.widget.TextView; | |
| 37 | 30 | import android.widget.Toast; | |
| 31 | + | ||
| 32 | + import com.alibaba.fastjson.JSONObject; | ||
| 33 | + | ||
| 34 | + import java.io.UnsupportedEncodingException; | ||
| 35 | + import java.net.URLEncoder; | ||
| 36 | + | ||
| 38 | 37 | import apijson.demo.HttpManager; | |
| 39 | 38 | import apijson.demo.HttpManager.OnHttpResponseListener; | |
| 40 | 39 | import apijson.demo.R; | |
| 40 | + import apijson.demo.RequestUtil; | ||
| 41 | 41 | import apijson.demo.StringUtil; | |
| 42 | 42 | import apijson.demo.model.BaseModel; | |
| 43 | 43 | import apijson.demo.model.Moment; | |
| 44 | 44 | import apijson.demo.model.Privacy; | |
| 45 | + import apijson.demo.model.User; | ||
| 46 | + import zuo.biao.apijson.JSON; | ||
| 47 | + import zuo.biao.apijson.JSONResponse; | ||
| 45 | 48 | ||
| 46 | - import com.alibaba.fastjson.JSONObject; | ||
| 49 | + import static zuo.biao.apijson.StringUtil.UTF_8; | ||
| 47 | 50 | ||
| 48 | 51 | /**请求Activity | |
| 49 | 52 | * 向服务器发起请求查询或操作相应数据 | |
@@ -60,6 +63,8 @@ public class RequestActivity extends Activity implements OnHttpResponseListener | |||
| 60 | 63 | ||
| 61 | 64 | public static final String RESULT_ID = "RESULT_ID"; | |
| 62 | 65 | 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"; | ||
| 63 | 68 | public static final String RESULT_RESPONSE = "RESULT_RESPONSE"; | |
| 64 | 69 | ||
| 65 | 70 | /** | |
@@ -86,9 +91,9 @@ public static Intent createIntent(Context context, long id, String url, String m | |||
| 86 | 91 | private boolean isAlive; | |
| 87 | 92 | ||
| 88 | 93 | 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; | ||
| 92 | 97 | ||
| 93 | 98 | private TextView tvRequestResult; | |
| 94 | 99 | private ProgressBar pbRequest; | |
@@ -212,27 +217,36 @@ public void onHttpResponse(int requestCode, final String resultJson, final Excep | |||
| 212 | 217 | } | |
| 213 | 218 | JSONResponse response = new JSONResponse(resultJson); | |
| 214 | 219 | ||
| 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)) { | ||
| 216 | 226 | Moment moment = response.getObject(Moment.class); | |
| 217 | 227 | id = moment == null ? 0 : BaseModel.value(moment.getId()); | |
| 218 | 228 | Log.d(TAG, "onHttpResponse post.equals(method) >> id = " + id); | |
| 219 | - | ||
| 220 | - } else if ("put".equals(method)) { | ||
| 229 | + } | ||
| 230 | + else if ("put".equals(method)) { | ||
| 221 | 231 | response.getJSONResponse(Moment.class.getSimpleName()); | |
| 222 | 232 | Log.d(TAG, "onHttpResponse put.equals(method) >> moment = " + JSON.toJSONString(response)); | |
| 223 | 233 | ||
| 224 | - } else if ("delete".equals(method)) { | ||
| 234 | + } | ||
| 235 | + else if ("delete".equals(method)) { | ||
| 225 | 236 | response = response.getJSONResponse(Moment.class.getSimpleName()); | |
| 226 | 237 | // if (JSONResponse.isSuccess(response)) {//delete succeed | |
| 227 | 238 | id = 0;//reuse default value | |
| 228 | 239 | // } | |
| 229 | 240 | Log.d(TAG, "onHttpResponse delete.equals(method) >> id = " + id | |
| 230 | 241 | + "; isSucceed = " + JSONResponse.isSuccess(response)); | |
| 231 | - | ||
| 232 | - } else if ("gets".equals(method)) { | ||
| 242 | + } | ||
| 243 | + else if ("gets".equals(method)) { | ||
| 233 | 244 | Privacy privacy = response.getObject(Privacy.class); | |
| 234 | 245 | Log.d(TAG, "onHttpResponse gets.equals(method) >> privacy = \n" + JSON.toJSONString(privacy)); | |
| 235 | 246 | } | |
| 247 | + else { | ||
| 248 | + //do nothing | ||
| 249 | + } | ||
| 236 | 250 | ||
| 237 | 251 | ||
| 238 | 252 | runOnUiThread(new Runnable() { | |
@@ -252,13 +266,17 @@ public void run() { | |||
| 252 | 266 | ||
| 253 | 267 | ||
| 254 | 268 | ||
| 269 | + private String name; | ||
| 255 | 270 | ||
| 256 | 271 | @Override | |
| 257 | 272 | 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 | + ); | ||
| 262 | 280 | ||
| 263 | 281 | super.finish(); | |
| 264 | 282 | } | |
| Back | FazBrowse Home | New Git URL |
0 commit comments