package com.AA.Other;
import java.util.ArrayList;
import java.util.List;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.util.Log;
public class {
public static final String URI = "http://www.absolutelyandroid.com/feed/";
public static List<Article>
getArticles(
boolean isBackground,Context callingContext){
if(!isNetworkAvailable(isBackground,callingContext))
return null;
Document doc = getDocument();
if(doc == null)
return null;
try{
ArrayList<Article> articles = new ArrayList<Article>();
NodeList items = doc.getElementsByTagName("item");
for(int i=0;i<items.getLength();i++){
Element el = (Element)items.item(i);
String title = el.getElementsByTagName("title").item(0).getFirstChild().getNodeValue();
String date = el.getElementsByTagName("pubDate").item(0).getFirstChild().getNodeValue();
String url = el.getElementsByTagName("link").item(0).getFirstChild().getNodeValue();
String desc = el.getElementsByTagName("description").item(0).getFirstChild().getNodeValue();
articles.add(new Article(desc,title,date,url));
}
return articles;
}catch(Exception e){
Log.e("AARSS","Error Parsing RSS",e);
return null;
}
}
ConnectivityManager manager = (ConnectivityManager)callingContext.getSystemService(Context.CONNECTIVITY_SERVICE);
if(isBackground && !manager.getBackgroundDataSetting())
return false;
NetworkInfo netInfo = manager.getActiveNetworkInfo();
if(netInfo == null || manager.getActiveNetworkInfo().getState() != NetworkInfo.State.CONNECTED)
return false;
return true;
}
Document doc = null;
try{
DocumentBuilder builder = DocumentBuilderFactory.newInstance()
.newDocumentBuilder();
DefaultHttpClient client = new DefaultHttpClient();
HttpGet request = new HttpGet(URI);
HttpResponse response = client.execute(request);
doc = builder.parse(response.getEntity().getContent());
}catch(java.io.IOException e){
return null;
}catch(SAXException e){
Log.e("AARSS","Parse Exception in RSS feed",e);
return null;
}catch(Exception e){
return null;
}
return doc;
}
}