RetrofitUtils
private final OkHttpClient okHttpClient;
private static RetrofitUtils mInstance;
private final Retrofit retrofit;
public RetrofitUtils() {
final HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor();
loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
okHttpClient = new OkHttpClient.Builder()
.addInterceptor(loggingInterceptor)
.connectTimeout(5,TimeUnit.SECONDS)
.readTimeout(5,TimeUnit.SECONDS)
.writeTimeout(5,TimeUnit.SECONDS)
.build();
retrofit = new Retrofit.Builder()
.baseUrl(UserApi.BASE_API)
.addConverterFactory(GsonConverterFactory.create())
.client(okHttpClient)
.build();
}
public static RetrofitUtils getmInstance() {
if(mInstance==null){
synchronized (RetrofitUtils.class){
mInstance=new RetrofitUtils();
}
}
return mInstance;
}
public void setGoods(String api, HashMap<String,String> params, final RetrofitCallback retrofitCallback){
//模式:外观模式
//设计模式:构建者模式
//第二步 ,创建请求接口类对象,体现一个动态代理模式
final UserapiService userapiService = retrofit.create(UserapiService.class);
//第三步,创建请求对象
final Call reqs = userapiService.reqs(api, params);
reqs.enqueue(new Callback() {
@Override
public void onResponse(Call call, Response response) {
final UserBean body = response.body();
retrofitCallback.onsuccess(body);
}
@Override
public void onFailure(Call<UserBean> call, Throwable t) {
retrofitCallback.onfailure("网络错误");
}
});
}
api
@GET
Call reqs(@Url String api, @QueryMap HashMap<String,String> params);
@POST
@FormUrlEncoded
Call req(@Url String api, @FieldMap HashMap<String,String> params);
Model
@Override
public void getGoodsList(String api, HashMap<String, String> params, RetrofitCallback retrofitCallback) {
RetrofitUtils.getmInstance().setGoods(api,params,retrofitCallback );
}
activity
public class FragmentOne extends Fragment implements Contract.CView,XRecyclerView.LoadingListener,ItemoneAdapter.GoodsCallback {
private XRecyclerView xrv;
private CPresenter cPresenter;
private int page=1;
private int count=10;
private ItemoneAdapter adapter;
private SearchView searchview;
private HashMap<String, String> params;
private Button sou;
private EditText namee;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
final View view = inflater.inflate(R.layout.fragmentone, container, false);
xrv =view.findViewById(R.id.xrv);
sou =view.findViewById(R.id.sou);
namee =view.findViewById(R.id.namee);
// searchview =view.findViewById(R.id.searchview);
// searchview.setSearchCalBack(this);
xrv.setLayoutManager(new GridLayoutManager(getActivity(),2));
cPresenter = new CPresenter(this);
adapter = new ItemoneAdapter(getActivity());
xrv.setAdapter(adapter);
//刷新加载
xrv.setLoadingListener(this);
//点击事件
adapter.setGoodsCallback(this);
sou.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
final String s = namee.getText().toString();
params = new HashMap<>();
params.put("keyword",s);
params.put("page",page+"");
params.put("count",count+"");
cPresenter.getGoodsList(UserApi.GOODS_API, params);
}
});
return view;
}
@Override
public void onSuccess(Object o) {
final UserBean goods = (UserBean) o;
Toast.makeText(getActivity(),"成功",Toast.LENGTH_LONG).show();
final List<UserBean.ResultBean> list = goods.getResult();
adapter.setList(list);
}
@Override
public void onFailure(String msg) {
Toast.makeText(getActivity(),msg,Toast.LENGTH_SHORT).show();
}
@Override
public void onRefresh() {
params.put("page",page+"");
cPresenter.getGoodsList(UserApi.GOODS_API,params);
xrv.refreshComplete();
}
@Override
public void onLoadMore() {
params.put("page",page+"");
cPresenter.getGoodsList(UserApi.GOODS_API,params);
xrv.loadMoreComplete();
}
/* @Override
public void getGoodsName(String name) {
if(name!=null&&!"".equals(name)){
page=1;
params.put(“keyword”,name);
params.put(“page”,page+"");
cPresenter.getGoodsList(UserApi.GOODS_API, params);
}else{
Toast.makeText(getActivity(), “输入商品信息为空,请重新输入”, Toast.LENGTH_LONG).show();
}
}*/
@Override
public void getCommodityId(String position) {
EventBus.getDefault().postSticky(position);
startActivity(new Intent(getActivity(),Main3Activity.class));
}