Answer the question
In order to leave comments, you need to log in
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;
}
}
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;
}
}
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)");
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question