A
A
Andrey Goroshko2018-08-20 14:34:02
Android
Andrey Goroshko, 2018-08-20 14:34:02

How to write a function to get a list of multiple/single files attached to an email in android retrofit?

In my application, I work with messages that come to me in one service. Each email may or may not have an attached file that the user wants to download. For downloading, they gave me api, for a start, here is the api itself:

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

https://сервер/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://сервер/v1/message/ID/attachment/ATTACH_FILE_NAME?type=TYPE

Here is an example of how in the server response, the letter looks like two attached files:
"attach": 

I have a class that is responsible for displaying the message itself, in order to download a specific file, I need its name and message id in which this file is. The class for displaying a message has several functions that are aimed at extracting from the server response everything related to the letter, the subject, the date of receipt, the sender, and the message itself. I understand that you need to insert a function into the same class that will receive a list of one / several attached files. Here is the class I am using to display the message:
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;

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

    
}

I can't understand how the function will look like, which will pull out the name and size of one / several attached files. This is in fact an array in which there are more arrays. But I can't create the method I need.
I want to make a function that will get the names of all the attached files that are in the message.
If anyone understands how to help me, then I will be very grateful for the help provided.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
illuzor, 2018-08-20
@iLLuzor

Work like an array of arrays. Create a variable

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

And then work with it as needed:
attach[0][0] // имя
attach[0][1] // размер. если нужно, приводите ко float
// и тд

For example, to return all filenames, you would write a method that would iterate over the given array, write all the names into a new array (or list) and return it.
If it is possible to change the api, you can do something like this:
Then create a private class:
private class FileData{
    private String name;
    private float size;
}

And then just work with the array:
@SerializedName("attach")
private FileData[] attach;

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question