retrofit的使用,

xiaoxiao2021-02-27  368

官网:http://square.github.io/retrofit/

1、定义接口:

public interface SentenceService { @GET("/meitumeiju/") Call<ResponseBody> loadMeiju( @Query("page") String page); @GET("/meitumeiju/{type}/") Call<ResponseBody> loadMeiju(@Path("type") String type, @Query("page") String page); }

2、创建一个Refrofit对象

Retrofit retrofit=new Retrofit.Builder().baseUrl(baseUrl).build();

3、获取API接口的实现类的实例对象

SentenceService sentenceService=retrofit.create(SentenceService .class);

4、调用请求方法,并得到Call实例

Call<ResponseBody> call = null; if (type==null){ call = sentenceService.loadMeiju(page); }else { call = sentenceService.loadMeiju(type,page); }

5、使用Call实例完成异步请求

call.enqueue(new Callback<ResponseBody>() { @Override public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) { if(response!=null&&response.body()!=null){ //进行相应的数据处理 mListener.onSuccess(sceneListDetail); } } @Override public void onFailure(Call<ResponseBody> call, Throwable t) { mListener.onError(t); } }); }
转载请注明原文地址: https://www.6miu.com/read-1719.html

最新回复(0)