android异步任务实现列表数据加载

/**
 * 异步响应结果
 * @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);
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值