first parse working
This commit is contained in:
parent
19e1d283f8
commit
b6037c9846
1
fmframework/.gitignore
vendored
1
fmframework/.gitignore
vendored
@ -1 +1,2 @@
|
|||||||
/.project
|
/.project
|
||||||
|
/bin/
|
||||||
|
@ -11,4 +11,12 @@ public class Album extends FMObj{
|
|||||||
protected ArrayList<Tag> tagList;
|
protected ArrayList<Tag> tagList;
|
||||||
protected ArrayList<Track> trackList;
|
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);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -11,4 +11,8 @@ public class Artist extends FMObj{
|
|||||||
protected ArrayList<Artist> similarArtists;
|
protected ArrayList<Artist> similarArtists;
|
||||||
protected ArrayList<Tag> tagList;
|
protected ArrayList<Tag> tagList;
|
||||||
protected Wiki wiki;
|
protected Wiki wiki;
|
||||||
|
|
||||||
|
public Artist(String name) {
|
||||||
|
this.name = name;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -2,16 +2,24 @@ package sarsoo.fmframework.net;
|
|||||||
|
|
||||||
import java.io.BufferedReader;
|
import java.io.BufferedReader;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
import java.io.InputStream;
|
||||||
import java.io.InputStreamReader;
|
import java.io.InputStreamReader;
|
||||||
import java.net.HttpURLConnection;
|
import java.net.HttpURLConnection;
|
||||||
import java.net.MalformedURLException;
|
import java.net.MalformedURLException;
|
||||||
import java.net.URL;
|
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 class Network {
|
||||||
public static void apiAlbumInfoCall(String artist, String album, String username) {
|
public static void apiAlbumInfoCall(String artist, String album, String username) {
|
||||||
try {
|
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());
|
artist, album, username, Key.getKey());
|
||||||
URL url = new URL(urlString);
|
URL url = new URL(urlString);
|
||||||
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
|
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
|
||||||
@ -21,16 +29,29 @@ public class Network {
|
|||||||
if (conn.getResponseCode() != 200) {
|
if (conn.getResponseCode() != 200) {
|
||||||
throw new RuntimeException("Failed : HTTP error code : " + conn.getResponseCode());
|
throw new RuntimeException("Failed : HTTP error code : " + conn.getResponseCode());
|
||||||
}
|
}
|
||||||
|
|
||||||
BufferedReader br = new BufferedReader(new InputStreamReader((conn.getInputStream())));
|
InputStream input = conn.getInputStream();
|
||||||
|
|
||||||
String output;
|
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
|
||||||
System.out.println("Output from Server .... \n");
|
DocumentBuilder dBuilder;
|
||||||
while ((output = br.readLine()) != null) {
|
try {
|
||||||
System.out.println(output);
|
dBuilder = dbFactory.newDocumentBuilder();
|
||||||
}
|
try {
|
||||||
|
Document doc = dBuilder.parse(input);
|
||||||
conn.disconnect();
|
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) {
|
} catch (MalformedURLException e) {
|
||||||
|
|
||||||
|
@ -9,7 +9,7 @@ import java.net.URL;
|
|||||||
|
|
||||||
public class TestCall {
|
public class TestCall {
|
||||||
|
|
||||||
public static void main(String [] args) {
|
public static void test() {
|
||||||
try {
|
try {
|
||||||
URL url = new URL("http://ws.audioscrobbler.com/2.0/?method=artist.getInfo&artist=Mastodon&format=json&api_key=" + Key.getKey());
|
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();
|
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
|
||||||
|
45
fmframework/src/sarsoo/fmframework/parser/AlbumParser.java
Normal file
45
fmframework/src/sarsoo/fmframework/parser/AlbumParser.java
Normal 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;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
21
fmframework/src/sarsoo/fmframework/test/NetworkTest.java
Normal file
21
fmframework/src/sarsoo/fmframework/test/NetworkTest.java
Normal 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);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user