P
pyretta
Ambitioniertes Mitglied
- 1
Hallo,
ich möchte einen RSS-Feed von einer Wordpress-Seite in eine App einbinden.
Anforderungen:
An Punkt 1 der Anforderungen, kann ich bereits ein Häkchen setzen, bei Punkt 2 stoße ich schon etwas an meine Grenzen.
Für Punkt 1 habe ich ein wunderbares Tutorial gefunden, und den Source-Code als Grundlage verwenden können.
Tutorial gibt es hier:
Android Development: Android RSS reader with image
Source-Code hier:
http://www.thirdmindmedia.co.uk/source/android/SimpleAndroidRSSReader_0.2.zip
Ich möchte den Source-Code weiterverwenden, und für Punkt 2 der Anforderungen anpassen.
In dem Source-Code werden die RSS-Feed Elemente elegant in ein JSONObject überführt und so an eine ListView übergeben.
Siehe hier:
(im Source-Code "RSSReader.java")
Dann wird das JSONObject mittels eines ArrayAdapters der ListView hinzugefügt und den Layout-Elementen (TextView und ImageView) zugewiesen.
Siehe hier (im Source-Code "RSSListAdapter.java"):
Dann wird es zur Anzeige der zuvor erstellte Adapter der ListView zugewiesen (im Source-Code "RSSActivity.java"):
Überall wo "//Modifikation" dran steht, habe ich schon mal einige Vorbereitungen getroffen, die wie ich hoffe zur Umsetzung von Punkt 2 nötig sind.
Ich weiss nun nur nicht, wie ich auf die JSONObjects von einer anderen Activity zugreifen kann UND vor allen Dingen, wie ich zum geklickten Feed den passenden Text aus den JSONObjects fische.
Kann mir da jemand helfen?
Vielen Dank im Voraus.
ich möchte einen RSS-Feed von einer Wordpress-Seite in eine App einbinden.
Anforderungen:
- Die Listeneinträge sollen ein Teaser-Bild, Teaser-Text und das Veröffentlichungsdatum enthalten.
- Bei Klick auf eines der Listeneinträge soll der Text des vollständigen Artikels, genauso oder ähnlich formatiert, wie auf der Wordpress-Website, innerhalb der App (am besten als Activity?) angezeigt werden.
An Punkt 1 der Anforderungen, kann ich bereits ein Häkchen setzen, bei Punkt 2 stoße ich schon etwas an meine Grenzen.
Für Punkt 1 habe ich ein wunderbares Tutorial gefunden, und den Source-Code als Grundlage verwenden können.
Tutorial gibt es hier:
Android Development: Android RSS reader with image
Source-Code hier:
http://www.thirdmindmedia.co.uk/source/android/SimpleAndroidRSSReader_0.2.zip
Ich möchte den Source-Code weiterverwenden, und für Punkt 2 der Anforderungen anpassen.
In dem Source-Code werden die RSS-Feed Elemente elegant in ein JSONObject überführt und so an eine ListView übergeben.
Siehe hier:
(im Source-Code "RSSReader.java")
Code:
import java.util.ArrayList;
import java.util.List;
import org.json.JSONException;
import org.json.JSONObject;
import android.text.Html;
import android.util.Log;
import com.tmm.android.rssreader.util.Article;
import com.tmm.android.rssreader.util.RSSHandler;
/**
* @author rob
*
*/
public class RssReader {
private final static String BOLD_OPEN = "<B>";
private final static String BOLD_CLOSE = "</B>";
private final static String BREAK = "<BR>";
private final static String ITALIC_OPEN = "<I>";
private final static String ITALIC_CLOSE = "</I>";
private final static String SMALL_OPEN = "<SMALL>";
private final static String SMALL_CLOSE = "</SMALL>";
/**
* This method defines a feed URL and then calles our SAX Handler to read the article list
* from the stream
*
* @return List<JSONObject> - suitable for the List View activity
*/
public static List<JSONObject> getLatestRssFeed(){
String feed = "http://www.healthon.de/feed/";
RSSHandler rh = new RSSHandler();
List<Article> articles = rh.getLatestArticles(feed);
Log.e("RSS ERROR", "Number of articles " + articles.size());
return fillData(articles);
}
/**
* This method takes a list of Article objects and converts them in to the
* correct JSON format so the info can be processed by our list view
*
* @param articles - list<Article>
* @return List<JSONObject> - suitable for the List View activity
*/
private static List<JSONObject> fillData(List<Article> articles) {
List<JSONObject> items = new ArrayList<JSONObject>();
for (Article article : articles) {
JSONObject current = new JSONObject();
try {
buildJsonObject(article, current);
} catch (JSONException e) {
Log.e("RSS ERROR", "Error creating JSON Object from RSS feed");
}
items.add(current);
}
return items;
}
/**
* This method takes a single Article Object and converts it in to a single JSON object
* including some additional HTML formating so they can be displayed nicely
*
* @param article
* @param current
* @throws JSONException
*/
private static void buildJsonObject(Article article, JSONObject current) throws JSONException {
String title = article.getTitle();
String description = article.getDescription();
String content = article.getEncodedContent(); //Modifikation
//Original
String date = article.getPubDate();
String imgLink = article.getImgLink();
StringBuffer sb = new StringBuffer();
sb.append(BOLD_OPEN).append(title).append(BOLD_CLOSE);
sb.append(BREAK);
sb.append(description);
sb.append(BREAK);
sb.append(SMALL_OPEN).append(ITALIC_OPEN).append(date).append(ITALIC_CLOSE).append(SMALL_CLOSE);
//Original
current.put("text", Html.fromHtml(sb.toString()));
current.put("imageLink", imgLink);
//Modifikation
StringBuffer sb_title = new StringBuffer();
sb_title.append(BOLD_OPEN).append(title).append(BOLD_CLOSE);
StringBuffer sb_description = new StringBuffer();
sb_description.append(description);
StringBuffer sb_date = new StringBuffer();
sb_date.append(SMALL_OPEN).append(ITALIC_OPEN).append(date).append(ITALIC_CLOSE).append(SMALL_CLOSE);
StringBuffer sb_fullart = new StringBuffer();
sb_fullart.append(BOLD_OPEN).append(title).append(BOLD_CLOSE);
sb_fullart.append(BREAK);
sb_fullart.append(content);
//sb_fullart.append(BREAK);
//sb_fullart.append(SMALL_OPEN).append(ITALIC_OPEN).append(date).append(ITALIC_CLOSE).append(SMALL_CLOSE);
//Modifikation
current.put("content", Html.fromHtml(sb_fullart.toString()));
current.put("title", Html.fromHtml(sb_title.toString()));
current.put("date", Html.fromHtml(sb_date.toString()));
current.put("description", Html.fromHtml(sb_description.toString()));
}
}
Siehe hier (im Source-Code "RSSListAdapter.java"):
Code:
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.List;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.text.Spanned;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
public class RssListAdapter extends ArrayAdapter<JSONObject> {
public RssListAdapter(Activity activity, List<JSONObject> imageAndTexts) {
super(activity, 0, imageAndTexts);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
Activity activity = (Activity) getContext();
LayoutInflater inflater = activity.getLayoutInflater();
// Inflate the views from XML
View rowView = inflater.inflate(R.layout.image_text_layout, null);
JSONObject jsonImageText = getItem(position);
//////////////////////////////////////////////////////////////////////////////////////////////////////
//The next section we update at runtime the text - as provided by the JSON from our REST call
////////////////////////////////////////////////////////////////////////////////////////////////////
TextView textView = (TextView) rowView.findViewById(R.id.job_text);
ImageView imageView = (ImageView) rowView.findViewById(R.id.feed_image);
try {
if (jsonImageText.get("imageLink") != null){
System.out.println("XXXX Link found!");
String url = (String) jsonImageText.get("imageLink");
URL feedImage= new URL(url);
HttpURLConnection conn= (HttpURLConnection)feedImage.openConnection();
InputStream is = conn.getInputStream();
Bitmap img = BitmapFactory.decodeStream(is);
imageView.setImageBitmap(img);
}
// Spanned content = (Spanned)jsonImageText.get("content");
Spanned text = (Spanned)jsonImageText.get("text");
textView.setText(text);
} catch (MalformedURLException e) {
//handle exception here - in case of invalid URL being parsed
//from the RSS feed item
}
catch (IOException e) {
//handle exception here - maybe no access to web
}
catch (JSONException e) {
textView.setText("JSON Exception");
}
return rowView;
}
}
Code:
import java.util.ArrayList;
import java.util.List;
import org.json.JSONObject;
import com.tmm.android.rssreader.reader.RssReader;
import android.app.ListActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.TextView;
import android.widget.Toast;
public class RssActivity extends ListActivity{
private RssListAdapter adapter;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
List<JSONObject> jobs = new ArrayList<JSONObject>();
try {
jobs = RssReader.getLatestRssFeed();
} catch (Exception e) {
Log.e("RSS ERROR", "Error loading RSS Feed Stream >> " + e.getMessage() + " //" + e.toString());
}
adapter = new RssListAdapter(this,jobs);
setListAdapter(adapter);
//Modifikation
getListView().setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
//Test des Klicks ergibt, dass immer der Text des vorherigen
//Listenelements in den Toast genommen wird.
//Warum weiss ich nicht...
TextView rsstext = (TextView) findViewById(R.id.job_text);
String item = rsstext.getText().toString();
Toast.makeText(getBaseContext(), item, Toast.LENGTH_LONG).show();
}
});
}
}
Ich weiss nun nur nicht, wie ich auf die JSONObjects von einer anderen Activity zugreifen kann UND vor allen Dingen, wie ich zum geklickten Feed den passenden Text aus den JSONObjects fische.
Kann mir da jemand helfen?

Vielen Dank im Voraus.