首页 > 解决方案 > 使用改造 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);
    )
}

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

配置文件上传的代码图像

带有分号相同错误的图像

标签: javajsonandroid-studioretrofit

解决方案


您可以List<YourModel>改用,改造将为您进行转换。检查这些答案。

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

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


推荐阅读