0

Hi I am trying to implement a simple application showing picasa photos in android.

After many trials now I use the Google Java Api Client (Not the Gdata one), to fetch the Picasa Feed.

In my program, the first layer fields of JSON in the feed (e.g. version and encoding in the code below) can be succesfully mapped, so no problem with the feed and connection However I am unable to map the second layer fields into Objects.

I use albumFeed = response.parseAs(AlbumFeed.class); which will invoke the parser.

From the official sample I have this AlbumFeed.java

    public class AlbumFeed {

      // cannot be List
      @Key
      List<AlbumEntry> feed;

      @Key
      String version;

      @Key
      String encoding;

}

With List <AlbumEntry> feed ; I will get this Error, which say List cannot be initiate

06-16 14:35:10.789: E/AndroidRuntime(1129): Caused by: java.lang.IllegalArgumentException: unable to create new instance of class java.util.List because it is an interface and because it has no accessible default constructor

I changed to ArrayList<AlbumEntry> feed no error will be shown but the ArrayList will be empty. Can anyone advise me the correct Way to map JSON to Objects using the given JsonObjectParser?

Many thanks if anyone can answer my question

more CODE

public class newApiActivity extends Activity {

  HttpTransport transport = AndroidHttp.newCompatibleTransport();
  static final JsonFactory JSON_FACTORY = new JacksonFactory();

  public static HttpRequestFactory createRequestFactory(HttpTransport transport) {
    return transport.createRequestFactory(new HttpRequestInitializer() {
      public void initialize(HttpRequest request) {

        // final JsonCParser jsoncparser = new JsonCParser(JSON_FACTORY);

        request.setParser(new JsonObjectParser(GSON_FACTORY));
        // request.setParser(jsoncparser);



        GoogleHeaders headers = new GoogleHeaders();
        headers.setApplicationName("APOD/1.0");
        headers.setGDataVersion("2");
        request.setHeaders(headers);
      }
    });
  }




public void mainLogic(){
HttpRequest buildGetRequest = reqFactory.buildGetRequest(url);

        HttpResponse response = buildGetRequest.execute();
        albumFeed = response.parseAs(AlbumFeed.class);

}

}

where AlbumEntry.java will be something like I am taking the xmlns to test the behavior)

    public class AlbumEntry extends Entry {

  @Key("xmlns")
  public String xmlns;


    }

The JSON Feed itself is like:

{"version":"1.0","encoding":"UTF-8",
"feed":{"xmlns":"http://www.w3.org/2005/Atom","xmlns$gphoto":"http://schemas.google.com/photos/2007,……}}
vincentlcy
  • 1,157
  • 2
  • 17
  • 19

1 Answers1

0

Stupid me. There is nothing to do with the android/ picasa api context.

After reading more on the GSON documentation, according to the json object {} will be a map but not an array . While array can be mapped to List, map can be mapped into an Object or Map

I changed to simple

@Key AlbumEntry feed;

in AlbumFeed.java

It starts working.

A nice reference on GSON is here Converting JSON to Java

Community
  • 1
  • 1
vincentlcy
  • 1,157
  • 2
  • 17
  • 19