Client:adt保存POST返回id用于后续操作

This commit is contained in:
TommyLemon 2017-01-26 22:12:42 +08:00
parent d751d3c5ce
commit 08163b2e56
7 changed files with 101 additions and 63 deletions

View File

@ -35,7 +35,7 @@ public final class R {
public static final int app_name=0x7f050000;
public static final int app_version=0x7f050001;
public static final int array=0x7f050015;
public static final int browser_can_only_receive_get_response=0x7f05001b;
public static final int browser_can_only_receive_get_response=0x7f05001c;
public static final int columns=0x7f050013;
public static final int complex=0x7f050016;
public static final int delete=0x7f05000f;
@ -43,9 +43,9 @@ public final class R {
public static final int demo_columns=0x7f050006;
public static final int demo_complex=0x7f050009;
public static final int demo_complex_post=0x7f05000c;
public static final int demo_delete=0x7f050003;
public static final int demo_delete=0x7f050004;
public static final int demo_post=0x7f050002;
public static final int demo_put=0x7f050004;
public static final int demo_put=0x7f050003;
public static final int demo_rely=0x7f050007;
public static final int demo_single=0x7f050005;
public static final int demo_wallet=0x7f05000a;
@ -54,11 +54,12 @@ public final class R {
public static final int post=0x7f05000e;
public static final int put=0x7f050010;
public static final int query=0x7f05000d;
public static final int query_error=0x7f05001c;
public static final int query_error=0x7f05001d;
public static final int received_result=0x7f05001a;
public static final int rely=0x7f050014;
public static final int single=0x7f050012;
public static final int update_log=0x7f050019;
public static final int user_id_changed=0x7f05001b;
}
public static final class style {
/**

View File

@ -18,6 +18,15 @@
android:onClick="selectPost"
android:text="@string/demo_post" />
<TextView
style="@style/select_name"
android:text="@string/put" />
<Button
style="@style/select_json"
android:onClick="selectPut"
android:text="@string/demo_put" />
<TextView
style="@style/select_name"
android:text="@string/delete"
@ -28,15 +37,6 @@
android:onClick="selectDelete"
android:text="@string/demo_delete" />
<TextView
style="@style/select_name"
android:text="@string/put" />
<Button
style="@style/select_json"
android:onClick="selectPut"
android:text="@string/demo_put" />
<TextView
style="@style/select_name"
android:text="@string/get"

View File

@ -15,6 +15,7 @@
<string name="access_permitted">Access Permitted</string>
<string name="update_log">Update Log</string>
<string name="received_result">received result!</string>
<string name="user_id_changed">"userId was changed to %1$s ,you can restore it by [DELETE]"</string>
<string name="browser_can_only_receive_get_response">Browsers can only receive GET responses</string>
<string name="query_error">There may be something wrong,you can follow by the steps:\n\n1.Check your net connection\n\n2.Check the url whether it\'s an available ipv4 address\n\n3.Long click the [ %1$s ] button to open the request by web browser\n\n4.Check logs outputed on the target server\n\n5.Try again</string>

View File

@ -4,8 +4,8 @@
<string name="app_name">APIJSON</string>
<string name="app_version">1.0</string>
<string name="demo_post">{\n&#160;&#160;&#160;\"tag\":\"User\",\n&#160;&#160;&#160;\"User\":{\n&#160;&#160;&#160;&#160;&#160;&#160;\"name\":\"Tommy\",\n&#160;&#160;&#160;&#160;&#160;&#160;\"sex\":0,\n&#160;&#160;&#160;&#160;&#160;&#160;\"phone\":\"1234567890\"\n&#160;&#160;&#160;}\n}</string>
<string name="demo_put">{\n&#160;&#160;&#160;\"tag\":\"User\",\n&#160;&#160;&#160;\"User\":{\n&#160;&#160;&#160;&#160;&#160;&#160;\"id\":38710,\n&#160;&#160;&#160;&#160;&#160;&#160;\"name\":\"Lemon\",\n&#160;&#160;&#160;&#160;&#160;&#160;\"picture\":\"[\"http://static.oschina.net/uploads/user/1218/2437072_100.jpg?t=1461076033000\", \"http://common.cnblogs.com/images/icon_weibo_24.png\"]\"\n&#160;&#160;&#160;}\n}</string>
<string name="demo_delete">{\n&#160;&#160;&#160;\"tag\":\"User\",\n&#160;&#160;&#160;\"User\":{\n&#160;&#160;&#160;&#160;&#160;&#160;\"id\":10000\n&#160;&#160;&#160;}\n}</string>
<string name="demo_put">{\n&#160;&#160;&#160;\"tag\":\"User\",\n&#160;&#160;&#160;\"User\":{\n&#160;&#160;&#160;&#160;&#160;&#160;\"id\":38710,\n&#160;&#160;&#160;&#160;&#160;&#160;\"name\":\"Lemon\",\n&#160;&#160;&#160;&#160;&#160;&#160;\"picture\":\"[\"http://static.oschina.net/uploads/user/1218/2437072_100.jpg?t=1461076033000\", \"http://common.cnblogs.com/images/icon_weibo_24.png\", \"http://static.oschina.net/uploads/user/585/1170143_50.jpg?t=1390226446000\"]\"\n&#160;&#160;&#160;}\n}</string>
<string name="demo_single">{\n&#160;&#160;&#160;\"User\":{\n&#160;&#160;&#160;&#160;&#160;&#160;\"id\":38710\n&#160;&#160;&#160;}\n}</string>
<string name="demo_columns">{\n&#160;&#160;&#160;\"User\":{\n&#160;&#160;&#160;&#160;&#160;&#160;\"columns\":\"id,name,phone\",\n&#160;&#160;&#160;&#160;&#160;&#160;\"id\":38710\n&#160;&#160;&#160;}\n}</string>
<string name="demo_rely">{\n&#160;&#160;&#160;\"User\":{\n&#160;&#160;&#160;&#160;&#160;&#160;\"id\":70793\n&#160;&#160;&#160;},\n&#160;&#160;&#160;\"Work\":{\n&#160;&#160;&#160;&#160;&#160;&#160;\"userId\":\"User/id\"\n&#160;&#160;&#160;}\n}</string>
@ -28,6 +28,7 @@
<string name="access_permitted">允许使用权限</string>
<string name="update_log">更新日志</string>
<string name="received_result">已获得结果!</string>
<string name="user_id_changed">"userId已改为 %1$s ,【删】后会恢复默认值"</string>
<string name="browser_can_only_receive_get_response">用浏览器只能获取GET结果</string>
<string name="query_error">可能出现了一些问题,可以按照以下步骤解决:\n\n1.检查网络连接\n\n2.检查url是否为一个可用的IPV4地址\n\n3.长按[ %1$s ]按钮用浏览器发送请求\n\n4.查看目标服务器上的log\n\n5.再试一次</string>

View File

@ -41,41 +41,41 @@ public class RequestUtil {
return request.setTag(User.class.getSimpleName());//;// 测试必须指定tag通过
}
public static JSONObject newDeleteRequest() {
// 测试necessaryColumns = [id]通过
// 测试对象不存在通过存在返回success通过
return new JSONRequest(new User(10000)).setTag(User.class.getSimpleName());//;// 测试必须指定tag通过
}
public static JSONObject newPutRequest() {
User data = new User(38710);//);// 测试necessaryColumns = [id]通过
public static JSONObject newPutRequest(long id) {
User data = new User(id <= 0 ? 38710 : id);//);// 测试necessaryColumns = [id]通过
data.setName("Lemon");
//测试disallowColumns = [phone]通过 data.setPhone("1234567890");
List<String> list = new ArrayList<String>();
list.add("http://static.oschina.net/uploads/user/1218/2437072_100.jpg?t=1461076033000");
list.add("http://common.cnblogs.com/images/icon_weibo_24.png");
list.add("http://static.oschina.net/uploads/user/585/1170143_50.jpg?t=1390226446000");
data.setPicture(JSON.toJSONString(list));//"\"" + JSON.toJSONString(new JSONObject("{\"id\":2}")) + "\"");//
return new JSONRequest(data).setTag(User.class.getSimpleName());
}
public static JSONObject newSingleRequest() {
return new JSONRequest(new User(38710));
public static JSONObject newDeleteRequest(long id) {
// 测试necessaryColumns = [id]通过
// 测试对象不存在通过存在返回success通过
return new JSONRequest(new User(id <= 0 ? 10000 : id)).setTag(User.class.getSimpleName());//;// 测试必须指定tag通过
}
public static JSONObject newColumnsRequest() {
JSONObject object = new JSONObject(new User(38710));
public static JSONObject newSingleRequest(long id) {
return new JSONRequest(new User(id <= 0 ? 38710 : id));
}
public static JSONObject newColumnsRequest(long id) {
JSONObject object = new JSONObject(new User(id <= 0 ? 38710 : id));
object.setColumns("id,name,phone");//测试排序通过 //StringUtil.getString(new String[]{"id", "name", "phone"}));//
return new JSONRequest(User.class.getSimpleName(), object);
}
public static JSONObject newRelyRequest() {
public static JSONObject newRelyRequest(long id) {
JSONRequest request = new JSONRequest();
request.put(new User(70793));
request.put(new User(id <= 0 ? 70793 : id));
request.put(Work.class.getSimpleName(), new JSONRequest("userId", "User/id"));
return request;
}
@ -95,13 +95,13 @@ public class RequestUtil {
return request.toArray(2, 0);
}
public static JSONObject newAccessErrorRequest() {
return new JSONRequest(new Wallet(38710));
public static JSONObject newAccessErrorRequest(long id) {
return new JSONRequest(new Wallet().setUserId(id <= 0 ? 38710 : id));
}
public static JSONObject newAccessPermittedRequest() {
public static JSONObject newAccessPermittedRequest(long id) {
JSONRequest request = new JSONRequest();
request.put(new Wallet().setUserId((long) 38710));
request.put(new Wallet().setUserId(id <= 0 ? 38710 : id));
request.put("currentUserId", 38710);
request.put("payPassword", "123456");
return request;

View File

@ -47,6 +47,7 @@ import apijson.demo.model.Wallet;
import apijson.demo.model.Work;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
/**activity for requesting a query in Server
* @author Lemon
@ -57,26 +58,30 @@ public class QueryActivity extends Activity implements OnHttpResponseListener {
public static final String INTENT_TYPE = "INTENT_TYPE";
public static final String INTENT_URL = "INTENT_URL";
public static final String INTENT_ID = "INTENT_ID";
public static final String RESULT_URL = "RESULT_URL";
public static final String RESULT_ID = "RESULT_ID";
/**
* @param context
* @param type
* @param url
* @param id
* @return
*/
public static Intent createIntent(Context context, int type, String url) {
public static Intent createIntent(Context context, int type, String url, long id) {
return new Intent(context, QueryActivity.class)
.putExtra(QueryActivity.INTENT_TYPE, type)
.putExtra(QueryActivity.INTENT_URL, url);
.putExtra(QueryActivity.INTENT_URL, url)
.putExtra(QueryActivity.INTENT_ID, id);
}
public static final int TYPE_POST = 0;
public static final int TYPE_DELETE = 1;
public static final int TYPE_PUT = 2;
public static final int TYPE_PUT = 1;
public static final int TYPE_DELETE = 2;
public static final int TYPE_SINGLE = 10;
public static final int TYPE_COLUMNS = 11;
@ -93,13 +98,15 @@ public class QueryActivity extends Activity implements OnHttpResponseListener {
private int type = TYPE_COMPLEX;
private String url;
private String error;
private long id;
private TextView tvQueryResult;
private ProgressBar pbQuery;
private EditText etQueryUrl;
private Button btnQueryQuery;
private String error;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
@ -110,23 +117,26 @@ public class QueryActivity extends Activity implements OnHttpResponseListener {
Intent intent = getIntent();
type = intent.getIntExtra(INTENT_TYPE, type);
url = intent.getStringExtra(INTENT_URL);
id = intent.getLongExtra(INTENT_ID, id);
tvQueryResult = (TextView) findViewById(R.id.tvQueryResult);
pbQuery = (ProgressBar) findViewById(R.id.pbQuery);
etQueryUrl = (EditText) findViewById(R.id.etQueryUrl);
btnQueryQuery = (Button) findViewById(R.id.btnQueryQuery);
etQueryUrl.setText(StringUtil.getString(StringUtil.isNotEmpty(url, true)
? url : "http://139.196.140.118:8080/"));//TODO my server ipv4 address, edit it to your server url
btnQueryQuery.setText(getMethod(type));
error = String.format(getResources().getString(R.string.query_error), StringUtil.getTrimedString(btnQueryQuery));
query();
btnQueryQuery.setOnClickListener(new OnClickListener() {
@ -143,6 +153,13 @@ public class QueryActivity extends Activity implements OnHttpResponseListener {
return true;
}
});
if (id > 0) {
Toast.makeText(context, String.format(getResources().getString(R.string.user_id_changed), "" + id)
, Toast.LENGTH_LONG).show();
}
}
@ -196,10 +213,10 @@ public class QueryActivity extends Activity implements OnHttpResponseListener {
switch (type) {
case TYPE_POST:
return "post";
case TYPE_DELETE:
return "delete";
case TYPE_PUT:
return "put";
case TYPE_DELETE:
return "delete";
default:
return "get";
}
@ -212,30 +229,30 @@ public class QueryActivity extends Activity implements OnHttpResponseListener {
case TYPE_POST:
request = JSON.toJSONString(RequestUtil.newPostRequest());
break;
case TYPE_DELETE:
request = JSON.toJSONString(RequestUtil.newDeleteRequest());
break;
case TYPE_PUT:
request = JSON.toJSONString(RequestUtil.newPutRequest());
request = JSON.toJSONString(RequestUtil.newPutRequest(id));
break;
case TYPE_DELETE:
request = JSON.toJSONString(RequestUtil.newDeleteRequest(id));
break;
case TYPE_SINGLE:
request = JSON.toJSONString(RequestUtil.newSingleRequest());
request = JSON.toJSONString(RequestUtil.newSingleRequest(id));
break;
case TYPE_COLUMNS:
request = JSON.toJSONString(RequestUtil.newColumnsRequest());
request = JSON.toJSONString(RequestUtil.newColumnsRequest(id));
break;
case TYPE_RELY:
request = JSON.toJSONString(RequestUtil.newRelyRequest());
request = JSON.toJSONString(RequestUtil.newRelyRequest(id));
break;
case TYPE_ARRAY:
request = JSON.toJSONString(RequestUtil.newArrayRequest());
break;
case TYPE_ACCESS_ERROR:
request = JSON.toJSONString(RequestUtil.newAccessErrorRequest());
request = JSON.toJSONString(RequestUtil.newAccessErrorRequest(id));
break;
case TYPE_ACCESS_PERMITTED:
request = JSON.toJSONString(RequestUtil.newAccessPermittedRequest());
request = JSON.toJSONString(RequestUtil.newAccessPermittedRequest(id));
break;
default:
request = JSON.toJSONString(RequestUtil.newComplexRequest());
@ -252,9 +269,22 @@ public class QueryActivity extends Activity implements OnHttpResponseListener {
Log.e(TAG, "onHttpResponse e = " + e.getMessage());
}
JSONResponse response = new JSONResponse(resultJson);
if (type == TYPE_ARRAY) {
switch (type) {
case TYPE_POST:
User postedUser = JSONResponse.getObject(response, User.class);
id = postedUser == null ? 0 : postedUser.getId();
Log.d(TAG, "onHttpResponse id = " + id);
break;
case TYPE_DELETE:
JSONObject result = response.getJSONObject(User.class.getSimpleName());
if (result != null && result.getIntValue("status") == 200) {//delete succeed
id = 0;//reuse default value
}
break;
case TYPE_ARRAY:
logList(JSONResponse.getList(response.getJSONObject("User[]"), User.class));
} else if (type == TYPE_COMPLEX) {
break;
case TYPE_COMPLEX:
JSONArray array = JSONResponse.getJSONArray(response.getJSONObject("[]"));//, "Comment[]");//
if (array == null || array.isEmpty()) {
Log.e(TAG, "onHttpResponse type == TYPE_COMPLEX >> array == null || array.isEmpty() >> return;");
@ -267,10 +297,13 @@ public class QueryActivity extends Activity implements OnHttpResponseListener {
Log.d(TAG, "onHttpResponse type == TYPE_COMPLEX >> work = " + JSON.toJSONString(work));
logList(JSONResponse.getList(response == null ? null : response.getJSONObject("Comment[]"), Comment.class));
}
} else if (type == TYPE_ACCESS_PERMITTED) {
break;
case TYPE_ACCESS_PERMITTED:
response = new JSONResponse(resultJson);
Wallet wallet = JSONResponse.getObject(response, Wallet.class);
Log.d(TAG, "onHttpResponse type == TYPE_ACCESS_PERMITTED >> wallet = " + JSON.toJSONString(wallet));
default:
break;
}
runOnUiThread(new Runnable() {
@ -304,7 +337,7 @@ public class QueryActivity extends Activity implements OnHttpResponseListener {
@Override
public void finish() {
setResult(RESULT_OK, new Intent().putExtra(RESULT_URL, url));
setResult(RESULT_OK, new Intent().putExtra(RESULT_URL, url).putExtra(RESULT_ID, id));
super.finish();
}

View File

@ -45,14 +45,14 @@ public class SelectActivity extends Activity {
select(QueryActivity.TYPE_POST);
}
public void selectDelete(View v) {
select(QueryActivity.TYPE_DELETE);
}
public void selectPut(View v) {
select(QueryActivity.TYPE_PUT);
}
public void selectDelete(View v) {
select(QueryActivity.TYPE_DELETE);
}
//get <<<<<<<<<<<<<<<<<<<<<<<<<<<
public void selectSingle(View v) {
select(QueryActivity.TYPE_SINGLE);
@ -92,8 +92,9 @@ public class SelectActivity extends Activity {
//click event,called form layout android:onClick >>>>>>>>>>>>>>>>
private String url;
private long id;
private void select(int type) {
startActivityForResult(QueryActivity.createIntent(context, type, url), REQUEST_TO_QUERY);
startActivityForResult(QueryActivity.createIntent(context, type, url, id), REQUEST_TO_QUERY);
}
@ -111,6 +112,7 @@ public class SelectActivity extends Activity {
Toast.makeText(context, "onActivityResult data == null !!!", Toast.LENGTH_SHORT).show();
} else {
url = data.getStringExtra(QueryActivity.RESULT_URL);
id = data.getLongExtra(QueryActivity.RESULT_ID, id);
}
break;
default: