post in Android requests to pass json data to the server instance
- 2021-08-17 00:58:02
- OfStack
In a recent project, there is a requirement like this:
The input parameters are encapsulated into JSON and EXAMPLE:
{ "uuid": "iamauuid",
"clientType": "AND",
"content": "{\"gender\":\"F\",\"name\":\"TTT\"}"}
Among them, content is object of json, and special characters in content are required to be escaped.
The main code is as follows:
/**
* post Request with json Transfer data to the server in the format of
*
* @param callback
*/
public static <T> void postShareContent(Context context, T saveObject, final ShareSaveDataCallback callback) {
final String uuid = StringUtils.generateUUID().replaceAll("-", "");
// Utilization Gson Come to a place json Transformation between data and class model
Gson gson = new Gson();
// Convert class model objects into json Data schema of
String contentJson = gson.toJson(saveObject);
// Use ShareEntity Encapsulate the content and then process it into json Format of
ShareEntity shareEntity = new ShareEntity();
//uuid Local generation, there are special methods, which will be annotated below
shareEntity.uuid = uuid;
shareEntity.clientType = "AND";
shareEntity.content = contentJson;
// Reuse gson.toJson() Processing will automatically put the 2 Layered object Escape the special characters of the object
String shareJson = gson.toJson(shareEntity);
StringEntity entity = null;
try {
// To pass to the back end json Data, using StringEntity To encapsulate
entity = new StringEntity(shareJson,"UTF-8");
// Note: UTF-8 It is to prevent Chinese from being garbled when it is transmitted to the back end
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
HttpApiClient.postJsonByAccessToken(context, ApiBaseUrl.getShareUrl(), entity, true, new ApiResponseHandler() {
@Override
public void onResponse(boolean success, JsonObject jsonObject, ErrorCode errCode) {
super.onResponse(success, jsonObject, errCode);
callback.onSaveResult(success, uuid, errCode);
}
});
}
Method of postJsonByAccessToken:
private static AsyncHttpClient mHttpClient = new AsyncHttpClient();
public static void postJsonByAccessToken(Context context, String url, StringEntity entity, boolean isCompleterUrl, AsyncHttpResponseHandler handler) {
synchronized (mHttpClient) {
addHeader(HEADER_TOKEN, token);
if (!isCompleterUrl) {
url = getCompleteUrl(url);
}
mHttpClient.post(context, url, entity, ApiParam.CONTENT_TYPE_JSON, handler);
}
}
contentType The type of is: application/json
/**
public interface ApiParam {
/**
* json Format
*/
String CONTENT_TYPE_JSON = "application/json";
}
The method of producing local uuid is as follows: it is equivalent to automatically producing a set of random numbers
public static String generateUUID() {
return UUID.randomUUID().toString();
}
Part 1 of the data code:
{"clientType":"AND",
"uuid":"e3ab0260286d442da86da7fac21e1cc7"
"content":"{\"matchEventStats\":{\"extraTime\":0,\"timeLineModels\":[{\"downPlayerId\":0,\"id\":2188,\"playerHeadUrl\":\"http://7xj3pr.com1.z0.glb.clouddn.com/registration/user/head/image/rdGsidKZHjPzun6TMrTDyMz7IngTONlQ\",\"playerId\":1147,\"playerName\":\" Zhang Yubin \",\"schoolId\":6,\"timeLineEventType\":\"GOAL\",\"timeMin\":0,\"timeSec\":0,\"upPlayerId\":0},{\"downPlayerHeadUrl\":\"http://7xj3pr.com1.z0.glb.clouddn.com/default/head/useravatar.png\",\"downPlayerId\":1682,\"downPlayerName\":\" Lin Xiugan \",\"id\":1209,\"playerId\":0,\"schoolId\":116,\"timeLineEventType\":\"SU\",\"timeMin\":4,\"timeSec\":3,\"upPlayerHeadUrl\":......}
Because of the large amount of data, the data in Part 1 is displayed to illustrate the problem.
That's basically it. Go and solve your problems!