封装Retrofit,RXJAVA,okhttp网络请求的工具

package com.example.weekly_dome.retrofit;

import com.example.weekly_dome.bean.NewsBean;
import com.example.weekly_dome.view.MView;

import java.util.Map;

import okhttp3.OkHttpClient;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * Created by z on 2017/12/30.
 */

public class RetrofitUtils {
    private static volatile RetrofitUtils instance;
    private final Retrofit retrofit;
    private ApiService apiService;

    public RetrofitUtils(String baseUrl){
        retrofit = new Retrofit.Builder()
                .addConverterFactory(GsonConverterFactory.create())
                .client(new OkHttpClient())
                .baseUrl(baseUrl)
                .build();
    }

    public static RetrofitUtils getInstance(String baseUrl){
        if(null==instance){
            synchronized (RetrofitUtils.class){
                if(instance==null){
                    instance=new RetrofitUtils(baseUrl);
                }
            }
        }
        return instance;
    }
public void getNews(final MView mView, Map map){
    apiService = retrofit.create(ApiService.class);
    apiService.getData(map).enqueue(new Callback() {
        @Override
        public void onResponse(Call call, Response response) {
            NewsBean body = response.body();
            mView.onSueecss(body);
        }

        @Override
        public void onFailure(Call call, Throwable t) {
            mView.onFailed(t);
        }
    });
}

}

你可能感兴趣的:(android)