简体   繁体   English

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

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

I am currently looking into implementing the Retrofit API (after using Volley) into my app and I have some questions that I cannot seem to find answers to anywhere else so I will ask here. 我目前正在考虑在我的应用程序中实现Retrofit API(在使用Volley之后),并且我有一些问题,似乎无法找到其他地方的答案,因此我将在这里提出。

  1. How do I go about downloading images using Retrofit API? 如何使用Retrofit API下载图像? I am asking this because Volley has the ImageLoader class and NetworkedImageView etc. and was wondering if Retrofit has something similar? 我之所以这样问是因为Volley具有ImageLoader类和NetworkedImageView等,并且想知道Retrofit是否具有类似的功能?

    1. I read that using the RequestIntercepter, it can add a header to every request. 我读到使用RequestIntercepter可以为每个请求添加标头。 How is this different from just adding a static (or dynamic) header (@Header) in the interface's abstract method 与仅在接口的抽象方法中添加静态(或动态)标头(@Header)有何不同?

    2. How does Retrofit deal with nested JSON objects? Retrofit如何处理嵌套的JSON对象? I read it uses GSON to convert the JSON into java objects but the POJO class must have the same field names. 我读到它使用GSON将JSON转换为java对象,但是POJO类必须具有相同的字段名称。

Thank you for reading 感谢您的阅读

For your first doubt: 您的第一个疑问:

Retrofit no have feature to manager image as Volley and UIL has. 不能像Volley和UIL那样对经理形象进行改造 The same company that developer Retrofit too have a nice lib to manager images called Picasso . 开发者Retrofit的同一家公司也有一个不错的库,用于管理映像Picasso

For your second doubt: 对于您的第二个疑问:

Headers that need to be added to every request can be specified using a RequestInterceptor. 可以使用RequestInterceptor指定需要添加到每个请求的标头。 The following code creates a RequestInterceptor that will add a User-Agent header to every request. 以下代码创建一个RequestInterceptor,它将为每个请求添加一个User-Agent头。

for instance: 例如:

I developed a client to Parse.com and to all request I need set my keys in the header: see here Android-Retrofit-Example 我为Parse.com开发了一个客户端,并且针对所有请求,我都需要在标题中设置密钥:请参见此处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;
    }
}

The last : 最后 :

JSON CONVERSION JSON转换

Retrofit uses Gson by default to convert HTTP bodies to and from JSON. 默认情况下,Retrofit使用Gson将HTTP正文与JSON相互转换。 If you want to specify behavior that is different from Gson's defaults (eg naming policies, date formats, custom types), provide a new Gson instance with your desired behavior when building a RestAdapter. 如果要指定与Gson的默认行为不同的行为(例如,命名策略,日期格式,自定义类型),请在构建RestAdapter时为新的Gson实例提供所需的行为。 Refer to the Gson documentation for more details on customization. 有关自定义的更多详细信息,请参阅Gson文档。

To get this : 为了得到这个:

{
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"
        }
}

Pojo : Pojo:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM