Android java:如何创建 POJO 并将其转换为可接受的 JSON Cloud Firestore REST API

Android java: How to create POJO and convert it into acceptable JSON for Cloud Firestore REST API

我通过 postman 像这样

进行了预览测试 REST API 查询

https://firestore.googleapis.com/v1/projects/MY_PROJECT_ID/databases/(default)/documents/USER_ID_HASH/documents/112233445566

JSON正文示例:

{
  "fields": {
    "longitude": {
      "doubleValue": 35.5635
    },
    "latitude": {
      "doubleValue": 45.5366
    }
  }
}

并获得 http 响应 200,因此将使用 retrofit2.

在代码中实现它

问题是 "doubleValue" 是根据值类型进行字段转换之类的东西,所以它也可能是 "stringValue" or "integerValue" 之类的。问题是如何创建 POJO class 以便在 JSON 正文中进一步转换,如上面的 JSON 正文示例?

我现在有什么:

API

public 接口 PlaceholderApi {

@POST("{userId}/documents/{timestamp}")
Call<Transaction.Result> saveLocationToCloud(
    @Path("userId") String userId,
    @Path("timestamp") long timestamp,
    @Body LocationData data
);

}

网络服务

public class NetworkService {

private static NetworkService instance;

private static final String WEB_API_KEY = "zaSy..........VWI";

private static final String BASE_URL_CLOUD
    = "https://firestore.googleapis.com/v1/projects/alocationtracker/databases/(default)/documents/";

private Retrofit retrofit;

private NetworkService() {

    HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
    interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);

    OkHttpClient.Builder client = new OkHttpClient.Builder()
        .addInterceptor(interceptor);

    client.interceptors().add(chain -> {
        Request request = chain.request();
        HttpUrl url = request.url().newBuilder().addQueryParameter("key", WEB_API_KEY).build();
        request = request.newBuilder().url(url).build();
        return chain.proceed(request);
    });

    retrofit = new Retrofit.Builder()
        .baseUrl(BASE_URL_CLOUD)
        .addConverterFactory(GsonConverterFactory.create())
        .client(client.build())
        .build();
}

public static NetworkService getInstance() {
    if (instance == null) {
        instance = new NetworkService();
    }
    return instance;
}

public PlaceholderApi getJsonApi() {
    return retrofit.create(PlaceholderApi.class);
}

}

POJO

public class LocationData {
    @SerializedName("userId")
    @Expose
    private String userId;

    @SerializedName("timestamp")
    @Expose
    private long timestamp;

    @SerializedName("longitude")
    @Expose
    private double longitude;

    @SerializedName("latitude")
    @Expose
    private double latitude;

    public LocationData(String userId) {
        this.userId = userId;
    }

    public LocationData(String userId, Location location) {
        this.userId = userId;
        this.timestamp = location.getTime();
        this.latitude = location.getLatitude();
        this.longitude = location.getLongitude();
    }
}

JSON 我得到的格式不正确,没有值类型,如 "doubleValue"

{"latitude":19.9802209,"longitude":16.1852646,"timestamp":1599830853181,"userId":"2DBtYfX1uxQszxNmod83"}

我认为 POJO class 是我唯一需要以某种方式更改的东西。或者我可能需要更改 retrofit.create(PlaceholderApi.class) 其他内容?

有什么建议,请。

谢谢。

如果您收到的回复低于 API

    {
  "fields": {
    "longitude": {
      "doubleValue": 35.5635
    },
    "latitude": {
      "doubleValue": 45.5366
    }
  }
}

然后将您的模型更改为如下所示。

public class PlacesResponseModel {

@SerializedName("fields")
@Expose
private Fields fields;

public Fields getFields() {
return fields;
}

public void setFields(Fields fields) {
this.fields = fields;
}

}

public class Fields {

@SerializedName("longitude")
@Expose
private Longitude longitude;
@SerializedName("latitude")
@Expose
private Latitude latitude;

public Longitude getLongitude() {
return longitude;
}

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

public Latitude getLatitude() {
return latitude;
}

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

}


public class Latitude {

@SerializedName("doubleValue")
@Expose
private Double doubleValue;

public Double getDoubleValue() {
return doubleValue;
}

public void setDoubleValue(Double doubleValue) {
this.doubleValue = doubleValue;
}

}
public class Longitude {

@SerializedName("doubleValue")
@Expose
private Double doubleValue;

public Double getDoubleValue() {
return doubleValue;
}

public void setDoubleValue(Double doubleValue) {
this.doubleValue = doubleValue;
}

}

并将其添加到改装 API 回调中,如下所示。

public interface PlaceholderApi {

@POST("{userId}/documents/{timestamp}")
Call<PlacesResponseModel> saveLocationToCloud(
    @Path("userId") String userId,
    @Path("timestamp") long timestamp,
    @Body LocationData data
);
}

现在调用最后一步,您应该映射模型