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

南笙酒味 提交于 2019-12-17 17:44:12

问题


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 requests with success/failure callbacks. Specifically, the Callback syntax is unclear to me and there are no concrete examples of how to do this on the Retrofit website, the Square blogpost introducing Retrofit, or elsewhere that I've seen. Can someone please post some example code on this? I filed an issue in the Retrofit repo asking that they update the README with this info.


回答1:


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);


来源:https://stackoverflow.com/questions/17034465/how-to-implement-an-async-callback-using-squares-retrofit-networking-library

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!