Skip to content

Instantly share code, notes, and snippets.

@omarmiatello
Last active February 27, 2016 14:00
Show Gist options
  • Save omarmiatello/6669487 to your computer and use it in GitHub Desktop.
Save omarmiatello/6669487 to your computer and use it in GitHub Desktop.
RssRequest for Volley (Android lib)
/**
* Copyright 2013 Omar Miatello - omar.miatello@justonetouch.it
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package it.justonetouch.utils.volleytoolbox;
import com.android.volley.NetworkResponse;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.Response.ErrorListener;
import com.android.volley.Response.Listener;
import com.android.volley.toolbox.HttpHeaderParser;
import org.mcsoxford.rss.RSSConfig;
import org.mcsoxford.rss.RSSFeed;
import org.mcsoxford.rss.RSSParser;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
public class RssRequest extends Request<RSSFeed> {
private final RSSParser mParser;
private final Listener<RSSFeed> mListener;
public RssRequest(int method, String url, Listener<RSSFeed> listener, ErrorListener errorListener) {
super(Method.GET, url, errorListener);
this.mListener = listener;
mParser = new RSSParser(new RSSConfig());
}
public RssRequest(int method, String url, Listener<RSSFeed> listener, ErrorListener errorListener, RSSParser parser) {
super(Method.GET, url, errorListener);
this.mListener = listener;
mParser = parser;
}
@Override
protected void deliverResponse(RSSFeed response) {
mListener.onResponse(response);
}
@Override
protected Response<RSSFeed> parseNetworkResponse(NetworkResponse response) {
InputStream is = new ByteArrayInputStream(response.data);
return Response.success(mParser.parse(is), HttpHeaderParser.parseCacheHeaders(response));
}
}
import it.justonetouch.utils.volleytoolbox.RssRequest;
import org.mcsoxford.rss.RSSFeed;
import org.mcsoxford.rss.RSSItem;
class RssRequestExample {
private RSSFeed downloadFeed(String url_news_rss) throws ExecutionException, InterruptedException {
Log.i(TAG, "Retriving RSS feed from: " + url_news_rss);
RequestQueue queue = Volley.newRequestQueue(getContext());
RequestFuture<RSSFeed> future = RequestFuture.newFuture();
RssRequest rssRequest = new RssRequest(Request.Method.GET, url_news_rss, future, future);
queue.add(rssRequest);
RSSFeed feed = future.get(); // this will block
Log.i(TAG, "Parsing complete. Found " + feed.getItems().size() + " entries");
return feed;
}
}
import it.justonetouch.utils.volleytoolbox.RssRequest;
import org.mcsoxford.rss.RSSFeed;
import org.mcsoxford.rss.RSSItem;
class RssRequestExampleWithTagManagerUtils {
private static final String URL_NEWS_RSS = "url_rss";
private RSSFeed downloadFeed() throws ExecutionException, InterruptedException {
Log.i(TAG, "Looking for " + URL_NEWS_RSS);
Container container = TagManagerUtils.getContainer(getContext());
String url_news_rss = container.getString(URL_NEWS_RSS);
Log.i(TAG, "Retriving RSS feed from: " + url_news_rss);
RequestQueue queue = Volley.newRequestQueue(getContext());
RequestFuture<RSSFeed> future = RequestFuture.newFuture();
RssRequest rssRequest = new RssRequest(Request.Method.GET, url_news_rss, future, future);
queue.add(rssRequest);
RSSFeed feed = future.get(); // this will block
Log.i(TAG, "Parsing complete. Found " + feed.getItems().size() + " entries");
return feed;
}
}
@waura
Copy link

waura commented Feb 27, 2016

Following code may be wrong.
public RssRequest(int method, String url, Listener listener, ErrorListener errorListener) {
super(Method.GET, url, errorListener);
....
}

It may be better to following.
public RssRequest(int method, String url, Listener listener, ErrorListener errorListener) {
super(method, url, errorListener);
....
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment