我正在处理传入和传出消息的应用程序。有一些带有附件文件的消息,此刻,我正在努力以某种方式获得这些附件,即下载到电话中。为了下载该文件,我需要知道两件事:其名称和其中存在该消息的ID。要显示整个消息,我有ViewMessage类:
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 FileData[] attach;
private boolean hasAttaches() {
return attach != null && attach.length > 0;
}
public String[] getAttachesNames() {
if(hasAttaches())
{
String[] names = new String[attach.length];
line 44 at my error logs - System.arraycopy(attach, 0, names, 0, attach.length);
return names;
}
return new String[0];
}
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;
}
}
class FileData{
private String name;
private float size;
}
然后,我尝试在主类中引起数组提取的功能:
String file_name[] = Objects.requireNonNull(response.body()).getAttachesNames();
line 144 in my error: for (String aFile_name : file_name) {
txt.append(Arrays.toString(file_name) + "/n");
}
但是结果是我的应用程序崩溃了,并出现了一个错误:
08-21 09:41:58.342 E: FATAL EXCEPTION: main
Process: com.example.developer_4.test_login, PID: 3462
java.lang.ArrayStoreException: source[0] of type com.example.developer_4.test_login.data.model.FileData cannot be stored in destination array of type java.lang.String[]
at java.lang.System.arraycopy(Native Method)
at com.example.developer_4.test_login.data.model.ViewMessage.getAttachesNames(ViewMessage.java:44)
at com.example.developer_4.test_login.ActionsMessages.MessageShow$2.onResponse(MessageShow.java:144)
at retrofit2.ExecutorCallAdapterFactory$ExecutorCallbackCall$1$1.run(ExecutorCallAdapterFactory.java:70)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6119)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
最佳答案
似乎您正在尝试将FileData []转换为String []。请使用其他方式或只发布您的响应和FileData类,以便我可以帮助您。
谢谢