自己封装Jackson的工具类——JsonUtil
来源:互联网 发布:三浦春马海报淘宝 编辑:程序博客网 时间:2024/05/17 08:10
使用Jackson
import android.text.TextUtils;import android.util.Log;import com.fasterxml.jackson.core.JsonProcessingException;import com.fasterxml.jackson.databind.ObjectMapper;import com.fasterxml.jackson.databind.SerializationFeature;import java.io.IOException;/** * Created by kevin on 15-1-24. */public class JsonSendUtil { private static ObjectMapper mapper; private static final String TAG = "JsonSendUtil"; public static final String SEND_FAILED = "1"; static { mapper = new ObjectMapper(); //mapper.disable(SerializationFeature.FAIL_ON_EMPTY_BEANS); } public static synchronized <T, U> U sendJsonForGetResult(T entity, Class<U> resultClass, String url) { String json; U resultEntity = null; try { json = mapper.writeValueAsString(entity); if (json != null) { String result = CustomHttpURLConnection.PostJsonToWebByHttpURLConnection(url, json); Log.d(TAG,"result: " + result +" Json: "+json); if (!TextUtils.isEmpty(result)) { resultEntity = mapper.readValue(result, resultClass); } } } catch (JsonProcessingException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return resultEntity; }}
使用
import com.fasterxml.jackson.annotation.JsonProperty;import cn.rspread.im.IMApplication;/** * Created by kevin on 15-1-23. */public class LoginOrRegisterEntity { //{"phone":{"ApiKey":"123456","phone":"861860302645"}} private Phone phone; public LoginOrRegisterEntity(String phoneNumber) { this.phone = new Phone(); phone.setApiKey(IMApplication.API_KEY); phone.setPhone(phoneNumber); } public Phone getPhone() { return phone; } public void setPhone(Phone phone) { this.phone = phone; } public static class Phone { @JsonProperty("ApiKey") private String apiKey; private String phone; public String getApiKey() { return apiKey; } public void setApiKey(String apiKey) { this.apiKey = apiKey; } public String getPhone() { return phone; } public void setPhone(String phone) { this.phone = phone; } }}
import com.fasterxml.jackson.annotation.JsonProperty;/** * Created by kevin on 15-1-23. */public class LoginOrRegisterResultEntity { //{"SendSmsResult":"1"} @JsonProperty("SendSmsResult") private String sendSmsResult; public String getSendSmsResult() { return sendSmsResult; } public void setSendSmsResult(String sendSmsResult) { this.sendSmsResult = sendSmsResult; }}
调用
public static boolean sendLoginOrRegister(String phone) { if (TextUtils.isEmpty(phone)) { return false; } loginPhoneNumber = phone; LoginOrRegisterEntity loginOrRegisterEntity = new LoginOrRegisterEntity(phone); LoginOrRegisterResultEntity resultEntity = JsonSendUtil.sendJsonForGetResult(loginOrRegisterEntity, LoginOrRegisterResultEntity.class, LOGIN_URL); if (resultEntity==null) { return false; } return ("1").equals(resultEntity.getSendSmsResult()); }
0 0
- 自己封装Jackson的工具类——JsonUtil
- JsonUtil工具类的封装
- 常用JSON工具类JsonUtil封装
- jackson jsonUtil
- JSON工具类JSONUtil
- JsonUtil工具类
- Json工具类 ---JsonUtil
- Json工具类JsonUtil
- Java-JsonUtil工具类
- JSONUtil工具类
- JSONUtil工具类
- jsonutil工具类
- 【utils】基于jackson的解析转换JsonUtil
- Json转换工具类JsonUtil
- 工具类分享之《JsonUtil》
- fastjson 与jackson 工具类封装
- jackson 工具类的 3 个常用方法,封装成工具类
- html工具类收藏,自己封装的
- Cadence OrCad Allegro SPB 16.6 下载及安装破解指南
- Jqury表格
- java自带线程池和队列详细讲解
- MINA的使用
- HTML5之4__Canvas API: 绘制曲线、变换
- 自己封装Jackson的工具类——JsonUtil
- Android框架Annotations浅析
- iOS图片拉伸技巧
- 转动数组
- node的知识点
- Microsoft DOS cmd and command
- unordered_map
- iOS应用性能调优的25个建议和技巧
- 关于json解析时异常org.json.JSONException: A JSONObject text must begin with '{' at character 1 of {的解决方法