Problem writing to an xml file
Hello,
I am currently trying to make use of last.fm's api, with the following code:
Code:
public void RetriveSongs(){
try
{
URL addr = new URL("http://ws.audioscrobbler.com/2.0/?method=user.getrecenttracks&user="
+ username + "&api_key=cdb1f1d67c2deba8eab843a922da857c&limit=200");
URLConnection conn = addr.openConnection();
conn.setDoOutput(true);
BufferedReader laino = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String line;
FileOutputStream fout = new FileOutputStream("songlist.xml");
PrintStream xml = new PrintStream(fout);
while((line = laino.readLine()) != null){
xml.println(line);
}
xml.close();
laino.close();
}
catch (Exception e) {
}
}
Username is a string private to the class where I have this function. Basically, last.fm's api answers to this POST request with an xml code. So I use all those streams and the loop to write that xml to a file on my local host, line by line, until I get an empty line - meaning the xml is over. However, under unknown circumstances (I am unsure when it works and when it doesn't), writing to the file will finish before the xml code from the api is over, resulting in a badly constructed xml file that ends abruptly. I compared the faulty file with the xml input I get when I simply use the URL from the above code in my browser, and there is no empty line at the place where the writing stops, and the browser shows the entire xml code just fine. If anyone can see the problem in this function, the help will be appreciated.
Re: Problem writing to an xml file
Quote:
If anyone can see the problem in this function
First, NEVER let exceptions fall through - the least you should do is print the stack trace
Code:
try{
//do something
}catch(Exception e){
e.printStackTrace();
}
Second, the code you posted is not even compilable, and even if the quotes were escaped in the String you pass to construct a URL to make it compilable, a MalformedURLException would be thrown.
Re: Problem writing to an xml file
Quote:
Originally Posted by
doWhile
First, NEVER let exceptions fall through - the least you should do is print the stack trace
Noted and corrected, thank you. Didn't really think about that at the time. However, the code compiles, no exceptions thrown, and the function works correctly to some point. Maybe the problem could come from the repeated use of the same file? First few tries I checked it and it seem to have been overriden completely with the new version, but now I am wondering if there could be a side effect.
Re: Problem writing to an xml file
Quote:
However, the code compiles, no exceptions thrown
Something happened to the String passed to URL - not in your code, but in your first post (perhaps the forum software is misparsing url's within code tags, which magically rectifies itself over time). As for your problem, you mention there are 'unknown circumstances' - have you reproduced the 'unknown circumstances' with updated code that catches exceptions?
Re: Problem writing to an xml file
Since you're not planning on using the stuff coming back from the connection, why not simply stream the bytes directly and avoid the buffering and string conversion?
It could be something to do with that causing problems.
So use the InputStream directly, and feed the byte[] to a FileOutputStream.
Code:
byte[] buffer = new byte[1024]; // pick whatever value seems fine to you
InputStream is = conn.getInputStream();
while (is.read(buffer) > -1) {
// write to the output stream
}
Re: Problem writing to an xml file
I fixed the problem, it turned out to be totally unrelated. I am still unsure what exactly was happening, but I have another class with similar function that requests other information the same way. I discovered that in the code I was using to test how the classes and their function works, the two classes' functions start almost simultaneously. As soon as I prevented them from running at the same time (a wild guess), everything went back to normal. Thank you for your time, and sorry for wasting it.