Search in sources :

Example 1 with FeedHandler

use of gov.whitehouse.core.FeedHandler in project wh-app-android by WhiteHouse.

the class FeedManager method updateFeedFromServer.

public static void updateFeedFromServer(String url, String title, String viewType) {
    final BehaviorSubject<Observable<List<FeedItem>>> subject;
    final OkHttpClient client = getClient();
    final Request request = getBaseRequest().url(url).get().build();
    if (sFeedItemsSubject.get(url) == null) {
        sFeedItemsSubject.put(url, BehaviorSubject.create());
    }
    subject = sFeedItemsSubject.get(url);
    subject.onNext(Observable.create((Subscriber<? super List<FeedItem>> op) -> {
        Response response;
        try {
            response = client.newCall(request).execute();
            if (response.code() >= 400) {
                op.onError(new IOException("Response code " + Integer.toString(response.code())));
            } else {
                SAXParser parser = SAXParserFactory.newInstance().newSAXParser();
                FeedHandler handler = new FeedHandler(title, viewType);
                parser.parse(response.body().byteStream(), handler);
                op.onNext(handler.getFeedItems());
            }
            op.onCompleted();
        } catch (Exception e) {
            op.onError(e);
        }
    }));
}
Also used : Response(com.squareup.okhttp.Response) OkHttpClient(com.squareup.okhttp.OkHttpClient) FeedItem(gov.whitehouse.data.model.FeedItem) FeedHandler(gov.whitehouse.core.FeedHandler) Request(com.squareup.okhttp.Request) SAXParser(javax.xml.parsers.SAXParser) IOException(java.io.IOException) Observable(rx.Observable) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException)

Aggregations

OkHttpClient (com.squareup.okhttp.OkHttpClient)1 Request (com.squareup.okhttp.Request)1 Response (com.squareup.okhttp.Response)1 FeedHandler (gov.whitehouse.core.FeedHandler)1 FeedItem (gov.whitehouse.data.model.FeedItem)1 FileNotFoundException (java.io.FileNotFoundException)1 IOException (java.io.IOException)1 SAXParser (javax.xml.parsers.SAXParser)1 Observable (rx.Observable)1