簡體   English   中英

使用翻新獲取JSON

[英]Using Retrofit to get JSON

我目前正在嘗試使用Retrofit來構建Muzei擴展,就像Roman在他的示例中使用的示例。

我真的無法適應Retrofit,但這就是我所擁有的

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;

    }
}

基本上,我試圖從http://elliothesp.co.uk/muzei.php中獲取JSON,並將每個項目傳遞給publishArtwork。 它運行異常,因為它無法從JSON提要中找到任何照片,而且我不確定如何使它正常工作

好吧,您的response JSON格式不適合您的界面,應該是:

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

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

    }
}

response

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

然后,在您獲得response迭代的response之后,只需重新編寫代碼即可

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM