Retrofit 使用方法

public class MainActivity extends Activity { 
    private ListView mListView;
    
    //private ImageListAdapter adapter;
    private ImageListPicassoAdapter adapter;
    private Context mContext;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mContext=this;
        
       ResultApi.getBroadcastApi(1,"user_invitation_list",1,15, new ICallBack<ResultBean>() { 
            @Override
            public void onSuccess(String flag, String key, ResultBean resultBean) {
                //adapter=new ImageListAdapter(mContext, resultBean.getResult());
                adapter=new ImageListPicassoAdapter(mContext, resultBean.getResult());
                mListView.setAdapter(adapter);
            }
            @Override
            public void onFailure(String flag, String key, String why) {
            }
        });
       
        /*ResultApi.getBroadcastApiNoPar(new ICallBack<ResultBean>() { 
            @Override
            public void onSuccess(String flag, String key, ResultBean resultBean) {
                //adapter=new ImageListAdapter(mContext, resultBean.getResult());
                adapter=new ImageListPicassoAdapter(mContext, resultBean.getResult());
                mListView.setAdapter(adapter);
            }
            @Override
            public void onFailure(String flag, String key, String why) {
                 
            }
        
        });*/
        
    }
View Code
public class ResultApi {
    
    public static Call<ResultBean> getBroadcastApi(int ver,final String action,int page,int pageSize,final ICallBack<ResultBean> callBack){
        Call<ResultBean> callResultBean=BuildService.getMeiNvService().getBroadcast(ver, action, page, pageSize);
        callResultBean.enqueue(new Callback<ResultBean>() {
            
            @Override
            public void onResponse(Call<ResultBean> call, Response<ResultBean> response) {
                   if (response.isSuccessful()) {
                           ResultBean resultBean = response.body();
                          if (resultBean.getResult().size()>0) {
                              //数据正确,把数据返回
                              callBack.onSuccess(action, "", resultBean);
                          } else {
                              //数据错误
                              callBack.onFailure(action, "", "数据错误");
                          }
                   }
            }
            
            @Override
            public void onFailure(Call<ResultBean> call, Throwable t) {
                
            }
        });
        return callResultBean;
    }
    
    
    public static Call<ResultBean> getBroadcastApiNoPar(final ICallBack<ResultBean> callBack){
        Call<ResultBean> callResultBean=BuildService.getMeiNvService().getBroadcastNoPar();
        callResultBean.enqueue(new Callback<ResultBean>() {
            
            @Override
            public void onResponse(Call<ResultBean> call, Response<ResultBean> response) {
                   if (response.isSuccessful()) {
                           ResultBean resultBean = response.body();
                          if (resultBean.getResult().size()>0) {
                              //数据正确,把数据返回
                              callBack.onSuccess("", "", resultBean);
                          } else {
                              //数据错误
                              callBack.onFailure("", "", "数据错误");
                          }
                   }
            }
            
            @Override
            public void onFailure(Call<ResultBean> call, Throwable t) {
                
            }
        });
        return callResultBean;
    }     
}
View Code
public interface ApiService {
    //http://api.abc.com/WebApi/api.ashx?ver=1&action=user_invitation_list&page=1&page_size=10 
    @GET("api.ashx")
    Call<ResultBean>  getBroadcast(@Query("ver") int ver,
            @Query("action") String action, @Query("page") int page,
            @Query("page_size") int pageSize);
    
    
    //http://api.abc.com/WebApi/api.ashx?ver=1&action=user_invitation_list&page=1&page_size=10 
    @GET("api.ashx?ver=1&action=user_invitation_list&page=1&page_size=10")
    Call<ResultBean>  getBroadcastNoPar();
}
View Code
public class BuildService {
     private static Retrofit retrofit;

        public static ApiService getMeiNvService() {
            if (retrofit == null) {
                retrofit = new Retrofit.Builder()
                        .baseUrl(config.HTTP_URL)                         //设置Base的访问路径
                        .client(defaultOkHttpClient())
                        .addConverterFactory(GsonConverterFactory.create())
                        .build();
            }
            return retrofit.create(ApiService.class);
        }
        
        public static OkHttpClient defaultOkHttpClient() {
            OkHttpClient client = new OkHttpClient();
            return client;
        }
}
View Code

 下载JAR包文件

posted on 2016-04-29 11:27  freexiaoyu  阅读(416)  评论(0编辑  收藏  举报