展示model

package com.example.aason.loginandreg.model;

import android.os.Handler;

import com.example.aason.loginandreg.api.CartApi;
import com.example.aason.loginandreg.contract.CartContract;
import com.example.aason.loginandreg.net.CartCallBack;

import java.io.IOException;
import java.util.HashMap;
import java.util.concurrent.TimeUnit;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import okhttp3.logging.HttpLoggingInterceptor;

public class CartModel implements CartContract.ICartModel {
Handler handler = new Handler();
@Override
public void getCart(HashMap map, final CartCallBack cartCallBack) {
HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
OkHttpClient okHttpClient = new OkHttpClient.Builder()
.addInterceptor(interceptor)
.build();
Request request = new Request.Builder().url(CartApi.BASE_API).build();
okHttpClient.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
if (cartCallBack != null) {
handler.post(new Runnable() {
@Override
public void run() {
cartCallBack.fail(“网络连接超时”);
}
});
}
}

        @Override
        public void onResponse(Call call, Response response) throws IOException {
            final String result = response.body().string();
            if (cartCallBack != null) {
                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        cartCallBack.success(result);
                    }
                });
            }
        }
    });
}

}

你可能感兴趣的:(展示model)