android - RoboSpice + Google HTTP Client + GSON = 无法将 JSON 解析为对象

标签 android json gson robospice google-http-client

我以这种方式将 RoboSpice 与 Google HTTP 客户端和 GSON 结合使用:

APISpice 服务:

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;
}

基础 Activity :

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;

}

关于android - RoboSpice + Google HTTP Client + GSON = 无法将 JSON 解析为对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29449577/

相关文章:

android - 更新到 gradle 7 后方法 com.android.build.gradle.internal.core.MergedFlavor#setSigningConfig 的模棱两可的方法重载

php - 处理 JSON 数据获取的最理想方式

python - flask jsonify : object is not iterable

android - 如何将文档快照(复杂对象)保存到共享首选项或文件?

gson - Response.toString() 返回 io.restassured.internal.RestAssuredResponseImpl@46320c9a 而不是响应正文字符串

java - JSON 查找值并返回带有键的对象

android - 第一个代码和第二个代码之间有什么区别,为什么我应该添加getString?

java - 我的代码不会从 Android 中的 sqlite 数据库表中删除最后 2 行

java - 如何控制 okHttpClient 的连接大小?

json - 将多态案例类转换为 json 并返回