Java tutorial
/* Copyright (C) 2014 Rany Albeg Wein - rany.albeg@gmail.com * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License * as published by the Free Software Foundation; either version 2 * of the License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ package com.rany.albeg.wein.rssr; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.security.InvalidParameterException; import javax.xml.parsers.ParserConfigurationException; import javax.xml.parsers.SAXParser; import javax.xml.parsers.SAXParserFactory; import org.apache.http.HttpResponse; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpGet; import org.apache.http.impl.client.DefaultHttpClient; import org.xml.sax.InputSource; import org.xml.sax.SAXException; import org.xml.sax.XMLReader; import android.os.AsyncTask; import android.util.Log; import com.rany.albeg.wein.rssr.interfaces.RssReaderListener; import com.rany.albeg.wein.rssr.models.RssReaderXMLHandler; public class RssReader { private static final String TAG = "RssReader"; private XMLReader mXMLReader; private boolean mReadyToRead; private RssReaderListener mRssReaderListener; private RssReaderXMLHandler mRssReaderXmlHandler; public RssReader(RssReaderListener readCompleteListener) { init(readCompleteListener); } private void init(RssReaderListener readCompleteListener) { mReadyToRead = true; mRssReaderListener = readCompleteListener; mRssReaderXmlHandler = new RssReaderXMLHandler(); try { SAXParserFactory factory = SAXParserFactory.newInstance(); SAXParser parser = factory.newSAXParser(); mXMLReader = parser.getXMLReader(); mXMLReader.setContentHandler(mRssReaderXmlHandler); } catch (ParserConfigurationException e) { Log.e(TAG, "ParserConfigurationException"); } catch (SAXException e) { Log.e(TAG, "SAXException init()"); } } public void read(String rssFeedUrl) throws InvalidParameterException { if (rssFeedUrl == null || rssFeedUrl.trim().length() == 0) throw new InvalidParameterException("You are passing an empty URL String to read(String rssFeedUrl)."); if (mReadyToRead) new ReadRssAsyncTask().execute(rssFeedUrl); else Log.w(TAG, "RSS read refused. read() did not finish yet."); } private class ReadRssAsyncTask extends AsyncTask<String, Void, Void> { private static final String TAG = "ReadRssAsyncTask"; @Override protected void onPreExecute() { mReadyToRead = false; if (mRssReaderListener != null) { mRssReaderListener.onRssReadStart(); } } @Override protected Void doInBackground(String... params) { try { HttpClient httpClient = new DefaultHttpClient(); HttpGet httpGetRequest = new HttpGet(params[0]); HttpResponse httpRespnose = httpClient.execute(httpGetRequest); BufferedReader bufferedReader = new BufferedReader( new InputStreamReader(httpRespnose.getEntity().getContent())); mXMLReader.parse(new InputSource(bufferedReader)); } catch (IOException e) { Log.d(TAG, "IOException in doInBackground()"); } catch (SAXException e) { Log.d(TAG, "SAXException in doInBackground()"); } return null; } @Override protected void onPostExecute(Void result) { if (mRssReaderListener != null) { mRssReaderListener.onRssReadComplete(mRssReaderXmlHandler.getChannel()); } mReadyToRead = true; } } }