欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android中post請求傳遞json數(shù)據(jù)給服務(wù)端的實(shí)例

 更新時間:2018年01月31日 09:50:15   作者:Lucky_bo  
下面小編就為大家分享一篇Android中post請求傳遞json數(shù)據(jù)給服務(wù)端的實(shí)例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧

在最近的項(xiàng)目中有個需求是這樣的:

入?yún)⒎庋b成JSON,EXAMPLE:

{ "uuid": "iamauuid", 
 "clientType": "AND", 
 "content": "{\"gender\":\"F\",\"name\":\"TTT\"}"}

其中content中是json的object,且要求把content中特殊的字符進(jìn)行轉(zhuǎn)義。

主要代碼如下:

/**
 * post請求用json的格式傳遞數(shù)據(jù)給服務(wù)器
 *
 * @param callback
 */
public static <T> void postShareContent(Context context, T saveObject, final ShareSaveDataCallback callback) {
 final String uuid = StringUtils.generateUUID().replaceAll("-", "");
 //利用Gson來處json數(shù)據(jù)和類模型之間的轉(zhuǎn)化
 Gson gson = new Gson();
 //把類模型對象轉(zhuǎn)化成json的數(shù)據(jù)模式
 String contentJson = gson.toJson(saveObject);
 //用ShareEntity把內(nèi)容給封裝起來,再處理成json的格式
 ShareEntity shareEntity = new ShareEntity();
 //uuid本地端生成,有特殊的方法,下面會注解
 shareEntity.uuid = uuid;
 shareEntity.clientType = "AND";
 shareEntity.content = contentJson;
 //再用gson.toJson()處理會自動把第二層的object對象的特殊字符轉(zhuǎn)義掉
 String shareJson = gson.toJson(shareEntity);
 StringEntity entity = null;
 try {
  //把要傳給后端的json數(shù)據(jù),用StringEntity給封裝起來
  entity = new StringEntity(shareJson,"UTF-8");
  //注明:UTF-8是防止傳給后端時中文出現(xiàn)亂碼
 } 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);
  }
 });
}

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的類型為:application/json
/**
public interface ApiParam {
 /**
  * json格式
  */
 String CONTENT_TYPE_JSON = "application/json";
}

其中生產(chǎn)本地的uuid方法如下:相當(dāng)于會自動生產(chǎn)一組隨機(jī)數(shù)

public static String generateUUID() {
 return UUID.randomUUID().toString();
}

數(shù)據(jù)代碼的一部分:

{"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\":\"張宇斌\",\"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\":\"林修乾\",\"id\":1209,\"playerId\":0,\"schoolId\":116,\"timeLineEventType\":\"SU\",\"timeMin\":4,\"timeSec\":3,\"upPlayerHeadUrl\":......}

由于數(shù)據(jù)量比較大,固就顯示一部分的數(shù)據(jù)來說明問題。

基本就是這樣,快去解決你們的問題吧!

以上這篇Android中post請求傳遞json數(shù)據(jù)給服務(wù)端的實(shí)例就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論