改装2发布Multipart Json的名字

时间:2016-04-06 13:59:43

标签: android json post multipart retrofit2

我试图通过改造2,坐标,例如:

发布

name =“geo”{“date”:“2016-03-28 18:15:22”,“lat”:65.9667,“long”: - 18.5333}

你能帮帮我吗?我有, 型号:

public class LocationCoordinatesEntity{

@SerializedName("long")
@Expose
private Double longitude;

@SerializedName("lat")
@Expose
private Double latitude;

@SerializedName("date")
@Expose
private Date date;

public Double getLongitude() {
    return longitude;
}

public void setLongitude(Double longitude) {
    this.longitude = longitude;
}

public Double getLatitude() {
    return latitude;
}

public void setLatitude(Double latitude) {
    this.latitude = latitude;
}

public Date getDate() {
    return date;
}

public void setDate(Date date) {
    this.date = date;
}

public LocationCoordinatesEntity(Double longitude, Double latitude, Long date){
    this.latitude = latitude;
    this.longitude = longitude;
    this.date = new Date(date);
} }

@Multipart
@POST("setgeojson")
Observable<Void> setgeoMultipart(@Part("geo") String geo);

  Observable<Void> sendMultipartObservable = apiInterface.setgeoMultipart(gson.toJson(coordinatesEntity));

当我发布时,我会收到错误:

java.lang.IllegalStateException: JSON must start with an array or an object.

2 个答案:

答案 0 :(得分:0)

您正在尝试发布字符串而不是json对象。

改变这个:

apiInterface.setgeoMultipart(coordinatesEntity);

并改变这一点:

@Multipart
@POST("setgeojson")
Observable<Void> setgeoMultipart(@Part("geo") LocationCoordinatesEntity geo);

答案 1 :(得分:0)

另一个技巧就是这样做(当改装将执行Http帖子时,这会将2个JSON对象合并为1):

HashMap<String, Object> map = new HashMap<>();
map.put("obj1name", someObject1);
map.put("obj2name", someObject2);
somePostCall(map);

接口调用应该如下所示:

@POST("some/url/to/post")
Call<SomeType> somePostCall(
        @Body HashMap combinedObject
);