Retrofit:如何下载图像? + 其他一些问题

Retrofit: How do I go about downloading images? + Some other questions

我目前正在考虑在我的应用程序中实现 Retrofit API(在使用 Volley 之后),我有一些问题似乎无法在其他任何地方找到答案,所以我会在这里提问。

  1. 如何使用 Retrofit 下载图像 API?我问这个是因为 Volley 有 ImageLoader class 和 NetworkedImageView 等,想知道 Retrofit 是否有类似的东西?

    1. 我读到使用 RequestIntercepter,它可以为每个请求添加一个 header。这与仅在接口的抽象方法

    2. 中添加静态(或动态)header (@Header) 有何不同
    3. Retrofit如何处理嵌套JSONobjects?我读到它使用 GSON 将 JSON 转换为 java objects 但 POJO class 必须具有相同的字段名称。

感谢阅读

第一个疑问:

Retrofit 没有像 Volley 和 UIL 那样的管理图像功能。开发人员 Retrofit 的同一家公司也有一个很好的库来管理名为 Picasso 的图像。

对于你的第二个疑问:

Headers that need to be added to every request can be specified using a RequestInterceptor. The following code creates a RequestInterceptor that will add a User-Agent header to every request.

例如:

我为Parse.com and to all request I need set my keys in the header: see here Android-Retrofit-Example

开发了一个客户端
public class RestClient {

    private static RestClient mRestClient = null;
    private static RestAdapter restAdapter;
    private static RequestInterceptor requestInterceptor;

    public static RestClient getInstance(){

        if(mRestClient == null ){

            mRestClient = new RestClient();
            setup();
        }

        return mRestClient;
    }

    private static void setup(){

        requestInterceptor = new RequestInterceptor() {
            @Override
            public void intercept(RequestFacade request) {

                request.addHeader("X-Parse-Application-Id", "");
                request.addHeader("X-Parse-REST-API-Key", "");
            }
        };

        restAdapter = new RestAdapter.Builder()
                .setEndpoint(" https://api.parse.com/1/")
                .setLogLevel(RestAdapter.LogLevel.FULL)
                .setRequestInterceptor(requestInterceptor)
                .build();
    }

    public RestAdapter getRestAdapter(){
        return restAdapter;
    }
}

最后一个:

JSON 转换

Retrofit uses Gson by default to convert HTTP bodies to and from JSON. If you want to specify behavior that is different from Gson's defaults (e.g. naming policies, date formats, custom types), provide a new Gson instance with your desired behavior when building a RestAdapter. Refer to the Gson documentation for more details on customization.

得到这个:

{
results: [3]
        {
            createdAt: "2015-03-07T20:43:44.107Z"
            objectId: "osCJ8PI65r"
            updatedAt: "2015-03-08T00:45:37.539Z"
            username: "Test 2"
        },
        {
            createdAt: "2015-03-07T21:42:38.591Z"
            objectId: "tkIi6Ll1Os"
            updatedAt: "2015-03-07T21:42:38.591Z"
            username: "Test 2"
        },
        {
            createdAt: "2015-03-08T01:13:21.188Z"
            objectId: "Cz0HqiYpwl"
            updatedAt: "2015-03-08T04:21:18.069Z"
            username: "Test 3"
        }
}

波乔:

public class User {

    private String objectId;
    private String username;
    private String createdAt;
    private String updatedAt;

    //gerate getters and setters
}

public class WrappeUser {

    @SerializedName(value="results")
    List<User> results;

    public List<User> getResults() {
        return results;
    }

    public void setResults(List<User> results) {
        this.results = results;
    }
}