我是Retrofit的新手。使用改造作为响应出错:com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected a string but was BEGIN_OBJECT at line 1 column 2 path $
所以我必须这样做:
- 请求POST
- 标题“Content-Type:application / json”
- data {“device”:{“token”:“213”,“platform”:“ios”}}
我创造了
public class Device {
@SerializedName("token")
@Expose
private String token;
@SerializedName("platform")
@Expose
private String platform;
public String getToken() {
return token;
}
public void setToken(String token) {
this.token = token;
}
public String getPlatform() {
return platform;
}
public void setPlatform(String platform) {
this.platform = platform;
}
}
public class Example {
@SerializedName("device")
@Expose
private Device device;
public Device getDevice() {
return device;
}
public void setDevice(Device device) {
this.device = device;
}
}
public interface ApiService {
@POST("/")
void addDevice(@Body Example example, Callback<String> callback);
}
private void executePostDevice() {
RestAdapter.Builder builder = new RestAdapter.Builder()
.setEndpoint(NetworkConstants.DEVICES_URL)
.setRequestInterceptor(new RequestInterceptor() {
@Override
public void intercept(RequestFacade request) {
request.addHeader("header", "application/json");
}
});
RestAdapter restAdapter = builder.build();
Example ex = new Example();
Device device = new Device();
device.setToken("test_token");
device.setPlatform("android");
ex.setDevice(device);
ApiService apiService = restAdapter.create(ApiService.class);
apiService.addDevice(ex, new Callback<String>() {
@Override
public void success(String s, Response response) {
}
@Override
public void failure(RetrofitError error) {
}
});
}