Вопрос

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

Это было полезно?

Решение

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

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top