package com.bozaixing.mobeweather.http.response;

import android.os.Handler;
import android.os.Looper;

import com.bozaixing.mobeweather.http.listener.DisposeDataListener;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

/**
 * Descr:   回调数据的处理
 * Author:  bozaixing.
 * Date:    2017-03-29.
 * Email:   654152983@qq.com.
 */

public class CommonCallback implements Callback {

    // 创建回调监听对象
    private DisposeDataListener mDisposeDataListener;

    // 创建线程事务管理handler的对象
    private Handler mHandler;


    /**
     * 构造方法
     * @param disposeDataListener
     */
    public CommonCallback(DisposeDataListener disposeDataListener){
        // init
        mDisposeDataListener = disposeDataListener;

        // 初始化handler对象并将handler添加到主线程中
        mHandler = new Handler(Looper.getMainLooper());

    }


    @Override
    public void onFailure(Call call, final IOException e) {

        mHandler.post(new Runnable() {
            @Override
            public void run() {
                mDisposeDataListener.onFailure(e);
            }
        });

    }

    @Override
    public void onResponse(Call call, final Response response) throws IOException {
        mHandler.post(new Runnable() {
            @Override
            public void run() {
                mDisposeDataListener.onSuccess(response);
            }
        });
    }
}
