diff --git a/fmframework/.gitignore b/fmframework/.gitignore index d8fe4fa..660f56d 100644 --- a/fmframework/.gitignore +++ b/fmframework/.gitignore @@ -1 +1,2 @@ /.project +/bin/ diff --git a/fmframework/src/sarsoo/fmframework/music/Album.java b/fmframework/src/sarsoo/fmframework/music/Album.java index 3be03a8..6c4a0ce 100644 --- a/fmframework/src/sarsoo/fmframework/music/Album.java +++ b/fmframework/src/sarsoo/fmframework/music/Album.java @@ -11,4 +11,12 @@ public class Album extends FMObj{ protected ArrayList tagList; protected ArrayList trackList; + public Album(String name, String url, String mbid, int id, String artist) { + this.name = name; + this.url = url; + this.mbid = mbid; + this.id = id; + this.artist = new Artist(artist); + } + } diff --git a/fmframework/src/sarsoo/fmframework/music/Artist.java b/fmframework/src/sarsoo/fmframework/music/Artist.java index 223c7d6..fd98c30 100644 --- a/fmframework/src/sarsoo/fmframework/music/Artist.java +++ b/fmframework/src/sarsoo/fmframework/music/Artist.java @@ -11,4 +11,8 @@ public class Artist extends FMObj{ protected ArrayList similarArtists; protected ArrayList tagList; protected Wiki wiki; + + public Artist(String name) { + this.name = name; + } } diff --git a/fmframework/src/sarsoo/fmframework/net/Network.java b/fmframework/src/sarsoo/fmframework/net/Network.java index 7a6c41d..96516ea 100644 --- a/fmframework/src/sarsoo/fmframework/net/Network.java +++ b/fmframework/src/sarsoo/fmframework/net/Network.java @@ -2,16 +2,24 @@ package sarsoo.fmframework.net; import java.io.BufferedReader; import java.io.IOException; +import java.io.InputStream; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.URL; +import javax.xml.parsers.DocumentBuilder; +import javax.xml.parsers.DocumentBuilderFactory; +import javax.xml.parsers.ParserConfigurationException; + +import org.w3c.dom.Document; +import org.xml.sax.SAXException; + public class Network { public static void apiAlbumInfoCall(String artist, String album, String username) { try { - String urlString = String.format("http://ws.audioscrobbler.com/2.0/?method=album.getInfo&artist=%s&album=%s&autocorrect=1&username=%s&format=json&api_key=%s", + String urlString = String.format("http://ws.audioscrobbler.com/2.0/?method=album.getInfo&artist=%s&album=%s&autocorrect=1&username=%s&api_key=%s", artist, album, username, Key.getKey()); URL url = new URL(urlString); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); @@ -21,16 +29,29 @@ public class Network { if (conn.getResponseCode() != 200) { throw new RuntimeException("Failed : HTTP error code : " + conn.getResponseCode()); } - - BufferedReader br = new BufferedReader(new InputStreamReader((conn.getInputStream()))); - - String output; - System.out.println("Output from Server .... \n"); - while ((output = br.readLine()) != null) { - System.out.println(output); - } - - conn.disconnect(); + + InputStream input = conn.getInputStream(); + + DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance(); + DocumentBuilder dBuilder; + try { + dBuilder = dbFactory.newDocumentBuilder(); + try { + Document doc = dBuilder.parse(input); + conn.disconnect(); + doc.getDocumentElement().normalize(); + + System.out.println("Root Element: " + doc.getDocumentElement().getNodeName()); + + + } catch (SAXException e) { + e.printStackTrace(); + } catch (IOException e) { + e.printStackTrace(); + } + } catch (ParserConfigurationException e) { + e.printStackTrace(); + } } catch (MalformedURLException e) { diff --git a/fmframework/src/sarsoo/fmframework/net/TestCall.java b/fmframework/src/sarsoo/fmframework/net/TestCall.java index 6ae48e6..3ef3ed2 100644 --- a/fmframework/src/sarsoo/fmframework/net/TestCall.java +++ b/fmframework/src/sarsoo/fmframework/net/TestCall.java @@ -9,7 +9,7 @@ import java.net.URL; public class TestCall { - public static void main(String [] args) { + public static void test() { try { URL url = new URL("http://ws.audioscrobbler.com/2.0/?method=artist.getInfo&artist=Mastodon&format=json&api_key=" + Key.getKey()); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); diff --git a/fmframework/src/sarsoo/fmframework/parser/AlbumParser.java b/fmframework/src/sarsoo/fmframework/parser/AlbumParser.java new file mode 100644 index 0000000..0167305 --- /dev/null +++ b/fmframework/src/sarsoo/fmframework/parser/AlbumParser.java @@ -0,0 +1,45 @@ +package sarsoo.fmframework.parser; + +import javax.xml.parsers.*; + +import java.io.IOException; +import java.io.InputStream; + +import sarsoo.fmframework.music.Album; + +import org.w3c.dom.Document; +import org.w3c.dom.NamedNodeMap; +import org.w3c.dom.Node; +import org.w3c.dom.NodeList; +import org.xml.sax.SAXException; + + +public class AlbumParser { + + public static Album parseAlbum(InputStream input){ + DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance(); + DocumentBuilder dBuilder; + try { + dBuilder = dbFactory.newDocumentBuilder(); + try { + Document doc = dBuilder.parse(input); + doc.getDocumentElement().normalize(); + + System.out.println("Root Element: " + doc.getDocumentElement().getNodeName()); + } catch (SAXException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } catch (ParserConfigurationException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + + return null; + + } + +} diff --git a/fmframework/src/sarsoo/fmframework/test/NetworkTest.java b/fmframework/src/sarsoo/fmframework/test/NetworkTest.java new file mode 100644 index 0000000..a8945dc --- /dev/null +++ b/fmframework/src/sarsoo/fmframework/test/NetworkTest.java @@ -0,0 +1,21 @@ +package sarsoo.fmframework.test; + +import static org.junit.jupiter.api.Assertions.*; + +import java.io.InputStream; + +import org.junit.jupiter.api.Test; + +import sarsoo.fmframework.net.Network; +import sarsoo.fmframework.parser.AlbumParser; + +class NetworkTest { + + @Test + void test() { + Network.apiAlbumInfoCall("Mastodon", "Leviathan", "sarsoo"); + //System.out.println(xml); + //AlbumParser.parseAlbum(xml); + } + +}