java - 使用改造 android studio 发布 JSONArray
问题描述
目前我正在通过 POST 单独发送变量。我想将所有项目编译为 JSON 数组并将其发送到我的服务器。
public interface ProfilesApi {
@FormUrlEncoded
@POST("upload_profiles.php")
Call<ResponsePojo> uploadProfiles(
@Field("profResCodePost") String resCode,
@Field("profProfilePost") String profile,
@Field("profNameRespondentPost") String nameRespondent,
@Field("profDateSurveyed") String dateSurveyed
);
}
JSON数组也是“@Field”还是应该是“@Body”?
JsonArray jsonArray = new JsonArray();
for (int i = 0; i < listProfiles.size(); i++) {
JsonObject jsonObject = new JsonObject();
ListProfiles currentProfile = listProfiles.get(i);
jsonObject.addProperty("profResCodePost", currentProfile.profileResCode);
jsonObject.addProperty("profProfilePost", currentProfile.profileProfile);;
jsonObject.addProperty("profOwnerTypePost", currentProfile.profileOwnerType);
jsonObject.addProperty("profNameRespondentPost", currentProfile.profileNameRespondent);
jsonObject.addProperty("profDateSurveyed", currentProfile.dateSurveyed);
jsonArray.add(jsonObject);
}
我的想法是这样的。
public interface ProfilesApi {
@FormUrlEncoded
@POST("upload_profiles.php")
Call<ResponsePojo> uploadProfiles(
@Body("jsonArrayProfile") JsonArray jsonArray
);
}
我尝试使用@Body 时的错误是“找不到符号方法值()”
当我尝试这个
public interface ProfilesJsonArray {
@FormUrlEncoded
@POST("upload_profiles.php")
Call<ResponsePojo> uploadProfiles(
@Body(JsonArray jsonArray)
);
}
错误是“错误:')' 预期”
编辑 1:我为 JSONProfiles 添加了一个 Java 类,如下所示
public class JSONProfiles {
@SerializedName("profResCodePost")
private final String resCode;
@SerializedName("profProfilePost")
private final String profile;
@SerializedName("profNameRespondentPost")
private final String name_respondent;
@SerializedName("profDateSurveyed")
private final String date_of_survey;
}
然后对 POST 方法执行此操作
public interface ProfilesJsonArray {
@Headers({
"Content-type: application/json"
})
@POST("upload_profiles.php")
Call<ResponsePojo> uploadProfiles(
@Body(ArrayList <JSONProfiles> jsonprof);
)
}
仍然无法处理错误“错误:类型的非法开始”
解决方案
您可以List<YourModel>
改用,改造将为您进行转换。检查这些答案。
推荐阅读
- python - NameError:未定义名称“OutOfBoundsDatetime”
- angular - Angular - 分叉后的分叉
- c++ - 为什么我的输出txt文件是空的,打开文本文件时我没有收到错误
- python - 简单的多处理池挂在 Jupyter 笔记本中
- html - 使用引导程序 4 水平排列多个画布
- cors - 405 方法不允许在 ASP.NET 5 WebApi OPTIONS 预检
- google-cloud-platform - 如何使用谷歌云计算引擎全核睿频?
- typescript - 如何排除模板文字类型的子字符串?
- javascript - 在简单的 3D 立方体模型上使用 Three.JS 的服务器发送事件 (SSE) 很慢
- c - 如何将 sqlite3 库与 makefile 链接