网络请求方式
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>()