//写入注解
@GET
Call<Bean> Show(@Url String url,@QueryMap HashMap<String,String> params);
RetrofitUtils 工具类
public class RetrofitUtils {
private static RetrofitUtils mInstance;
private Retrofit retrofit;
/**
* 私有方法
*/
private RetrofitUtils(){
HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
interceptor.setLevel(HttpLoggingInterceptor.Level.BASIC);
OkHttpClient okHttpClient = new OkHttpClient.Builder()
.addNetworkInterceptor(interceptor)
.addInterceptor(interceptor)
.build();
retrofit = new Retrofit.Builder()
.baseUrl("http://172.17.8.100/")
.addConverterFactory(GsonConverterFactory.create())
.client(okHttpClient)
.build();
}
public static RetrofitUtils getmInstance() {
if(mInstance == null){
synchronized (RetrofitUtils.class){
mInstance = new RetrofitUtils();
}
}
return mInstance;
}
public Api api(){
return retrofit.create(Api.class);
}
}
model层
public class SearchModel implements SearchConstract.SerachModel {
@Override
public void getSearchModel(String url, HashMap<String, String> params, final SearchConstract.ModelCallBack modelCallBack) {
Api api = RetrofitUtils.getmInstance().api();
Call<Bean> beanCall = api.Show(url, params);
beanCall.enqueue(new Callback<Bean>() {
@Override
public void onResponse(Call<Bean> call, Response<Bean> response) {
if(modelCallBack != null){
modelCallBack.Success(response.body().getResult());
}
}
@Override
public void onFailure(Call<Bean> call, Throwable t) {
if(modelCallBack != null){
modelCallBack.FaulUre(t.getMessage());
}
}
});
}
}
mianActivity
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
----点击搜索------按照关键字搜索----------
sousuo.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String s = edit.getText().toString();
HashMap<String,String> params = new HashMap<>();
params.put("keyword",s);
params.put("page","1");
params.put("count","10");
searchPresenter.getSearchPresenter(ApiUrl.USER_SP,params);
}
});
searchPresenter = new SearchPresenter(this);
}
执行成功
@Override
public void Success(List<Bean.ResultBean> beanList) {
mrecyclerview.setLayoutManager(new GridLayoutManager(this,2));
adapter = new Adapter(this,beanList);
mrecyclerview.setAdapter(adapter);
}