gpt4 book ai didi

android - 无法从服务器改造android的响应中提取数组,结果我崩溃了?

转载 作者:行者123 更新时间:2023-11-29 16:38:30 25 4
gpt4 key购买 nike

我正在处理传入和传出消息的应用程序。有一些带有附件的消息,目前我正在研究以某种方式获取这些附件的方法,即下载到手机。为了下载该文件,我需要知道两件事:它的名称和它所在的消息的 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 类,以便我可以帮助您。

谢谢

关于android - 无法从服务器改造android的响应中提取数组,结果我崩溃了?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51943071/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com