A
A
Andrey Goroshko2018-08-21 11:41:54
Android
Andrey Goroshko, 2018-08-21 11:41:54

How to get the name and size of the android file attached to the letter from the server response?

In my application, I work with messages that can have attached files, so in order to send a request to download a file, I need to know the file name and message id. So I created a class to get the size and file name, then I call this class in the function of showing the entire message. And so I ran into a problem that for some reason diamonds with a question mark are displayed in my textView, or nothing is displayed. Maybe I somehow incorrectly initialized the array of names, or called the class incorrectly. Here is a class for getting information about a file:

public class FileData {
    @SerializedName("size")
    @Expose
    private double size;
    @SerializedName("name")
    @Expose
    private String name;

    public FileData(double size, String name) {
        this.size = size;
        this.name = name;
    }

    public double getSize() {
        return size;
    }

    public String getName() {
        return name;
    }
}

Here is my class for displaying 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;

    @SerializedName("attach")
    private List<FileData> attach;
    
    public String[] getAttachesNames() {
        String[] names = new String[attach.size()];
        for (int i = 0; i < attach.size(); i++) {
            FileData fileInfo = attach.get(i);
            names[i] = fileInfo.getName();
        }
        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 String getBody() {
        return body;
    }

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

    public String getCanDelete() {
        return canDelete;
    }

    public String getCanReply() {
        return canReply;
    }

    public String getName() {
        return name;
    }

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

    public String getSubject() {
        return subject;
    }
}

and finally here's how I'm trying to put data into the textView:
String[] file_name = Objects.requireNonNull(response.body()).getAttachesNames();
                    for (int c=0;c<file_name.length-1;c++)
                        txt.append(file_name[c]+"\n");

                    if (file_name.length > 0) {
                        setEmptyBackground(bt);
                    } else {
                        bt.setEnabled(false);
                        txt.setText("No attachment)");
                    }

maybe I can pull out the names or names of attached files in some other way, because I can’t get the name from the server response in any way, it seems to me that diamonds are due to encoding problems, but not a fact. If someone understands what the problem is, I will be very grateful if you point me to it, or advise its solution.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
O
Oleg, 2018-08-21
@402d

It seems to me that rhombuses are due to encoding problems, but not a fact.
https://www.anekdot.ru/id/232085/
check what is leaving. what comes. What are you dragging through all the stages.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question