자신 이 Jackson 을 봉인 하 는 도구 류 - JSonUtil
3676 단어 자바
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());
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Is Eclipse IDE dying?In 2014 the Eclipse IDE is the leading development environment for Java with a market share of approximately 65%. but ac...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.