getting the value from an attribute of an element in an rss xml feed

by LuKeNuKuM » Fri, 04 Sep 2009 01:53:03 GMT


Sponsored Links
 i folks,

i've been struggling to find a way to read in a url value from within
a tag in an xml feed. im so close but i just can't seem to crack it.

i started out using this http://www.devx.com/wireless/Article/39810/1954
as a guide which works great but customising it to read in an
attribute of an element is causing me problems.

the feed is here:
http://newsrss.bbc.co.uk/rss/newsonline_uk_edition/front_page/rss.xml

within the xml's 'item' element is <media:thumbnail width="X"
height="X" url="X"/> it's the url attribute im after. i can get the
title back fine but the structure of the media:thumbnail tag means im
missing something!

what's interesting(!?) is the fact that when i remove the loop and
only go for the first item, i get the image back! the line with strImg
= imgElement.getAttribute("url") just won't work within the loop...
any ideas are greatly appreciated.

this is very rough n ready, im learning :-)

========================

package apps.httpDownload;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
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.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;

import android.widget.ImageView;
import android.widget.TextView;

public class httpDownload extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

ArrayList<String> dataReturned = DownloadRSS("http://
newsrss.bbc.co.uk/rss/newsonline_uk_edition/front_page/rss.xml");

TextView txt = (TextView) findViewById(R.id.text);
for (int i = 0; i < dataReturned.size(); i++) {
if (i < 10)
txt.setText(txt.getText() + "\n" + dataReturned.get(i) +
"\n");

Bitmap bitmap =
DownloadImage(dataReturned.get(1));
ImageView img = (ImageView) findViewById(R.id.img);
img.setImageBitmap(bitmap);

}


}

private InputStream OpenHttpConnection(String urlString)
throws IOException
{
InputStream in = null;
int response = -1;

URL url = new URL(urlString);
URLConnection conn = url.openConnection();

if (!(conn instanceof HttpURLConnection))
throw new IOException("Not an HTTP connection");

try{
HttpURLConnection httpConn = (HttpURLConnection) conn;
httpConn.setAllowUserInteraction(false);
httpConn.setInstanceFollowRedirects(true);
httpConn.setRequestMethod("GET");
httpConn.connect();

response = httpConn.getResponseCode();
if (response == HttpURLConnection.HTTP_OK) {
in = httpConn.getInputStream();
}
}
catch (Exception ex)



Other Threads

1. How to launch the street view from map view

New to android, please forgive if this question sounds naive.

The latest MapView can check whether the map is in street view mode
and allow users to toggle it.

However, the set street view api only display the blue layout on top
of the map. Is there any way that I can launch the street view like in
Google Map.

Also, does this version support route and direction?

Please kindly advice. Thanks

--~--~---------~--~----~------------~-------~--~----~

2. listActivity and checkBox

Hi,

Android supports to bind and populate data to a list view in
listActivity through some adapters.

You can define the list view in a xml file. Take notePad as example:

//-----------
// noteslist_item.xml
-----------------------------------------------------------------------------------------
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/text1"
    android:layout_width="fill_parent"
    android:layout_height="?android:attr/listPreferredItemHeight"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:gravity="center_vertical"
    android:paddingLeft="5dip"
    android:singleLine="true"
/>
--------------------------------------------------------------------------------------------

Then you can bind the data in the java code by an adapter: for example

--------------------------------------------------------------------------------------------

        Cursor cursor = managedQuery(getIntent().getData(),
PROJECTION, null, null,
                Notes.DEFAULT_SORT_ORDER);

        // Used to map notes entries from the database to views
        SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,
R.layout.noteslist_item, cursor,
                new String[] { Notes.TITLE }, new int[]
{ android.R.id.text1 });
        setListAdapter(adapter);
---------------------------------------------------------------------------------------------

It works fine. Now my question is how to differentiate those populated
TextView? Do they have different IDs?

If the list item is a complex view with checkBox or radio button, how
can we check their status/state respectively?

Thanks.

--elephantbug

--~--~---------~--~----~------------~-------~--~----~

3. About the use of the Wi-Fi function

4. Images and Layout Questions

5. how to get access to private API s

6. EventHandler ?

7. *** TCP connect() time out ***