67,513
社区成员
发帖
与我相关
我的任务
分享
T retObj = JSON.parseObject(response.body().string(), new TypeReference<T>(){});
T retObj = (T) JSON.parseObject(response.body().string(), new TypeReference<AccessResult>(){});
public class AccessResult {
private Boolean result;
private String status;
protected Object responseBody;
public void setResult(Boolean result){
this.result = result;
}
public Boolean getResult(){
return this.result;
}
public void setStatus(String status) {
this.status = status;
}
public String getStatus() {
return this.status;
}
protected void setResponseBody(Object responseBody) {
this.responseBody = responseBody;
}
protected Object getResponseBody() {
return this.responseBody;
}
}
public class UserAccessResult extends AccessResult {
private User user = (User) responseBody;
public void setUser(User user) {
this.user = user;
}
public User getUser() {
return this.user;
}
}
public static <T extends AccessResult> T post(Object params, String url){
……
// 初始化返回对象
T retObj = null;
try {
//创建调用对象,重写回调函数
Response response = okHttpClient.newCall(request).execute();
if (response.isSuccessful()){
// success
retObj = JSON.parseObject(response.body().string(), new TypeReference<T>(){});
response.body().close();
}
else {
response.body().close();
throw new IOException("Unexpected code " + response);
}
} catch (NullPointerException | IOException e){
e.printStackTrace();
} catch (Exception e){
e.printStackTrace();
} finally {
return retObj;
}
}
UserAccessResult result = NetCommunication.post(u, getString(R.string.server_host_url) + "/login");
if (result != null && result.getResult()) {
app.setCurrentUser(result.getUser());
return true;
} else return false;
UserAccessResult result = NetCommunication.post(u, getString(R.string.server_host_url) + "/login", new UserAccessResult());
public static <T extends AccessResult<? extends Object>> T post(Object params, String url, T ret){
……
if (response.isSuccessful()){
//success
ret = (T) JSON.parseObject(response.body().string(), ret.getClass());
response.body().close();
}
……
}