次のコードを使用してURLからxmlを取得する際に問題が発生します。
private static String getAlbumArt(String artistName, String albumName){
try{
XMLParser xml_parser = new XMLParser();
String xml = xml_parser.getXmlFromUrl(getAlbumURL(artistName, albumName));
Document doc = xml_parser.getDomElement(xml);
NodeList N = doc.getElementsByTagName("album");
Node node = N.item(0);
NodeList N2 = node.getChildNodes();
System.out.println("1------");
for (int i = 0; i < N2.getLength(); i++) {
Node detailNode = N2.item(i);
if (detailNode.getNodeType() == Node.ELEMENT_NODE)
{
System.out.println("2------");
if (detailNode.getNodeName().equalsIgnoreCase("image"))
{
String sizeVal = ((Element) detailNode).getAttribute("size");
String url = detailNode.getTextContent();
if (sizeVal.equalsIgnoreCase("large")) {
return url;
}
}
}
}
}
catch (Exception e){
}
return null;
}
上記のコードで呼び出すxml関数:
public String getXmlFromUrl(String url) {
String xml = null;
try {
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
xml = EntityUtils.toString(httpEntity);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return xml;
}
getAlbumURL:
public static String getAlbumURL(String artist, String album){
return URL_METHOD + METHOD_GETALBUM + AMPERSAND
+ API_KEY + AMPERSAND
+ PARAM_ARTIST + artist + AMPERSAND
+ PARAM_ALBUM + album;
}
XMLparser:
パブリッククラスXMLParser{
// constructor
public XMLParser() {
}
//Get XML from URL
public String getXmlFromUrl(String url) {
String xml = null;
try {
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
xml = EntityUtils.toString(httpEntity);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return xml;
}
//Get dom element
public Document getDomElement(String xml){
Document doc = null;
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
try {
DocumentBuilder db = dbf.newDocumentBuilder();
InputSource is = new InputSource();
is.setCharacterStream(new StringReader(xml));
doc = db.parse(is);
} catch (ParserConfigurationException e) {
Log.e("Error: ", e.getMessage());
return null;
} catch (SAXException e) {
Log.e("Error: ", e.getMessage());
return null;
} catch (IOException e) {
Log.e("Error: ", e.getMessage());
return null;
}
return doc;
}
//Get nod element
public final String getElementValue(Node elem ) {
Node child;
if( elem != null){
if (elem.hasChildNodes()){
for( child = elem.getFirstChild(); child != null; child = child.getNextSibling() ){
if( child.getNodeType() == Node.TEXT_NODE ){
return child.getNodeValue();
}
}
}
}
return "";
}
//Get element value
public String getValue(Element item, String str) {
NodeList nlList = item.getElementsByTagName(str).item(0).getChildNodes();
Node nValue = (Node) nlList.item(0);
return nValue.getNodeValue();
}
}
何か案は ?何が悪いのか真剣にわかりません。以前にこれを使用しましたが、機能しました。