1.写根接口
//写入跟接口
public static final String Base_Url="http://172.17.8.100/";
2.创建接口写子布局
public interface ServiceApi {
@GET("small/commodity/v1/commodityList")
Call<ResponseBody> getResponseData();
}
3.在M层可写的代码
public void containResponseData(final MyCallBack myCallBack) {
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(Contain.Base_Url)
.build();
ServiceApi serviceApi = retrofit.create(ServiceApi.class);
Call<ResponseBody> call = serviceApi.getResponseData();
call.enqueue(new Callback<ResponseBody>() {
@Override
public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
try {
String json = response.body().string();
Gson gson = new Gson();
ShoppingBean shoppingBean = gson.fromJson(json, ShoppingBean.class);
myCallBack.responseData(shoppingBean);
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public void onFailure(Call<ResponseBody> call, Throwable t) {
}
});
}