KarneeKarnay
Newbie
Hi Guys,
I'm a relative novice to Android development. I'm trying to build a simple app that parses xml content from an online database and presents it in a list view to the user. My code has hit a snag though and only gathers the first two results. My code is below.
NodeFinder - This class is looped through repeatedly until the for loop is finished. Am I breaking any rules in using a constructor instead of a method?
I've never attempted to use an xml parser before. I'm not sure what best practice is. Are there any issues you guys can see with my code in general and with only the first two elements being printed?
Below is the XML Data and after that the log.
I'm a relative novice to Android development. I'm trying to build a simple app that parses xml content from an online database and presents it in a list view to the user. My code has hit a snag though and only gathers the first two results. My code is below.
Code:
import android.os.AsyncTask;
import android.util.Log;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import java.net.URL;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.CharacterData;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
public class Downloader extends AsyncTask <String,Void,Void> {
NodeList nodelist;
NodeList gameList;
protected void onPreExecture()
{
System.out.println("THis is doing something");
}
protected Void doInBackground(String... Url)
{
try
{
NodeFinder nodeSearch;
URL url = new URL(Url[0]);
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
//Download the XML File
Document doc = db.parse(new InputSource(url.openStream()));
doc.getDocumentElement().normalize();
//Locate the Tag Name
nodelist = doc.getElementsByTagName("Game");
for (int i = 0; i < nodelist.getLength(); i++)
{
nodeSearch = new NodeFinder(nodelist,i,"GameTitle");
nodeSearch = new NodeFinder(nodelist,i,"Overview");
}
}
catch (Exception e)
{
Log.e("Error", e.getMessage());
e.printStackTrace();
}
System.out.println("This is going on in the background");
return null;
}
protected void onPostExecute(String args)
{
System.out.println("This has been executed!");
}
}
NodeFinder - This class is looped through repeatedly until the for loop is finished. Am I breaking any rules in using a constructor instead of a method?
Code:
public class NodeFinder {
public NodeFinder(NodeList gameHolder, int index, String nodeTag)
{
Element element = (Element) gameHolder.item(index);
NodeList name = element.getElementsByTagName(nodeTag);
Element line = (Element) name.item(0);
System.out.println(nodeTag + " " + line.getChildNodes().item(0).getNodeValue());
}
}
I've never attempted to use an xml parser before. I'm not sure what best practice is. Are there any issues you guys can see with my code in general and with only the first two elements being printed?
Below is the XML Data and after that the log.
Code:
<Data>
<baseImgUrl>http://thegamesdb.net/banners/</baseImgUrl>
<Game>
<id>3597</id>
<GameTitle>Halo 4</GameTitle>
<PlatformId>15</PlatformId>
<Platform>Microsoft Xbox 360</Platform>
<ReleaseDate>11/06/2012</ReleaseDate>
<Overview>...</Overview>
<ESRB>M - Mature</ESRB>
<Genres>...</Genres>
<Players>2</Players>
<Co-op>Yes</Co-op>
<Youtube>http://www.youtube.com/watch?v=oBk3c4UIcGw</Youtube>
<Publisher>Microsoft</Publisher>
<Developer>343 Industries</Developer>
<Rating>8.25</Rating>
<Images>...</Images>
</Game>
<Game>
<id>9</id>
<GameTitle>Halo 2</GameTitle>
<PlatformId>1</PlatformId>
<Platform>PC</Platform>
<ReleaseDate>05/31/2007</ReleaseDate>
<Overview>
Halo 2 is the sequel to the highly successful and critically acclaimed Halo®: Combat Evolved. In Halo 2, the saga continues as Master Chief—a genetically enhanced super-soldier—is the only thing standing between the relentless Covenant and the destruction of all humankind.
</Overview>
<ESRB>M - Mature</ESRB>
<Genres>
<genre>Action</genre>
<genre>Shooter</genre>
</Genres>
<Players>2</Players>
<Co-op>No</Co-op>
<Youtube>http://www.youtube.com/watch?v=Zz6FNKawJBc</Youtube>
<Publisher>Microsoft</Publisher>
<Developer>Bungie</Developer>
<Rating>7.25</Rating>
<Similar>
<SimilarCount>1</SimilarCount>
<Game>
<id>6079</id>
<PlatformId>14</PlatformId>
</Game>
</Similar>
<Images>
<fanart>
<original width="1280" height="720">fanart/original/9-1.jpg</original>
<thumb>fanart/thumb/9-1.jpg</thumb>
</fanart>
<fanart>
<original width="1280" height="720">fanart/original/9-2.jpg</original>
<thumb>fanart/thumb/9-2.jpg</thumb>
</fanart>
<fanart>
<original width="1920" height="1080">fanart/original/9-3.jpg</original>
<thumb>fanart/thumb/9-3.jpg</thumb>
</fanart>
<boxart side="back" width="1520" height="2156" thumb="boxart/thumb/original/back/9-1.jpg">boxart/original/back/9-1.jpg</boxart>
<boxart side="front" width="1527" height="2156" thumb="boxart/thumb/original/front/9-2.jpg">boxart/original/front/9-2.jpg</boxart>
</Images>
</Game>
<Game>
<id>90</id>
<GameTitle>Halo 3</GameTitle>
<PlatformId>15</PlatformId>
<Platform>Microsoft Xbox 360</Platform>
<ReleaseDate>09/25/2007</ReleaseDate>
<Overview>
Master Chief returns to a Covenant Dominated Earth on a mission to kill the final alien leader. Meanwhile, the Arbiter, Johnson, and Keyes form a loose alliance and escape from Delta Halo. The Covenant is ripped in civil war, and the Elites along with a handful of other alien races become sympathetic to the human cause. Delta Halo's impromptu dis-activation has brought all of the Halos to a "remote activation phase". They can be activated from a facility called the ark, which happens to be on Earth.
</Overview>
<ESRB>M - Mature</ESRB>
<Genres>
<genre>Shooter</genre>
</Genres>
<Players>2</Players>
<Co-op>Yes</Co-op>
<Publisher>Microsoft Game Studios</Publisher>
<Developer>Bungie</Developer>
<Rating>7.375</Rating>
<Images>
<fanart>
<original width="1920" height="1080">fanart/original/90-1.jpg</original>
<thumb>fanart/thumb/90-1.jpg</thumb>
</fanart>
<fanart>
<original width="1920" height="1080">fanart/original/90-2.jpg</original>
<thumb>fanart/thumb/90-2.jpg</thumb>
</fanart>
<fanart>
<original width="1920" height="1080">fanart/original/90-3.jpg</original>
<thumb>fanart/thumb/90-3.jpg</thumb>
</fanart>
<fanart>
<original width="1920" height="1080">fanart/original/90-4.jpg</original>
<thumb>fanart/thumb/90-4.jpg</thumb>
</fanart>
<boxart side="back" width="1518" height="2148" thumb="boxart/thumb/original/back/90-1.jpg">boxart/original/back/90-1.jpg</boxart>
<boxart side="front" width="1530" height="2148" thumb="boxart/thumb/original/front/90-1.jpg">boxart/original/front/90-1.jpg</boxart>
<clearlogo width="400" height="300">clearlogo/90.png</clearlogo>
</Images>
</Game>
Code:
01-11 23:22:17.654 420-447/com.example.karnee.myapplication E/Error: Attempt to invoke interface method 'org.w3c.dom.NodeList org.w3c.dom.Element.getChildNodes()' on a null object reference
01-11 23:22:17.654 420-447/com.example.karnee.myapplication W/System.err: java.lang.NullPointerException: Attempt to invoke interface method 'org.w3c.dom.NodeList org.w3c.dom.Element.getChildNodes()' on a null object reference
01-11 23:22:17.655 420-447/com.example.karnee.myapplication W/System.err: at com.example.karnee.myapplication.NodeFinder.<init>(NodeFinder.java:17)
01-11 23:22:17.655 420-447/com.example.karnee.myapplication W/System.err: at com.example.karnee.myapplication.Downloader.doInBackground(Downloader.java:48)
01-11 23:22:17.655 420-447/com.example.karnee.myapplication W/System.err: at com.example.karnee.myapplication.Downloader.doInBackground(Downloader.java:22)
01-11 23:22:17.655 420-447/com.example.karnee.myapplication W/System.err: at android.os.AsyncTask$2.call(AsyncTask.java:295)
01-11 23:22:17.655 420-447/com.example.karnee.myapplication W/System.err: at java.util.concurrent.FutureTask.run(FutureTask.java:237)
01-11 23:22:17.655 420-447/com.example.karnee.myapplication W/System.err: at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:234)
01-11 23:22:17.656 420-447/com.example.karnee.myapplication W/System.err: at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
01-11 23:22:17.656 420-447/com.example.karnee.myapplication W/System.err: at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
01-11 23:22:17.656 420-447/com.example.karnee.myapplication W/System.err: at java.lang.Thread.run(Thread.java:818)
01-11 23:22:17.656 420-447/com.example.karnee.myapplication I/System.out: This is going on in the background