//首先就是要导包
compile 'com.squareup.retrofit2:retrofit:2.3.0'
//RetrofitHelper
package com.example.xzhaogao01.netWork;
import okhttp3.OkHttpClient;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
/**
* Created by 绅丶士 on 2017/12/2.
*/
public class RetrofitHelper {
private static OkHttpClient okHttpClient;
private static ServiceAPL serviceAPL;
static {
ininOkHttpClient();
}
private static void ininOkHttpClient(){
if(okHttpClient==null){
synchronized (RetrofitHelper.class){
if(okHttpClient==null){
okHttpClient=new OkHttpClient.Builder().build();
}
}
}
}
public static ServiceAPL getAPI(){
if(serviceAPL==null){
synchronized (ServiceAPL.class){
if(serviceAPL==null){
serviceAPL=RetrofitHelper.createAPI(ServiceAPL.class,UrlUtils.BASE_HOST_URL);
}
}
}
return serviceAPL;
}
/**
*
* @param clazz
* @param url
* @param <T>
* @return
*/
public static <T> T createAPI(Class<T> clazz,String url){
Retrofit retrofit=new Retrofit.Builder().baseUrl(UrlUtils.BASE_HOST_URL)
.addConverterFactory(GsonConverterFactory.create())
.build();
return retrofit.create(clazz);
}
}
//ServiceAPL
public interface ServiceAPL {
@GET(UrlUtils.TAGS)
Call<UserBean> tabs();
}
UrlUtils
public class UrlUtils {
public static final String BASE_HOST_URL = "http://gank.io/api/";
public static final String TAGS = "data/Android/10/1";
}
//调用
public class ShowYeFr extends Fragment{
UserDao userDao;
private RecyclerView lv;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view=inflater.inflate(R.layout.fr_item01,container,false);
lv = view.findViewById(R.id.Rlv);
//设置布局管理器
lv.setLayoutManager(new LinearLayoutManager(getContext()));
//判断网络是否连接
IFWork ifWork = new IFWork();
//添加分割线
//lv.addItemDecoration(new RecycleViewDivider(getContext(), LinearLayoutManager.HORIZONTAL,5,getResources().getColor(R.color.colorPrimary)));
//判断网络
//动态代理
ServiceAPL api= RetrofitHelper.getAPI();
Call<UserBean> tags=api.tabs();
tags.enqueue(new Callback<UserBean>() {
@Override
public void onResponse(Call<UserBean> call, Response<UserBean> response) {
UserBean body = response.body();
List<UserBean.ResultsBean> results = body.getResults();
for (UserBean.ResultsBean r:
results ) {
userDao.insert(new User(System.currentTimeMillis(),r.getCreatedAt(),r.getDesc()));
}
//创建适配器
Myadap myadap = new Myadap(getActivity(), results);
lv.setAdapter(myadap);
}
@Override
public void onFailure(Call<UserBean> call, Throwable t) {
}
});
//初始化数据库
initDbHelp();
return view;
}
private void initDbHelp(){
DaoMaster.DevOpenHelper devOpenHelper = new DaoMaster.DevOpenHelper(getActivity(), "recluse-db", null);
SQLiteDatabase db = devOpenHelper.getWritableDatabase();
DaoMaster daoMaster = new DaoMaster(db);
DaoSession daoSession = daoMaster.newSession();
userDao=daoSession.getUserDao();
}
}
package com.example.xzhaogao01.netWork; import okhttp3.OkHttpClient; import retrofit2.Retrofit; import retrofit2.converter.gson.GsonConverterFactory; /** * Created by 绅丶士 on 2017/12/2. */ public class RetrofitHelper { private static OkHttpClient okHttpClient; private static ServiceAPL serviceAPL; static { ininOkHttpClient(); } private static void ininOkHttpClient(){ if(okHttpClient==null){ synchronized (RetrofitHelper.class){ if(okHttpClient==null){ okHttpClient=new OkHttpClient.Builder().build(); } } } } public static ServiceAPL getAPI(){ if(serviceAPL==null){ synchronized (ServiceAPL.class){ if(serviceAPL==null){ serviceAPL=RetrofitHelper.createAPI(ServiceAPL.class,UrlUtils.BASE_HOST_URL); } } } return serviceAPL; } /** * * @param clazz * @param url * @param <T> * @return */ public static <T> T createAPI(Class<T> clazz,String url){ Retrofit retrofit=new Retrofit.Builder().baseUrl(UrlUtils.BASE_HOST_URL) .addConverterFactory(GsonConverterFactory.create()) .build(); return retrofit.create(clazz); } }
//ServiceAPL
public interface ServiceAPL {
@GET(UrlUtils.TAGS)
Call<UserBean> tabs();
}
UrlUtils
public class UrlUtils {
public static final String BASE_HOST_URL = "http://gank.io/api/";
public static final String TAGS = "data/Android/10/1";
}
//调用
public interface ServiceAPL { @GET(UrlUtils.TAGS) Call<UserBean> tabs(); }
public class ShowYeFr extends Fragment{ UserDao userDao; private RecyclerView lv; @Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { View view=inflater.inflate(R.layout.fr_item01,container,false); lv = view.findViewById(R.id.Rlv); //设置布局管理器 lv.setLayoutManager(new LinearLayoutManager(getContext())); //判断网络是否连接 IFWork ifWork = new IFWork(); //添加分割线 //lv.addItemDecoration(new RecycleViewDivider(getContext(), LinearLayoutManager.HORIZONTAL,5,getResources().getColor(R.color.colorPrimary))); //判断网络 //动态代理 ServiceAPL api= RetrofitHelper.getAPI(); Call<UserBean> tags=api.tabs(); tags.enqueue(new Callback<UserBean>() { @Override public void onResponse(Call<UserBean> call, Response<UserBean> response) { UserBean body = response.body(); List<UserBean.ResultsBean> results = body.getResults(); for (UserBean.ResultsBean r: results ) { userDao.insert(new User(System.currentTimeMillis(),r.getCreatedAt(),r.getDesc())); } //创建适配器 Myadap myadap = new Myadap(getActivity(), results); lv.setAdapter(myadap); } @Override public void onFailure(Call<UserBean> call, Throwable t) { } }); //初始化数据库 initDbHelp(); return view; } private void initDbHelp(){ DaoMaster.DevOpenHelper devOpenHelper = new DaoMaster.DevOpenHelper(getActivity(), "recluse-db", null); SQLiteDatabase db = devOpenHelper.getWritableDatabase(); DaoMaster daoMaster = new DaoMaster(db); DaoSession daoSession = daoMaster.newSession(); userDao=daoSession.getUserDao(); } }