I am trying to use the simple-framework in android to read a file which I take from the nasa feed. The nasa file is here: http://www.nasa.gov/rss/dyn/image_of_the_day.rss
The way I go around this is by using the following code:
Serializer serialezr = new Persister();
String file_path = Environment.getExternalStorageDirectory().getAbsolutePath() + "/nasa";
try{
File dir = new File(file_path);
if(!dir.exists())
{
dir.mkdirs();
}
File nasa = new File(file_path,filename);
if(nasa.exists())
{
nasa.delete();
}
nasa.createNewFile();
source = new File(file_path + "/" + filename);
}
catch(Exception e)
{
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);
e.printStackTrace(pw);
view.setText(sw.toString() + "here");
}
The above code creates the nasa.xml file in a directory which I will later use
DefaultHttpClient httpclient = new DefaultHttpClient();
HttpGet httpget = new HttpGet("http://www.nasa.gov/rss/dyn/image_of_the_day.rss");
HttpResponse response = httpclient.execute(httpget);
HttpEntity ht = response.getEntity();
BufferedHttpEntity buf = new BufferedHttpEntity(ht);
InputStream is = buf.getContent();
BufferedReader r = new BufferedReader(new InputStreamReader(is));
StringBuilder total = new StringBuilder();
String line;
while ((line = r.readLine()) != null) {
total.append(line + "\n");
}
FileUtils.writeStringToFile(source,total.toString());
The above code actually gets the file from their website using a DefaultHttpClient.
"Googling" this I have discovered that the xml file might not be written properly? Looking at the xml file from nasa I cannot see any "<>" tags which appeared to be the most common error that throws this exception.
UPDATES
- Instead of using BufferedReader and appending a line after a line I have used InputStreamReader to read each character. It runs out of memory
- Looks like if I take out the ImageView from the layout everything works even the xml parsing.