2

I'm currently trying to build a Muzei extension using Retrofit like the example Roman uses in his example.

I can't really get my head around Retrofit, however this is what I have

ArtSource.java

public class ArtSource extends RemoteMuzeiArtSource {
    private static final String TAG = "The Muzei Collection";
    private static final String SOURCE_NAME = "The Muzei Collection";

    private static final int ROTATE_TIME_MILLIS = 3 * 60 * 60 * 1000; // rotate every 3 hours

    public ArtSource() {
        super(SOURCE_NAME);
    }

    @Override
    public void onCreate() {
        super.onCreate();
        setUserCommands(BUILTIN_COMMAND_ID_NEXT_ARTWORK);
    }

    @Override
    protected void onTryUpdate(int reason) throws RetryException {
        String currentToken = (getCurrentArtwork() != null) ? getCurrentArtwork().getToken() : null;

        RestAdapter restAdapter = new RestAdapter.Builder()
                .setServer("http://elliothesp.co.uk")
                .build();

        ArtService service = restAdapter.create(ArtService.class);
        PhotosResponse response = service.getPopularPhotos();

   if (response == null || response.photos == null) {
        throw new RetryException();
    }

    if (response.photos.size() == 0) {
        Log.w(TAG, "No photos returned from API.");
        scheduleUpdate(System.currentTimeMillis() + ROTATE_TIME_MILLIS);
        return;
    }

        Random random = new Random();
        Photo photo;
        String token;
        while (true) {
            photo = response.photos.get(random.nextInt(response.photos.size()));
            token = Integer.toString(photo.id);
            if (response.photos.size() <= 1 || !TextUtils.equals(token, currentToken)) {
                break;
            }
        }

        publishArtwork(new Artwork.Builder()
                .title(photo.title)
                .byline(photo.user)
                .imageUri(Uri.parse(photo.url))
                .token(token)
                .viewIntent(new Intent(Intent.ACTION_VIEW,
                        Uri.parse("http://www.google.com")))
                .build());

        scheduleUpdate(System.currentTimeMillis() + ROTATE_TIME_MILLIS);
    }
}

ArtService.java

interface ArtService {
    @GET("/muzei.php")
    PhotosResponse getPopularPhotos();

    static class PhotosResponse {
        List<Photo> photos;
    }

    static class Photo {
        int id;
        String user;
        String title;
        String url;

    }
}

Basically, I'm trying to take the JSON from http://elliothesp.co.uk/muzei.php and pass each item to publishArtwork. It runs the exception as it cannot find any photos from the JSON feed and I am not sure how to get that working

ianhanniballake
  • 191,609
  • 30
  • 470
  • 443
K20GH
  • 6,032
  • 20
  • 78
  • 118
  • http://pastebin.com/xAVp3CKs – K20GH Feb 14 '14 at 21:19
  • Is this the whole log? And what is at the line 62 of your ArtSource.java (it's not listed completely) – nikis Feb 14 '14 at 21:37
  • also you can try to make your interface public – nikis Feb 14 '14 at 21:43
  • photo = response.photos.get(random.nextInt(response.photos.size())); – K20GH Feb 14 '14 at 22:48
  • btw, the example to pull from the 500px.com api json is here: https://github.com/romannurik/muzei/tree/master/example-source-500px/src/main/java/com/example/muzei/examplesource500px – K20GH Feb 14 '14 at 22:48
  • I've added in an exception, it no longer crashes, as it now logs an exception saying that no photo was found at the json feed. – K20GH Feb 14 '14 at 22:59

1 Answers1

4

Well, JSON format of your response doesn't fit your interface, it should be:

interface service {
    @GET("/muzei.php")
    Map<String,Photo> getPopularPhotos();

    static class Photo {
        int id;
        String user;
        String title;
        String url;

    }
}

And response:

Map<String, Photo> response = service.getPopularPhotos();

Then just re-write your code after you get response to iterate through it

nikis
  • 11,166
  • 2
  • 35
  • 45
  • Thanks! I had a feeling that was the issue but I didn't understand it will enough. Your answer just made it all very clear though – K20GH Feb 15 '14 at 09:30