I have a controller I am trying to access a Xml file to create an arraylist. But it throws the error in the title when trying to access the URL - this is strange because the code is working fine in a normal java class but only in my servlet this is happening.
Can anyone provide some help or advice ? Below is some of the code of the servlet:
@WebServlet
public class RSSController extends HttpServlet {
private static ArrayList<RSSFeed> list = new ArrayList<RSSFeed>();
public void service(HttpServletRequest req, HttpServletResponse res) throws IOException {
String test = "TEST TEXT";
PrintWriter out = res.getWriter();
out.println(test);
out.println("RSS Function: " + readRSSFeed("https://www.health.gov.au/news/rss.xml"));
out.println(list.size());
System.out.println("hi");
for(int i = 0; i < list.size(); i++){
RSSFeed temp = list.get(i);
out.println(temp.getLink());
out.println(temp.getTitle());
out.println(i);
}
}
public static String readRSSFeed(String urlAddress){
System.out.println("readRSSFeed Function has started");
try{
System.out.println("Try loop has initated");
URL rssUrl = new URL (urlAddress);
BufferedReader in = new BufferedReader(new InputStreamReader(rssUrl.openStream()));
String sourceCode = "";
String line;
while((line=in.readLine())!=null){
if(line.contains("<title>")){
System.out.println(line);
int firstPos = line.indexOf("<title>");
String temp = line.substring(firstPos);
temp=temp.replace("<title>","");
int lastPos = temp.indexOf("</title>");
temp = temp.substring(0,lastPos);
sourceCode +=temp+ "\n" ;
RSSFeed tempRSS = new RSSFeed(temp, "testDesc", "testLink");
list.add(tempRSS);
System.out.println(tempRSS.getTitle());
}
}
in.close();
return sourceCode;
//If URL does not work
} catch (MalformedURLException ue){
System.out.println("Malformed URL");
} catch (IOException ioe){ //Any other issues
System.out.println("Something went wrong reading the contents");
ioe.printStackTrace();
}
return null;
}
Aucun commentaire:
Enregistrer un commentaire