JsonHttpResponseHandler 工具

请求处理基本类

public class BaseResponseHandler extends JsonHttpResponseHandler {
    Context context;
    LoadingDialog loadingDialog;
    PullToRefreshListView listview;
    PullToRefreshScrollView scrollView;
    private int remindType = 0;
    private boolean showLoading;
    public final static int NOTING = 0;
    public final static int TOAST = 1;
    public final static int DIALOG = 2;
    public final static int UPDATE = 3;

    public BaseResponseHandler(Context context, boolean showLoading,
            int remindType) {
        this.context = context;
        loadingDialog = new LoadingDialog(context);
        loadingDialog.setCancelable(false);
        this.showLoading = showLoading;
        this.remindType = remindType;
    }

    public BaseResponseHandler(Context context, boolean showLoading,
            int remindType, PullToRefreshListView listView) {
        this(context, showLoading, remindType);
        this.listview = listView;
    }

    public BaseResponseHandler(Context context, boolean showLoading,
            int remindType, PullToRefreshScrollView scrollView) {
        this(context, showLoading, remindType);
        this.scrollView = scrollView;
    }

    @Override
    public void onFailure(int statusCode, Header[] headers,
            String responseString, Throwable throwable) {
        // TODO Auto-generated method stub
        super.onFailure(statusCode, headers, responseString, throwable);
        throwable.printStackTrace();
        handFail();
    }

    @Override
    public void onFailure(int statusCode, Header[] headers,
            Throwable throwable, JSONArray errorResponse) {
        // TODO Auto-generated method stub
        super.onFailure(statusCode, headers, throwable, errorResponse);
        handFail();
    }

    @Override
    public void onFailure(int statusCode, Header[] headers,
            Throwable throwable, JSONObject errorResponse) {
        // TODO Auto-generated method stub
        super.onFailure(statusCode, headers, throwable, errorResponse);
        handFail();
    }

    @Override
    public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
        // TODO Auto-generated method stub
        Log.i("response", response.toString());
        try {
            if (response.getInt("resultCode")==2) {
                new MyAlertDialog(context, response.getString("resultMsg")).show();
            }
        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    @Override
    public void onStart() {
        // TODO Auto-generated method stub
        super.onStart();
        if (showLoading)
            loadingDialog.show();
    }

    @Override
    public void onFinish() {
        // TODO Auto-generated method stub
        super.onFinish();
        loadingDialog.dismiss();
    }

    private void handFail() {
        if (remindType == BaseResponseHandler.TOAST)
            ToastUtils.showToast(context, "网络错误");
        if (remindType == BaseResponseHandler.UPDATE){
            ToastUtils.showToast(context, "检查更新失败");
            Intent intent = new Intent(context,
                    LoginActivity.class);
            context.startActivity(intent);
            LauncherActivity.instance.finish();
        }
        if (listview != null)
            listview.onRefreshComplete();
        if (scrollView != null)
            scrollView.onRefreshComplete();
    }
}

加载框

public class LoadingDialog extends Dialog {

    Context context;

    public LoadingDialog(Context context) {
        super(context, R.style.loadingdialog);
        this.context = context;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_loading);
        this.setOnShowListener(new OnShowListener() {
            @Override
            public void onShow(DialogInterface dialog) {
                findViewById(R.id.img_loading).startAnimation(
                        AnimationUtils.loadAnimation(context, R.anim.rotate));
            }
        });
    }

    public void setMessage(String message) {
        ((TextView) findViewById(R.id.txt_message)).setText(message);
    }
}

posted @ 2015-12-10 18:21  蜗牛眼里的世界  阅读(509)  评论(0编辑  收藏  举报