如何创建一个功能来下载附件文件android改造?

时间:2018-08-20 13:14:53

标签: android

在我的应用程序中,我使用消息,因为在每个客户端中,我都需要实现下载消息所附文件的功能。这是给我的文档:

URL
id - message id for view INT
attach_file_name - name of attached file

https://server/v1/message/<id>/attachment/<attach_file_name>

Method
GET
URL Params
type 
0 - for received messages
1 - for sent messages

Data Params
{}

Success Response
HTTP 200
Headers:
Content-Type: TYPE # file mimetype
Content-Disposition: attachment; filename=FILENAME # attached file name
Body: file

Sample Call
curl -i -X GET -H "Content-Type:application/json" -H "Authorization:Bearer $ACCESS_TOKEN" https://server/v1/message/ID/attachment/ATTACH_FILE_NAME?type=TYPE

这是传入的附件文件数组的示例:

[ {"name":"File 1", "size":12.1},  {"name":"File 2", "size":12.2} ]

我做的第一件事是在接口中创建一个查询,该查询会将参数传递给服务器:

@Headers({"Content-type:application/json"})
@GET("/v1/message/{id}/attachment/{attach_file_name}")
Call<GetAttachment> getAttach (@Header("Autorization") String token,@Path("id") Integer id, @Path("attach_file_name") String file_name, @Query("type") int type, @Query("offset") int offset);

接下来,在直接进入文件下载和来自接口的请求之前,我尝试注册一个函数,该函数将接收带有参数的附加文件数组:

public class ViewMessage {
    @SerializedName("date")
    @Expose
    private String date;
    @SerializedName("type")
    @Expose
    private String type;
    @SerializedName("user_id")
    @Expose
    private String userId;
    @SerializedName("body")
    @Expose
    private String body;
    @SerializedName("can_delete")
    @Expose
    private String canDelete;
    @SerializedName("can_reply")
    @Expose
    private String canReply;
    @SerializedName("name")
    @Expose
    private String name;
    @SerializedName("subject")
    @Expose
    private String subject;

    @SerializedName("attach")
    private String[][] attach;

    private boolean hasAttaches() {
        return attach != null && attach.length > 0;
    }

    private String[] getAttachesNames() {
        String[] names = new String[attach.length];
        for (int i = 0; i < attach.length; i++) {
            names[i] = attach[i][0];
        }
        return names;
    }


    public String getDate() {
        return date;
    }

    public void setDate(String date) {
        this.date = date;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    /*public String getUserId() {
        return userId;
    }

    public void setUserId(String userId) {
        this.userId = userId;
    }*/

    public String getBody() {
        return body;
    }

    public void setBody(String body) {
        this.body = body;
    }

    public String getCanDelete() {
        return canDelete;
    }

    /*public void setCanDelete(String canDelete) {
        this.canDelete = canDelete;
    }*/

    public String getCanReply() {
        return canReply;
    }

    /*public void setCanReply(String canReply) {
        this.canReply = canReply;
    }*/

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getSubject() {
        return subject;
    }



    /*public void setSubject(String subject) {
        this.subject = subject;
    }*/


}

我只添加了一个变量和几个函数,由于某种原因,我现在不理解其中包含附件的消息,因此在查看整个消息时它什么都不显示。即没有信函的主题,发件人,信函的正文等。我无法理解为什么我在负责数组报告的变量中有问题。而且我不知道如何编写一个函数来从一系列附加文件中提取它们的名称和大小。如果您在这里帮助我,我将非常高兴。

0 个答案:

没有答案