androidjsongsonrobospicegoogle-http-client

RoboSpice + Google HTTP Client + GSON = failed to parse JSON to object


I'm using RoboSpice with Google HTTP Client & GSON this way:

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

Request:

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

Model (Contact.java):

public class Contact {
    private String data;
}

BaseActivity:

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

And 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) {
            //
        }
    }

It seems to be valid code, but, unfortunately, when it finish the request execution, field data in returned Contact instance is null. There are no errors in logcat.

The requested content is 100% valid JSON. Why it is not being parsed?


Solution

  • Ok, I've found the solution. I need to add @Key annotation to fields in model. It's strange, because pure Gson does not require this.

    public class Contact {
    
        @Key
        private String data;
    
    }