zoukankan      html  css  js  c++  java
  • 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包文件

  • 相关阅读:
    Octave/Matlab初步学习
    week_3
    week_2
    week_1
    清除input[type=number]的默认样式
    js,获取和设置cookie、 localStorage
    php表单提交时获取不到post数据的解决方法
    console.log 简写
    JS合并两个数组的方法
    javascript ES5、ES6的一些知识
  • 原文地址:https://www.cnblogs.com/freexiaoyu/p/5445685.html
Copyright © 2011-2022 走看看