package com.example.beijing_daxing.base;

import android.app.ProgressDialog;
import android.content.Context;
import android.util.Log;
import android.widget.Toast;


import com.google.gson.Gson;
import com.lzy.okgo.callback.AbsCallback;
import com.lzy.okgo.model.Response;
import com.lzy.okgo.request.base.Request;

import okhttp3.ResponseBody;

/**
 * @author wanghao
 * @date 2023/11/20  14:27
 */
public class MyRecultCall extends AbsCallback<MyResult> {
    private ProgressDialog dialog;
    private Context context;

    public MyRecultCall(ProgressDialog dialog, Context context) {
        this.dialog = dialog;
        this.context = context;
    }

    @Override
    public MyResult convertResponse(okhttp3.Response response) throws Throwable {
        ResponseBody body = response.body();
        if (body == null) return null;
        Gson gson = new Gson();
        MyResult resust = gson.fromJson(body.string(),  MyResult.class);
        resust.setData(gson.toJson(resust.getData()));
        return resust;
    }

    @Override
    public void onSuccess(Response<MyResult> response) {
        dialog.dismiss();
        // Log.e("网络请求情况", "onSuccess:");
    }

    @Override
    public void onError(Response<MyResult> response) {
        super.onError(response);
        dialog.dismiss();
        Log.e("网络请求情况", "onError:" );
        Toast.makeText(context, "网络连接错误", Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onStart(Request<MyResult, ? extends Request> request) {
        super.onStart(request);
        dialog.show();
        // Log.e("网络请求情况", "onStart:" );
    }

    @Override
    public void onFinish() {
        super.onFinish();
        dialog.dismiss();
        Log.e("网络请求情况", "onFinish:" );
    }
}
