RoboSpice + Google HTTP 客户端 + GSON = 无法解析 JSON 对象
RoboSpice + Google HTTP Client + GSON = failed to parse JSON to object
我将 RoboSpice 与 Google HTTP 客户端和 GSON 一起使用:
ApiSpiceService:
public class ApiSpiceService extends GoogleHttpClientSpiceService {
private static final int THREAD_COUNT = 3;
@Override
public CacheManager createCacheManager(Application application) throws CacheCreationException {
CacheManager cacheManager = new CacheManager();
GsonObjectPersisterFactory gsonObjectPersisterFactory = new GsonObjectPersisterFactory(application);
cacheManager.addPersister(gsonObjectPersisterFactory);
return cacheManager;
}
@Override
public int getThreadCount() {
return THREAD_COUNT;
}
}
要求:
public class InfoRequest extends GoogleHttpClientSpiceRequest<Contact> {
private final String url;
public InfoRequest() {
super(Contact.class);
this.url = "some-url/path.json";
}
@Override
public Contact loadDataFromNetwork() throws Exception {
HttpTransport httpTransport = new NetHttpTransport();
HttpRequestFactory httpRequestFactory = httpTransport.createRequestFactory(new InfoHttpRequestInitializer());
HttpRequest httpRequest = httpRequestFactory.buildGetRequest(new GenericUrl(url));
httpRequest.setParser(new GsonFactory().createJsonObjectParser());
return httpRequest.execute().parseAs(Contact.class);
}
private class InfoHttpRequestInitializer implements HttpRequestInitializer {
@Override
public void initialize(HttpRequest request) throws IOException {
}
}
}
型号(Contact.java):
public class Contact {
private String data;
}
基础活动:
public abstract class BaseActivity extends ActionBarActivity {
protected SpiceManager spiceManager = new SpiceManager(ApiSpiceService.class);
@Override
protected void onStart() {
super.onStart();
spiceManager.start(this);
}
@Override
protected void onStop() {
spiceManager.shouldStop();
super.onStop();
}
}
和 MainActivity:
public class MainActivity extends BaseActivity {
private InfoRequest infoRequest;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
infoRequest = new InfoRequest();
}
@Override
protected void onStart() {
super.onStart();
spiceManager.execute(infoRequest, "txt", DurationInMillis.ALWAYS_EXPIRED, new TextRequestListener());
}
private class TextRequestListener implements RequestListener<Contact> {
@Override
public void onRequestFailure(SpiceException spiceException) {
//
}
@Override
public void onRequestSuccess(Contact s) {
//
}
}
这似乎是有效的代码,但不幸的是,当它完成请求执行时,返回的 Contact 实例中的字段数据为空。
logcat.
中没有错误
请求的内容 100% 有效JSON。为什么它没有被解析?
好的,我找到了解决方案。我需要将@Key 注释添加到模型中的字段。很奇怪,因为纯Gson不需要这个。
public class Contact {
@Key
private String data;
}
我将 RoboSpice 与 Google HTTP 客户端和 GSON 一起使用:
ApiSpiceService:
public class ApiSpiceService extends GoogleHttpClientSpiceService {
private static final int THREAD_COUNT = 3;
@Override
public CacheManager createCacheManager(Application application) throws CacheCreationException {
CacheManager cacheManager = new CacheManager();
GsonObjectPersisterFactory gsonObjectPersisterFactory = new GsonObjectPersisterFactory(application);
cacheManager.addPersister(gsonObjectPersisterFactory);
return cacheManager;
}
@Override
public int getThreadCount() {
return THREAD_COUNT;
}
}
要求:
public class InfoRequest extends GoogleHttpClientSpiceRequest<Contact> {
private final String url;
public InfoRequest() {
super(Contact.class);
this.url = "some-url/path.json";
}
@Override
public Contact loadDataFromNetwork() throws Exception {
HttpTransport httpTransport = new NetHttpTransport();
HttpRequestFactory httpRequestFactory = httpTransport.createRequestFactory(new InfoHttpRequestInitializer());
HttpRequest httpRequest = httpRequestFactory.buildGetRequest(new GenericUrl(url));
httpRequest.setParser(new GsonFactory().createJsonObjectParser());
return httpRequest.execute().parseAs(Contact.class);
}
private class InfoHttpRequestInitializer implements HttpRequestInitializer {
@Override
public void initialize(HttpRequest request) throws IOException {
}
}
}
型号(Contact.java):
public class Contact {
private String data;
}
基础活动:
public abstract class BaseActivity extends ActionBarActivity {
protected SpiceManager spiceManager = new SpiceManager(ApiSpiceService.class);
@Override
protected void onStart() {
super.onStart();
spiceManager.start(this);
}
@Override
protected void onStop() {
spiceManager.shouldStop();
super.onStop();
}
}
和 MainActivity:
public class MainActivity extends BaseActivity {
private InfoRequest infoRequest;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
infoRequest = new InfoRequest();
}
@Override
protected void onStart() {
super.onStart();
spiceManager.execute(infoRequest, "txt", DurationInMillis.ALWAYS_EXPIRED, new TextRequestListener());
}
private class TextRequestListener implements RequestListener<Contact> {
@Override
public void onRequestFailure(SpiceException spiceException) {
//
}
@Override
public void onRequestSuccess(Contact s) {
//
}
}
这似乎是有效的代码,但不幸的是,当它完成请求执行时,返回的 Contact 实例中的字段数据为空。 logcat.
中没有错误请求的内容 100% 有效JSON。为什么它没有被解析?
好的,我找到了解决方案。我需要将@Key 注释添加到模型中的字段。很奇怪,因为纯Gson不需要这个。
public class Contact {
@Key
private String data;
}