MVP+retrofit+okhttp+RxJava框架

工具类
public class HttpUtils {

public static OkHttpClient okHttpClient;

public final LoginApi api;

//私有化构造方法
private HttpUtils() {
    //创建okhttpclient对象
    okHttpClient = new OkHttpClient.Builder()
            .connectTimeout(5, TimeUnit.SECONDS)
            .readTimeout(5, TimeUnit.SECONDS)
            .addNetworkInterceptor(new myInceptor())
            .build();
    //创建retrofit对象
    Retrofit retrofit = new Retrofit.Builder()
            .addConverterFactory(GsonConverterFactory.create())
            .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
            //http://mobile.bwstudent.com/small/
           // http://172.17.8.100
           // .baseUrl("http://mobile.bwstudent.com")
            .baseUrl("http://172.17.8.100/")
            .build();
    //实例化接口
    api = retrofit.create(LoginApi.class);
}
//单例
public static HttpUtils getInstance() {
    return HttpUtilsInstnce.httpUtils;
}
private static class HttpUtilsInstnce {
    private static HttpUtils httpUtils = new HttpUtils();
}
//拦截器
private static class myInceptor implements Interceptor {
    @Override
    public Response intercept(Chain chain) throws IOException {
        Request request = chain.request();
        RequestBody requestBody = request.body();
        Headers headers = request.headers();
        Response response = chain.proceed(request);
        Headers headers1 = response.headers();
        return response;
    }
}

}
//p层请求数据
public class MainPresenter extends BasePresenter {

private final HttpUtils instace;
private ShopBean shopBean1;
 private MainView iMainView;
//调用HttpUtils
public MainPresenter(MainView iMainView){
    instace = HttpUtils.getInstace();
    this.iMainView=iMainView;
}
public void getData(){
    //商品
    Observable shop = instace.api.getShop();
    shop.subscribeOn(Schedulers.io())//请求在io线程
            .observeOn(AndroidSchedulers.mainThread())//请求完成后在主线程
            .subscribe(new Observer() {//订阅
                @Override
                public void onCompleted() {
                    //getView().onShopBean(shopBean1);
                    iMainView.onShopBean(shopBean1);
                }

                @Override
                public void onError(Throwable e) {
                    e.printStackTrace();
                    getView().onError("请求失败!");
                }

                @Override
                public void onNext(ShopBean shopBean) {
                    shopBean1 = shopBean;
                }
            });
            }

v层
mainPresenter = new MainPresenter(this);
mainPresenter.setView(this);
mainPresenter.getData();
///
@Override
public void onShopBean(ShopBean shopBean){
rxAdapter = new RxAdapter(getActivity());
recyclerView.setAdapter(rxAdapter);
//布局管理器
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity());
linearLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
recyclerView.setLayoutManager(linearLayoutManager);
//数据
List commodityList = shopBean.getResult().getRxxp().getCommodityList();
rxAdapter.setRxxp(commodityList);
}
///////
public interface MainView extends BaseView {
void onShopBean(ShopBean shopBean);
void onBannerBean(BannerBean bannerBean);
// void onParticulars(ShopParticularsBean shopParticularsBean);
void onError(String result);
}

//////////适配器
public class RxAdapter extends RecyclerView.Adapter {
private Context context;
public RxAdapter(Context context) {
this.context = context;
}
private ArrayList list = new ArrayList<>();
@NonNull
@Override
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
View view = LayoutInflater.from(context).inflate(R.layout.home_item1, null);
ViewHolder viewHolder = new ViewHolder(view);
return viewHolder;
}
@Override
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, int i) {
ViewHolder viewHolder1 = (ViewHolder) viewHolder;
viewHolder1.title1.setText(list.get(i).getCommodityName());
viewHolder1.price1.setText(“¥”+list.get(i).getPrice());
Glide.with(context).load(list.get(i).getMasterPic()).into(viewHolder1.img1);
}
@Override
public int getItemCount() {
return list.size();
}

public void setRxxp(List commodityList) {
    if(commodityList!=null&&commodityList.size()>0){
        this.list.clear();
        this.list.addAll(commodityList);
        notifyDataSetChanged();
    }
}
public class ViewHolder extends RecyclerView.ViewHolder {
    private ImageView img1;
    private TextView title1,price1;
    public ViewHolder(@NonNull View itemView) {
        super(itemView);
        img1 = itemView.findViewById(R.id.img1);
        title1 = itemView.findViewById(R.id.title1);
        price1 = itemView.findViewById(R.id.price1);
    }
}

}

//Frasco
//初始化
Fresco.initialize(this);
//适配器
viewHolder.item_img.setImageURI(datas.get(position).getPicUrl());
//布局里
android:id="@+id/item_img"
android:layout_width=“120dp”
android:layout_height=“120dp”>

你可能感兴趣的:(MVP+retrofit+okhttp+RxJava框架)