4

I'm trying to write a SyncAdapter that makes a set of requests based on an array of "entities" I made for the purpose.

For this, I have made a small class, that holds the information needed to create the Request, called EntityContract

public class EntityContract {

public final SyncContract syncContract;
public final String entityName;
public final String endpoint;
public final Class<? extends ModelClass> modelClass;

public EntityContract(SyncContract syncContract, String entityName, String endpoint , Class<? extends ModelClass> modelClass)
{
    this.syncContract = syncContract;
    this.endpoint = endpoint;
    this.entityName = entityName;
    this.modelClass = modelClass;
}

public String getUrl()
{
    return SyncContract.SERVICE_URL + "/" + endpoint;
}

}

In my SyncAdapter I simply iterate over an array of entityContracts to start the request one after another.

The problem I have is, when I try to parse the Response using Gson. (Thus this is not a Gson related question, I guess. It's about Java semantics)

The gson expects a call in the following structure

List< ExampleModel > list = new Gson().fromJson( response.body().charStream() , new TypeToken<List<ExampleModel>>(){}.getType() );

or

ExampleModel[] list = new Gson().fromJson( response.body().charStream() , ExampleModel[] );

where "ExampleModel" had to be replaced with the Class from the entityContract, so it would read something like this:

List< myContract.modelClass > list = new Gson().fromJson( response.body().charStream() , new TypeToken<List<myContract.modelClass>>(){}.getType() ); // Won't compile!!!

Is it possible to instantiate the List with a parameter read from a Class-variable? If not, is there another structure to achieve this?

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
T. Richter
  • 369
  • 1
  • 13

0 Answers0