androidrookie123
Lurker
Currently I was trying to get the location of the song being played in this XML
format
<rss version="2.0">
<channel>
<item>
<song data="Academia"/>
<artist data="Sia"/>
<album data="Some"/>
<lat data="1.38499"/>
<lng data="103.744092"/>
<playtime data="2010-06-27 12:01:27"/>
</item>
</channel>
</rss>
I tried to get the location but my listview shows me nothing instead
my codes doesn't have errors so I might be missing something
that can display the location of the song played.
Help is apperciated. thanks.
this is my following codes
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
import android.app.ListActivity;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.util.Log;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
public class lastfivelocation extends ListActivity implements LocationListener {
String songRes;
LocationManager whslocation;
Location whslocateme;
String Slongtitude;
String Slatitude;
String SongTitle;
TextView view01;
double lat;
double lng;
ListView lv1;
ArrayAdapter<String> aa;
private ArrayList<String> songData = new ArrayList<String>();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.lastfivelocation);
view01 =(TextView)findViewById(R.id.TextView01);
XMLParser(SongTitle , 0.0 ,0.0);
lv1 = getListView();
registerForContextMenu(lv1);
aa= new ArrayAdapter<String>
(this,android.R.layout.simple_list_item_1,songData);
aa.notifyDataSetChanged();
lv1.setAdapter(aa);
}
private void XMLParser(String songName,double lati,double lngo){
//Get the XML
URL url;
try{
//Get the city name and construct the full URL to get the XML file
String StringUrl="http://sit.rp.edu.sg/c345/getsonglocations.php?apikey=2f407662cc3ff7bdde78f34bf0fa4c1aa8b96e08&song="+songName;
url=new URL(StringUrl);
URLConnection connection;
connection=url.openConnection();
//Starts a HTTP connection
HttpURLConnection httpConnection = (HttpURLConnection)connection;
int responseCode = httpConnection.getResponseCode();
if (responseCode == HttpURLConnection.HTTP_OK){
InputStream in = httpConnection.getInputStream();
Log.d("response", ""+responseCode);
// A factory API that enables applications to obtain a parser that produces DOM object trees from XML documents
DocumentBuilderFactory dbf=
DocumentBuilderFactory.newInstance();
DocumentBuilder db=dbf.newDocumentBuilder();
//Parse the RSS feed
Document dom=db.parse(in);
Element docEle=dom.getDocumentElement();
//Get the current weather condition by the Tag Name
NodeList nl=docEle.getElementsByTagName("item");
Log.d("nosongs",nl.getLength()+" ");
//Retrieve the child elements in *current_conditions*
if (nl != null && nl.getLength() > 0) {
//If there are more than 1 *current_conditions*, it will go
//through each of the XML tree of *current_conditions* and
//retrieve the content from there. This will be more
//applicable for *weather_forecase*
for (int i = 0 ; i < nl.getLength(); i++) {
Element entry=(Element)nl.item(i);
//Retrieve the child elements by its various tag
//name - Complete the following to obtain all the
//necessary information
Element SsongName =(Element)entry.getElementsByTagName("song").item(0);
songRes = SsongName.getAttributeNode("data").getValue();
Element lng = (Element)entry.getElementsByTagName("lng").item(0);
Slongtitude = lng.getAttributeNode("data").getValue();
Element lat = (Element)entry.getElementsByTagName("lat").item(0);
Slatitude = lat.getAttributeNode("data").getValue();
songData.add(songRes);
songData.add(Slongtitude);
songData.add(Slatitude);
}
}
}
}catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (ParserConfigurationException e) {
e.printStackTrace();
} catch (SAXException e) {
e.printStackTrace();
}
finally {
}
whslocation =(LocationManager) getSystemService(LOCATION_SERVICE);
//get the current update to this class
whslocation.requestLocationUpdates(LocationManager.GPS_PROVIDER,5000 ,30,
lastfivelocation.this);
//locate current your position
whslocateme = whslocation.getLastKnownLocation("gps");
onLocationChanged(whslocateme);
}
@Override
public void onLocationChanged(Location location) {
// TODO Auto-generated method stub
if(location != null) {
lat = location.getLatitude()*1E6;
lng = location.getLongitude()*1E6;
}
}
@Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
}
@Override
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}
}
format
<rss version="2.0">
<channel>
<item>
<song data="Academia"/>
<artist data="Sia"/>
<album data="Some"/>
<lat data="1.38499"/>
<lng data="103.744092"/>
<playtime data="2010-06-27 12:01:27"/>
</item>
</channel>
</rss>
I tried to get the location but my listview shows me nothing instead
my codes doesn't have errors so I might be missing something
that can display the location of the song played.
Help is apperciated. thanks.
this is my following codes
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
import android.app.ListActivity;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.util.Log;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
public class lastfivelocation extends ListActivity implements LocationListener {
String songRes;
LocationManager whslocation;
Location whslocateme;
String Slongtitude;
String Slatitude;
String SongTitle;
TextView view01;
double lat;
double lng;
ListView lv1;
ArrayAdapter<String> aa;
private ArrayList<String> songData = new ArrayList<String>();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.lastfivelocation);
view01 =(TextView)findViewById(R.id.TextView01);
XMLParser(SongTitle , 0.0 ,0.0);
lv1 = getListView();
registerForContextMenu(lv1);
aa= new ArrayAdapter<String>
(this,android.R.layout.simple_list_item_1,songData);
aa.notifyDataSetChanged();
lv1.setAdapter(aa);
}
private void XMLParser(String songName,double lati,double lngo){
//Get the XML
URL url;
try{
//Get the city name and construct the full URL to get the XML file
String StringUrl="http://sit.rp.edu.sg/c345/getsonglocations.php?apikey=2f407662cc3ff7bdde78f34bf0fa4c1aa8b96e08&song="+songName;
url=new URL(StringUrl);
URLConnection connection;
connection=url.openConnection();
//Starts a HTTP connection
HttpURLConnection httpConnection = (HttpURLConnection)connection;
int responseCode = httpConnection.getResponseCode();
if (responseCode == HttpURLConnection.HTTP_OK){
InputStream in = httpConnection.getInputStream();
Log.d("response", ""+responseCode);
// A factory API that enables applications to obtain a parser that produces DOM object trees from XML documents
DocumentBuilderFactory dbf=
DocumentBuilderFactory.newInstance();
DocumentBuilder db=dbf.newDocumentBuilder();
//Parse the RSS feed
Document dom=db.parse(in);
Element docEle=dom.getDocumentElement();
//Get the current weather condition by the Tag Name
NodeList nl=docEle.getElementsByTagName("item");
Log.d("nosongs",nl.getLength()+" ");
//Retrieve the child elements in *current_conditions*
if (nl != null && nl.getLength() > 0) {
//If there are more than 1 *current_conditions*, it will go
//through each of the XML tree of *current_conditions* and
//retrieve the content from there. This will be more
//applicable for *weather_forecase*
for (int i = 0 ; i < nl.getLength(); i++) {
Element entry=(Element)nl.item(i);
//Retrieve the child elements by its various tag
//name - Complete the following to obtain all the
//necessary information
Element SsongName =(Element)entry.getElementsByTagName("song").item(0);
songRes = SsongName.getAttributeNode("data").getValue();
Element lng = (Element)entry.getElementsByTagName("lng").item(0);
Slongtitude = lng.getAttributeNode("data").getValue();
Element lat = (Element)entry.getElementsByTagName("lat").item(0);
Slatitude = lat.getAttributeNode("data").getValue();
songData.add(songRes);
songData.add(Slongtitude);
songData.add(Slatitude);
}
}
}
}catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (ParserConfigurationException e) {
e.printStackTrace();
} catch (SAXException e) {
e.printStackTrace();
}
finally {
}
whslocation =(LocationManager) getSystemService(LOCATION_SERVICE);
//get the current update to this class
whslocation.requestLocationUpdates(LocationManager.GPS_PROVIDER,5000 ,30,
lastfivelocation.this);
//locate current your position
whslocateme = whslocation.getLastKnownLocation("gps");
onLocationChanged(whslocateme);
}
@Override
public void onLocationChanged(Location location) {
// TODO Auto-generated method stub
if(location != null) {
lat = location.getLatitude()*1E6;
lng = location.getLongitude()*1E6;
}
}
@Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
}
@Override
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}
}