Retrofit 使用方法

來源:互聯網
上載者:User

標籤:

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包檔案

Retrofit 使用方法

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.