Skip to content

Instantly share code, notes, and snippets.

@CreatorB
Created November 26, 2014 06:29
Show Gist options
  • Save CreatorB/8603d1fc193ba0761c86 to your computer and use it in GitHub Desktop.
Save CreatorB/8603d1fc193ba0761c86 to your computer and use it in GitHub Desktop.
Snippet Code of How to Create RSS Feed Android app on http://creatorb-lab.blogspot.com/2014/11/cara-membuat-aplikasi-android-rss-feed.html
package id.creatorb.muslimnews;
import id.creatorb.androidrssfeed.R;
import id.creatorb.muslimnews.adapter.DaftarPostAdapter;
import id.creatorb.muslimnews.post.PostData;
import id.creatorb.refresh.Refresher;
import id.creatorb.refresh.RefresherListView;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Locale;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import org.xmlpull.v1.XmlPullParserFactory;
import android.app.Activity;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
public class MainActivity extends Activity implements Refresher {
private enum RSSXMLTag {
TITLE, DATE, LINK, CONTENT, GUID, IGNORETAG;
}
private ArrayList<PostData> listData;
private String urlString = "http://feeds.feedburner.com/muslimorid";
private RefresherListView postListView;
private DaftarPostAdapter postAdapter;
private int pagnation = 1; //start dari 1
private boolean isRefreshLoading = true;
private boolean isLoading = false;
private ArrayList<String> guidList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.daftar_artikel);
guidList = new ArrayList<String>();
listData = new ArrayList<PostData>();
postListView = (RefresherListView) this.findViewById(R.id.postListView);
postAdapter = new DaftarPostAdapter(this, R.layout.list_item_artikel, listData);
postListView.setAdapter(postAdapter);
postListView.setOnRefresh(this);
postListView.onRefreshStart();
postListView.setOnItemClickListener(onItemClickListener);
}
private OnItemClickListener onItemClickListener = new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
PostData data = listData.get(arg2 - 1);
Bundle postInfo = new Bundle();
postInfo.putString("content", data.postContent);
Intent postviewIntent = new Intent(MainActivity.this, LihatPost.class);
postviewIntent.putExtras(postInfo);
startActivity(postviewIntent);
}
};
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
private class RssDataController extends
AsyncTask<String, Integer, ArrayList<PostData>> {
private RSSXMLTag currentTag;
@Override
protected ArrayList<PostData> doInBackground(String... params) {
// TODO Auto-generated method stub
String urlStr = params[0];
InputStream is = null;
ArrayList<PostData> postDataList = new ArrayList<PostData>();
try {
URL url = new URL(urlStr);
HttpURLConnection connection = (HttpURLConnection) url
.openConnection();
connection.setReadTimeout(10 * 1000);
connection.setConnectTimeout(10 * 1000);
connection.setRequestMethod("GET");
connection.setDoInput(true);
connection.connect();
int response = connection.getResponseCode();
Log.d("debug", "The response is: " + response);
is = connection.getInputStream();
// parse xml
XmlPullParserFactory factory = XmlPullParserFactory
.newInstance();
factory.setNamespaceAware(true);
XmlPullParser xpp = factory.newPullParser();
xpp.setInput(is, null);
int eventType = xpp.getEventType();
PostData pdData = null;
SimpleDateFormat dateFormat = new SimpleDateFormat(
"EEE, DD MMM yyyy HH:mm:ss", Locale.US);
while (eventType != XmlPullParser.END_DOCUMENT) {
if (eventType == XmlPullParser.START_DOCUMENT) {
} else if (eventType == XmlPullParser.START_TAG) {
if (xpp.getName().equals("item")) {
pdData = new PostData();
currentTag = RSSXMLTag.IGNORETAG;
} else if (xpp.getName().equals("title")) {
currentTag = RSSXMLTag.TITLE;
} else if (xpp.getName().equals("link")) {
currentTag = RSSXMLTag.LINK;
} else if (xpp.getName().equals("pubDate")) {
currentTag = RSSXMLTag.DATE;
} else if (xpp.getName().equals("encoded")) {
currentTag = RSSXMLTag.CONTENT;
} else if (xpp.getName().equals("guid")) {
currentTag = RSSXMLTag.GUID;
}
} else if (eventType == XmlPullParser.END_TAG) {
if (xpp.getName().equals("item")) {
// format data disini
// dan lainnya pada adapter
Date postDate = dateFormat.parse(pdData.postDate);
pdData.postDate = dateFormat.format(postDate);
postDataList.add(pdData);
} else {
currentTag = RSSXMLTag.IGNORETAG;
}
} else if (eventType == XmlPullParser.TEXT) {
String content = xpp.getText();
content = content.trim();
if (pdData != null) {
switch (currentTag) {
case TITLE:
if (content.length() != 0) {
if (pdData.postTitle != null) {
pdData.postTitle += content;
} else {
pdData.postTitle = content;
}
}
break;
case LINK:
if (content.length() != 0) {
if (pdData.postLink != null) {
pdData.postLink += content;
} else {
pdData.postLink = content;
}
}
break;
case DATE:
if (content.length() != 0) {
if (pdData.postDate != null) {
pdData.postDate += content;
} else {
pdData.postDate = content;
}
}
break;
case CONTENT:
if (content.length() != 0) {
if (pdData.postContent != null) {
pdData.postContent += content;
} else {
pdData.postContent = content;
}
}
break;
case GUID:
if (content.length() != 0) {
if (pdData.postGuid != null) {
pdData.postGuid += content;
} else {
pdData.postGuid = content;
}
}
break;
default:
break;
}
}
}
eventType = xpp.next();
}
Log.v("tst", String.valueOf((postDataList.size())));
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (XmlPullParserException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return postDataList;
}
@Override
protected void onPostExecute(ArrayList<PostData> result) {
// TODO Auto-generated method stub
boolean isupdated = false;
for (int i = 0; i < result.size(); i++) {
//cek apakah post telah ada pada list
if (guidList.contains(result.get(i).postGuid)) {
continue;
} else {
isupdated = true;
guidList.add(result.get(i).postGuid);
}
if (isRefreshLoading) {
listData.add(i, result.get(i));
} else {
listData.add(result.get(i));
}
}
if (isupdated) {
postAdapter.notifyDataSetChanged();
}
isLoading = false;
if (isRefreshLoading) {
postListView.onRefreshComplete();
} else {
postListView.onLoadingMoreComplete();
}
super.onPostExecute(result);
}
}
@Override
public void startFresh() {
// TODO Auto-generated method stub
if (!isLoading) {
isRefreshLoading = true;
isLoading = true;
new RssDataController().execute(urlString + 1);
} else {
postListView.onRefreshComplete();
}
}
@Override
public void startLoadMore() {
// TODO Auto-generated method stub
if (!isLoading) {
isRefreshLoading = false;
isLoading = true;
new RssDataController().execute(urlString + (++pagnation));
} else {
postListView.onLoadingMoreComplete();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment