我正在开发一个Android应用,我使用RoboSpice + Google HTTP Client + Jackson2来处理对JSON REST API的调用。
我无法使用@JsonProperty
- 在序列化对象期间始终默默忽略它,而是使用Java变量名。
我经历了很多StackOverflow的答案,遗憾的是没有任何成功。
我的POJO定义如下所示:
package org.something;
import android.content.Context;
import com.google.api.client.util.Key;
import com.fasterxml.jackson.annotation.JsonProperty;
import org.something.client.utils.SystemInfo;
public class ClientNode {
@Key
private String id;
@Key
@JsonProperty(value="os_type")
private String osType;
@Key
@JsonProperty(value="os_version")
private String osVersion;
@Key
@JsonProperty(value="os_architecture")
private String osArchitecture;
@Key
@JsonProperty(value="app_version")
private String appVersion;
@Key
@JsonProperty(value="memory_usage")
private int memoryUsage;
@Key
@JsonProperty(value="current_network_interface_type")
private String currentNetworkInterfaceType;
@Key
@JsonProperty(value="current_network_mobile_type")
private String currentNetworkMobileType;
@Key
@JsonProperty(value="device_model")
private String deviceModel;
@Key
@JsonProperty(value="device_manufacturer")
private String deviceManufacturer;
@Key
@JsonProperty(value="current_network_mobile_carrier")
private String currentNetworkMobileCarrier;
@Key
@JsonProperty(value="power_battery_charging_state")
private String powerBatteryChargingState;
@Key
@JsonProperty(value="power_battery_charging_type")
private String powerBatteryChargingType;
@Key
@JsonProperty(value="power_battery_level")
private int powerBatteryLevel;
@Key
@JsonProperty(value="power_battery_health")
private String powerBatteryHealth;
public ClientNode() {
// Required by Jackson2
}
public ClientNode(Context context) {
SystemInfo systemInfo = new SystemInfo(context);
this.osType = systemInfo.getOsType();
this.osVersion = systemInfo.getOsVersion();
this.osArchitecture = systemInfo.getOsArchitecture();
this.appVersion = systemInfo.getAppVersion();
// TODO add cpu load
this.memoryUsage = systemInfo.getMemoryUsage();
this.currentNetworkInterfaceType = systemInfo.getCurrentNetworkInterfaceType();
if(this.currentNetworkInterfaceType.equals("mobile")) {
this.currentNetworkMobileType = systemInfo.getCurrentNetworkMobileType();
this.currentNetworkMobileCarrier = systemInfo.getCurrentNetworkMobileCarrier();
}
// TODO add network strength
this.deviceModel = systemInfo.getDeviceModel();
this.deviceManufacturer = systemInfo.getDeviceManufacturer();
this.powerBatteryChargingState = systemInfo.getPowerBatteryChargingState();
this.powerBatteryChargingType = systemInfo.getPowerBatteryChargingType();
this.powerBatteryLevel = systemInfo.getPowerBatteryLevel();
this.powerBatteryHealth = systemInfo.getPowerBatteryHealth();
}
public String getId() {
return id;
}
}
我还尝试使用@JsonProperty("something")
的语法 - 仍然不起作用。
这是负责提出实际请求的代码:
package org.something;
import android.content.Context;
import android.util.Log;
import com.google.api.client.http.GenericUrl;
import com.google.api.client.http.HttpRequest;
import com.google.api.client.http.json.JsonHttpContent;
import com.google.api.client.json.jackson2.JacksonFactory;
import com.octo.android.robospice.request.googlehttpclient.GoogleHttpClientSpiceRequest;
import org.something.api.ApiHelpers;
import org.something.api.ApiUriBuilder;
import org.radiokit.joint.client.api.models.plumber.resource.architecture.ClientNode;
import org.something.api.responses.SystemInfoUpdateResponse;
public class SystemInfoUpdateRequest extends GoogleHttpClientSpiceRequest<SystemInfoUpdateResponse> {
private final static String LOG_TAG = "SystemInfoUpdateReq";
private final String clientNodeId;
private final Context context;
public SystemInfoUpdateRequest(Context context, String clientNodeId) {
super(SystemInfoUpdateResponse.class);
this.context = context;
this.clientNodeId = clientNodeId;
}
@Override
public SystemInfoUpdateResponse loadDataFromNetwork() throws Exception {
Log.d(LOG_TAG, "loadDataFromNetwork: start");
JsonHttpContent content = new JsonHttpContent(new JacksonFactory(), new ClientNode(context));
HttpRequest request = getHttpRequestFactory().buildPutRequest(new GenericUrl(new ApiUriBuilder("xxx", "ClientNode", this.clientNodeId).build()), content);
request.setParser(new JacksonFactory().createJsonObjectParser());
request = ApiHelpers.addApiHeaders(request);
return request.execute().parseAs(getResultType());
}
}
我做错了什么?