【问题标题】:POST a JSONArray using retrofit android studio使用改造 android studio 发布 JSONArray
【发布时间】:2021-08-21 05:06:57
【问题描述】:

目前我正在通过 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);
    )
}

仍然无法处理错误“错误:类型的非法开始”

Image of code for Profile Upload

Image with semicolon same error

【问题讨论】:

    标签: java json android-studio retrofit


    【解决方案1】:

    您可以改用List&lt;YourModel&gt;,改造将为您完成转换。检查这些答案。

    https://stackoverflow.com/a/56982069/4491971

    https://stackoverflow.com/a/34435868/4491971

    【讨论】:

    • 感谢您的评论。我尝试为要发送的项目创建一个 Java 类,但仍然遇到语法错误。我也尝试使用列表它仍然无法正常工作。 @mohit-ajwani
    • Body 右括号后缺少分号。应该是@Body(JsonArray jsonArray); 这只是一个例子。添加分号,它应该可以解决error: illegal start of type 的错误。这解决了图像错误。
    • 我尝试像这样使用第一个链接中的那个。有或没有分号都不起作用。我认为 JSONProfiles 类有问题。我添加了一个带分号的图像。 @POST("example/api/order") Call&lt;JSONArray&gt; postOrder(@Body List&lt;Cart&gt; cartList);
    猜你喜欢
    • 2019-06-21
    • 2018-07-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-12
    • 1970-01-01
    • 2020-05-21
    相关资源
    最近更新 更多