first parse working

This commit is contained in:
aj 2018-03-24 15:43:26 -07:00
parent 19e1d283f8
commit b6037c9846
7 changed files with 112 additions and 12 deletions

View File

@ -1 +1,2 @@
/.project
/bin/

View File

@ -11,4 +11,12 @@ public class Album extends FMObj{
protected ArrayList<Tag> tagList;
protected ArrayList<Track> 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);
}
}

View File

@ -11,4 +11,8 @@ public class Artist extends FMObj{
protected ArrayList<Artist> similarArtists;
protected ArrayList<Tag> tagList;
protected Wiki wiki;
public Artist(String name) {
this.name = name;
}
}

View File

@ -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();
@ -22,16 +30,29 @@ public class Network {
throw new RuntimeException("Failed : HTTP error code : " + conn.getResponseCode());
}
BufferedReader br = new BufferedReader(new InputStreamReader((conn.getInputStream())));
InputStream input = conn.getInputStream();
String output;
System.out.println("Output from Server .... \n");
while ((output = br.readLine()) != null) {
System.out.println(output);
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();
}
conn.disconnect();
} catch (MalformedURLException e) {
e.printStackTrace();

View File

@ -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();

View File

@ -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;
}
}

View File

@ -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);
}
}