/**
* 异步响应结果
* @author jiangsl
*
*/
public class Response implements Serializable{
/**
*
*/
private static final long serialVersionUID = 1L;
/**
* 请求是否成功
*/
boolean success;
/**
* 结果提示信息或数据
*/
String descriptor;
/**
* 结果数据
*/
HashMap<String, Object> map;
public Response(){
map = new HashMap<String, Object>();
}
public boolean isSuccess() {
return success;
}
public void setSuccess(boolean success) {
this.success = success;
}
public String getDescriptor() {
return descriptor;
}
public void setDescriptor(String descriptor) {
this.descriptor = descriptor;
}
public void put(String key, Object value){
map.put(key, value);
}
public Object get(String key){
return map.get(key);
}
public HashMap<String, Object> getMap() {
return map;
}
public void setMap(HashMap<String, Object> map) {
this.map = map;
}
}
private class LoadingTask extends AsyncTask<Integer, Integer, Response> {
@Override
protected void onPreExecute() {
super.onPreExecute();
list.clear();
DialogUtils.getInstance(context).getProgressDialog(context.getString(R.string.loading), true);
}
@Override
protected Response doInBackground(Integer... params) {
Response response = new Response();
response.setSuccess(false);
if (NetworkUtils.isNetworkConnected(context)) {
try {
String result = HttpUserService.getUsers(Config.getUserId(context));
if (result != null) {
JSONObject obj = new JSONObject(result);
if (obj.getInt("code") == 2) {
JSONObject resObj = obj.getJSONObject("result");
JSONArray array = resObj.getJSONArray("list");
for (int i = 0; i < array.length(); i++) {
JSONObject object = array.getJSONObject(i);
User user = new User();
user.parseJson(object);
list.add(user);
};
response.setSuccess(true);
} else {
response.setSuccess(false);
response.setDescriptor(obj.getString("result"));
}
} else {
response.setSuccess(false);
response.setDescriptor(context.getString(R.string.cannot_connection_server));
}
} catch (Exception e) {
e.printStackTrace();
response.setDescriptor(ExceptionManager.getErrorDesc(context, e));
}
} else {
response.setDescriptor(context.getString(R.string.no_connection));
}
return response;
}
@Override
protected void onPostExecute(Response result) {
super.onPostExecute(result);
DialogUtils.getInstance(context).dismissProgressDialog();
if (!isAdded()) {
return;
}
if (result.isSuccess()) {
adapter.notifyDataSetChanged();
}else{
toast(result.getDescriptor());
}
}
}
public class ExceptionManager {
/**
* 显示错误提示
* @param e
*/
public static String getErrorDesc(Context context, Exception e){
if(e instanceof NetworkStatusLineException){
return context.getString(R.string.exception_statusline) + e.getMessage();
}else if(e instanceof JSONException){
return context.getString(R.string.exception_json) + e.getMessage();
}else if(e instanceof UnknownHostException){
return context.getString(R.string.exception_unknownhost);
}else if(e instanceof SocketException){
return context.getString(R.string.exception_socket) + e.getMessage();
}else if(e instanceof SocketTimeoutException){
return context.getString(R.string.exception_sockettimeout);
}else if(e instanceof ConnectTimeoutException){
return context.getString(R.string.exception_connecttimeout);
}else if(e instanceof IOException){
return context.getString(R.string.exception_io);
}else if(e instanceof IllegalArgumentException){
return context.getString(R.string.exception_illegalargument);
}
return context.getString(R.string.exception_unknownhost) + e.getMessage();
}
}
public class DataFormatException extends Exception {
private static final long serialVersionUID = 1L;
public DataFormatException(String valueOf) {
super(valueOf);
}
}