自己封装Jackson的工具类——JsonUtil
2015-05-07 12:03
405 查看
使用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()); }
相关文章推荐
- 处理json工具类JacksonUtil
- fastjson 与jackson 工具类封装
- 用jackson封装的JSON工具类
- 用jackson封装的JSON工具类
- 用jackson封装的JSON工具类
- 利用jackson/fastjson封装JSON转换工具类
- 自己写的JSON 工具类,封装一些常用的方法
- 常用JSON工具类JsonUtil封装
- 自己封装的json工具类
- 闲来无事,将自己收集到的工具类封装了下,做了一个lib,写了个简单的mvp,初学者可以看看
- Java 的 JSON 开源类库之JackSon工具类--JSONUtils
- Util工具类 URLConnectionUtil - 20141211 封装为访问网络框架
- 自己编写的一个Json工具类,实现了反射将整个Object转换为Json对象的功能,支持Hibernate的延迟加载对象
- 自己封装ajax,json.
- 自己封装的一个工具类
- Android Gson解析json工具类封装
- Json数据封装到相应的实体类中(工具类)
- json数据与JAVA数据的转换 jsonJavaBean.netApache 自己编写了一个工具类,处理页面提交json格式数据到后台,再进行处理成JAVA对象数据 1、DTO:Data T
- java封装json格式工具类优化
- 捕获异常、存sd卡、自定义封装json(含网络工具类)、生成Json格式、传log日志到服务器、app崩溃友好重启