How to implement an async Callback using Square's Retrofit networking library

前端 未结 1 1467
攒了一身酷
攒了一身酷 2020-12-08 04:58

As an iOS developer beginning to work with Android I came across Retrofit. I understand how to implement synchronous requests but am having trouble implementing asynchronous

相关标签:
1条回答
  • 2020-12-08 05:29

    After some more research and just plain spending more time in the Android/Java world I figured this out, using the example from their docs.

    Interface:

    @GET("/user/{id}/photo")  
    void listUsers(@Path("id") int id, Callback<Photo> cb);
    

    Implementation:

    RestAdapter restAdapter = new RestAdapter.Builder()
                .setServer("baseURL")     
                .build();
    ClientInterface service = restAdapter.create(ClientInterface.class);
    
    Callback callback = new Callback() {
        @Override
        public void success(Object o, Response response) {
    
        }
    
        @Override
        public void failure(RetrofitError retrofitError) {
    
        }
    };
    service.listUsers(666, callback);
    
    0 讨论(0)
提交回复
热议问题