初学者!
所以我需要帮助将已解析的RSS提要保存到数据库中,以便以后离线查看。
我正在使用SAXParser解析RSS,然后在ListView中显示它。 我已经设法创建工作数据库,但我不知道如何将解析的字符串添加到数据库。我应该在解析之后还是在显示ListView之后立即使用它?
我的项目基于http://www.itcuties.com/android/how-to-write-android-rss-parser/ RSS解析器示例。
示例代码将不胜感激。
提前谢谢!
编辑: 我的主要活动AsyncTask:
private class GetRSSDataTask extends AsyncTask<String, Void, List<RssItem> > {
@Override
protected List<RssItem> doInBackground(String... urls) {
try {
// Create RSS reader
RssReader rssReader = new RssReader(urls[0]);
// Parse RSS, get items
return rssReader.getItems();
} catch (Exception e) {
Log.e("RssParser", e.getMessage());
}
return null;
}
@Override
protected void onPostExecute(List<RssItem> result) {
RssItem item = new RssItem();
db.open();
//do something by database
db.insertNewsInfo(item);
Log.i("String", item.toString());
db.close();
// Get a ListView from main view
ListView itcItems = (ListView) findViewById(R.id.listView);
// Create a list adapter
ArrayAdapter<RssItem> adapter = new ArrayAdapter<RssItem>(local,android.R.layout.simple_list_item_1, result);
// Set list adapter for the ListView
itcItems.setAdapter(adapter);
// Set list view item click listener
itcItems.setOnItemClickListener(new ListListener(result, local));
}
}
答案 0 :(得分:1)
对于Ex:您被解析为新闻网站
你可以上课NewsInformation.java
public class NewsInformation {
public String completeTextLink;
public String title;
public String writerName;
public String dateWriten;
public String source;
public String smallBody;
public String bigBody;
public String page;
}
通过使用上面的类将您的新闻插入数据库
DatabaseHelper.java
public class DatabaseHelper extends SQLiteOpenHelper {
private final String TAG = "DatabaseHelper";
private static final String DATABASE_NAME = "db_for_news";
public DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, 10);
//fdd
Log.i(TAG, "Object created.");
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE news ( page TEXT ," +
" completeTextLink TEXT ,title TEXT , writerName TEXT , dateWriten TEXT ," +
" source TEXT , smallBody TEXT , bigBody TEXT);");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
Log.w(DatabaseHelper.class.getName(), "Upgrading database from version "
+ oldVersion + " to " + newVersion + ", which will destroy all old data");
db.execSQL("Drop table if exists news" );
onCreate(db);
}
}
DatabaseHandler.java
中的
public class DatabaseHandler {
private DatabaseHelper dbHelper;
private SQLiteDatabase database;
//methods for all table
public DatabaseHandler(Context context) {
dbHelper = new DatabaseHelper(context);
}
public void open() throws SQLException {
database = dbHelper.getWritableDatabase();
}
public void close() {
dbHelper.close();
}
public void clearTable(String tableName) {
database.delete( tableName, null, null);
}
//news table method
public void insertNewsInfo(NewsInformation newsInfo) {
ContentValues cv = new ContentValues();
cv.put("bigBody" , newsInfo.bigBody );
cv.put("completeTextLink" , newsInfo.completeTextLink );
cv.put("dateWriten" , newsInfo.dateWriten );
cv.put("source" , newsInfo.source );
cv.put("smallBody" , newsInfo.smallBody );
cv.put("title" , newsInfo.title );
cv.put("writerName" , newsInfo.writerName );
cv.put("page" , newsInfo.page );
database.insert("news" , "writerName", cv);
}
public List<NewsInformation> getAllNewsForPage(String page) {
List<NewsInformation> NewsInfoList = new ArrayList<NewsInformation>();
Cursor cursor = database.rawQuery("select completeTextLink " +
" , title , writerName , dateWriten , source , smallBody , bigBody" +
" FROM news where page = ?", new String[]{page});
cursor.moveToFirst();
while (!cursor.isAfterLast()) {
NewsInformation newsInfo = new NewsInformation();
newsInfo.completeTextLink = cursor.getString(0);
newsInfo.title = cursor.getString(1);
newsInfo.writerName = cursor.getString(2);
newsInfo.dateWriten = cursor.getString(3);
newsInfo.source = cursor.getString(4);
newsInfo.smallBody = cursor.getString(5);
newsInfo.bigBody = cursor.getString(6);
newsInfo.page = page;
NewsInfoList.add(newsInfo);
cursor.moveToNext();
}
// Make sure to close the cursor
cursor.close();
return NewsInfoList;
}
public String getBigBody(String completeBodyLink)
{
Cursor cursor = database.rawQuery("select bigBody FROM news where completeTextLink = ?", new String[]{completeBodyLink});
cursor.moveToFirst();
return cursor.getString(0);
}
}
在您的活动中保存到DataBase
IN DATA >>private NewsDatabaseHandler db;
IN onCreat()>> db = new NewsDatabaseHandler(this);
//Where you want insert to database or read from it
db.open();
//do something by database
//for Ex:
NewsInformation item = new NewsInformation();
item.completeTextLink = "Your Link of news";
item.title = "title of news";
item.writerName = "writer of news";
item.dataWriten = "1999";
item.source = "something";
item.smallBody = "news body";
item.bigBody = "news text";
item.page = "1";
db.insertNewsInfo(item);
db.close();
答案 1 :(得分:0)
听起来你已经成功解析了RSS。接下来的步骤是:
1)将解析后的数据转换为java对象 2)坚持对象
对于第1步,如果您使用链接中的示例代码,则已经完成了此操作。 E.g:
public class RssItem {
// item title
private String title;
// item link
private String link;
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getLink() {
return link;
}
public void setLink(String link) {
this.link = link;
}
@Override
public String toString() {
return title;
}
}
此java对象表示要保留的数据。下一步是将ORM库集成到您的项目中。通常,最常见的是ORMlite。要使用它:
ormlite-android-4.47.jar
和ormlite-core-4.47.jar
在libs中以下是一个例子:
Dao<RssItem, String> rssDao = DaoManager.createDao(connectionSource, RssItem.class);
TableUtils.createTable(connectionSource, RssItem.class);
// create an instance of RssItem
RssItem item = new RssItem();
item.setTitle("News Item A");
// persist the item object to the database
rssDao.create(item);
这是ORM持久性的基础知识。一旦你坚持下去,你也可以使用DAO进行查询。