Api+GreenDao+Retrofit

//網絡請求方式

public interface Api {
    @GET("%E7%A6%8F%E5%88%A9/10/{prae}")
    Call<Good> getNoParmars1(@Path("prae") int prae);
}

//接口Api 定義接口使用的

public  class  API_url {
    public static String Good_url="http://gank.io/api/data/";
}

//封裝的一個Retrofit方法框架(也是獲取代理對象的類)

public class Retrofit_view {


    private static Retrofit retrofit;

    public static Retrofit getreoefit(String url) {
        HttpLoggingInterceptor httpLoggingInterceptor = new HttpLoggingInterceptor(new HttpLoggingInterceptor.Logger() {
            @Override
            public void log(String message) {
                Log.d("咕咕咕咕", message);
            }
        });
        httpLoggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);

        OkHttpClient okHttpClient = new OkHttpClient.Builder().addInterceptor(httpLoggingInterceptor).build();

        retrofit = new Retrofit.Builder().client(okHttpClient).addConverterFactory(GsonConverterFactory.create())
                .baseUrl(url).build();
        return retrofit;
    }
    public static <T> T getApi(String url,Class<T> cla){
        Retrofit getreoefit = getreoefit(url);
        T t = getreoefit.create(cla);
        return t;
    };

//網絡判斷是否是有網的狀態

public class Weri_sercise {

    public static boolean getsercise(Context context){
       ConnectivityManager systemService = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo activeNetworkInfo = systemService.getActiveNetworkInfo();
        if (activeNetworkInfo!=null){
            return true;
        }
        return false;
    }

//這是一個實現類

public class MaApp extends Application {
    public static MaApp instances;
    private DaoSession daoSession;
    private SQLiteDatabase writableDatabase;
    private DaoMaster.DevOpenHelper uers;
    private DaoMaster daoMaster;

    @Override
    public void onCreate() {
        super.onCreate();
        instances = this;
        setDatabase();
    }

    public static MaApp getInstances() {
        return instances;
    }

    private void setDatabase() {
        uers = new DaoMaster.DevOpenHelper(this, "uers-db", null);
        writableDatabase = uers.getWritableDatabase();
        daoMaster = new DaoMaster(writableDatabase);
        daoSession = daoMaster.newSession();
    }

    public DaoSession setDaoSession() {
        return daoSession;
    }

    public SQLiteDatabase setWritableDatabase() {
        return writableDatabase;
    }

//這是獲取代理對象

Api api1 = Retrofit_view.getApi(url, Api.class);

//這是獲取Retrofit對象(也可以成爲是獲取數據庫)
resultsBeanDao = MaApp.getInstances().setDaoSession().getResultsBeanDao();

//獲取Call對象
Call<Good> noParmars1 = api1.getNoParmars1(prae);

//異步請求
noParmars1.enqueue(new Callback<Good>() 

發佈了49 篇原創文章 · 獲贊 60 · 訪問量 6萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章