第一次JSON回复
{
status: "SUCCESS"
errorCode: "0000"
errorMessage: " Login successfully . "
resultType: " user "
result: {
accountId: 1
createdOn: {...}-
id: 0
modifiedOn: {...}-
userEmailAddress: "ramesh.golla3"
userFirstName: "Ramesh"
userId: 8
userLastName: "golla"
userToken: "JbRl3grUNBYeq44l"
}-
}
// 2nd JSON resp
{
status: "SUCCESS"
errorCode: "0000"
errorMessage: ""
resultType: " account list "
result: [17]
0: {
address1: ""
address2: ""
address3: ""
city: ""
companyName: ""
companyPhoneNumber1: 0
companyPhoneNumber2: 0
companyWebsite: ""
country: ""
createdOn: {
date: 18
day: 2
hours: 12
minutes: 32
month: 1
nanos: 0
seconds: 18
time: 1392706938000
timezoneOffset: -330
year: 114
}-
id: 1
state: ""
status: 1
zipCode: ""
}
为所有回复制作通用类
private String status;
private String errorCode;
private String errorMessage;
private String resultType;
private Object result;
将对象强制转换为必要的模型,但是当我将其转换为对象时,我将对象强制转换为UserPojo。我用spring做这个
java.lang.ClassCastException:java.util.LinkedHashMap无法强制转换为in.qlc.model.User
asynctask
class LoginTask extends AsyncTask<Void, String, String> {
String result;
ResponseEntity<UserResult> responseEntity;
@Override
protected String doInBackground(Void... params) {
// / The connection URL
String url = Constants.AUTHENTICATE_URL;
Log.e("Main", "do in background ");
UserSend te = new UserSend();
te.setPassword("ramesh@123");
te.setUserEmailAddress("ramesh.golla3");
//
HttpHeaders requestHeaders = new HttpHeaders();
requestHeaders.setContentType(MediaType.APPLICATION_JSON);
// Populate the headers in an HttpEntity object to use for the
// request
HttpEntity requestEntity = new HttpEntity(te, requestHeaders);
// Create a new RestTemplate instance
RestTemplate restTemplate = new RestTemplate();
restTemplate.getMessageConverters().add(
new MappingJackson2HttpMessageConverter());
// Perform the HTTP GET request
responseEntity = restTemplate.exchange(url, HttpMethod.POST,
requestEntity, UserResult.class);
Log.e("resopnse ", " = " + responseEntity.getBody());
return result;
}
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
User user = ((User) responseEntity.getBody().getResult());
Log.e("Main", "onPostExecute "
+ responseEntity.getBody().getResult());
}
}
模型类
@Root
@JsonIgnoreProperties(ignoreUnknown = true)
public class UserResult {
private String status;
private String errorCode;
private String errorMessage;
private String resultType;
private Object result;
// private User result;
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
public String getErrorCode() {
return errorCode;
}
public void setErrorCode(String errorCode) {
this.errorCode = errorCode;
}
public String getErrorMessage() {
return errorMessage;
}
public void setErrorMessage(String errorMessage) {
this.errorMessage = errorMessage;
}
public String getResultType() {
return resultType;
}
public void setResultType(String resultType) {
this.resultType = resultType;
}
public Object getResult() {
return result;
}
public void setResult(Object result) {
this.result = result;
}
}
错误:
02-26 14:38:27.697:E / AndroidRuntime(28840):致命异常:主要 02-26 14:38:27.697:E / AndroidRuntime(28840): java.lang.ClassCastException:无法强制转换java.util.LinkedHashMap to in.qlc.model.User 02-26 14:38:27.697:E / AndroidRuntime(28840):at in.qlc.field.sense.LoginActivity $ LoginTask.onPostExecute(LoginActivity.java:113) 02-26 14:38:27.697:E / AndroidRuntime(28840):at in.qlc.field.sense.LoginActivity $ LoginTask.onPostExecute(LoginActivity.java:1) 02-26 14:38:27.697:E / AndroidRuntime(28840):at android.os.AsyncTask.finish(AsyncTask.java:602)02-26 14:38:27.697: E / AndroidRuntime(28840):at android.os.AsyncTask.access $ 600(AsyncTask.java:156)02-26 14:38:27.697:E / AndroidRuntime(28840):at android.os.AsyncTask $ InternalHandler.handleMessage(AsyncTask.java:615) 02-26 14:38:27.697:E / AndroidRuntime(28840):at android.os.Handler.dispatchMessage(Handler.java:99)02-26 14:38:27.697:E / AndroidRuntime(28840):at android.os.Looper.loop(Looper.java:137)02-26 14:38:27.697: E / AndroidRuntime(28840):at android.app.ActivityThread.main(ActivityThread.java:4424)02-26 14:38:27.697:E / AndroidRuntime(28840):at java.lang.reflect.Method.invokeNative(Native Method)02-26 14:38:27.697:E / AndroidRuntime(28840):at java.lang.reflect.Method.invoke(Method.java:511)02-26 14:38:27.697: E / AndroidRuntime(28840):at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:825) 02-26 14:38:27.697:E / AndroidRuntime(28840):at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:592)02-26 14:38:27.697:E / AndroidRuntime(28840):at dalvik.system.NativeStart.main(Native Method)02-26 14:38:27.727: E / InputDispatcher(692):channel'b342acc8 in.qlc.fieldsense / in.qlc.field.sense.LoginActivity(server)'~Channel 是不可恢复的破碎,将被处置! 02-26 14:38:28.217: E / gpop.InstalledAppsGrid(861):隐藏列表大小21