0
我一直在试图创建一个简单的应用程序,它将整个频道解析为一个字符串并打印出来,还可以将解析后的数据保存到sqlite数据库中。我一直在关注tutorial1和tutorial2 我也有RSS阅读器的3-4其他实现一起阅读IBM教程,但不能弄清楚什么是错Android RSS阅读器问题
Item.java
public class Item extends Object {
private String Data= null;
Item() {
}
Item(String data) {
this.Data = data;
}
public void setData(String data) {
this.Data = data;
}
public String getData() {
return this.Data;
}
}
RSSHandler的.java
public class RSSHandler extends DefaultHandler {
public static final String ITEMS_TABLE="items";
private final String MY_DEBUG_TAG = "handler";
boolean inChannel = false;
public Item item;
LearDB leardb = null;
RSSHandler(Context context)
{
leardb = new LearDB(context);
item =new Item();
}
@Override
public void startDocument() throws SAXException {
this.item = new Item();
}
@Override
public void endDocument() throws SAXException{
}
@Override
public void startElement(String Uri, String localName, String qName,
Attributes atts) throws SAXException {
if (localName.equals("channel")) {
this.inChannel = true;
}
else {
return;
}
}
@Override
public void endElement(String Uri, String localName, String qName)
throws SAXException {
if (localName.equals("channel")) {
this.inChannel = false;
}
else {
return;
}
}
@Override
public void characters(char ch[], int start, int length) {
String temp = new String(ch, start, length);
try {
if (this.inChannel) {
this.item.setData(temp);
}
} catch (Exception e) {
Log.e(MY_DEBUG_TAG, "Parsing Error", e);
}
}
public String getData() {
return this.item.getData();
}
public Item getItem() {
return this.item;
}
public void insertItem(String temp) {
SQLiteDatabase db = leardb.getWritableDatabase();
ContentValues value=new ContentValues();
value.put("data", temp);
db.insert(ITEMS_TABLE, null, value);
}
}
LearDB.java
public class LearDB extends SQLiteOpenHelper {
private static final String DATABASE_NAME="lear.db";
public static final String ITEMS_TABLE="items";
public LearDB(Context context) {
super(context, DATABASE_NAME, null, 1);
}
@Override
public void onCreate(SQLiteDatabase db) {
String sql = ("CREATE TABLE items (_id INTEGER PRIMARY KEY AUTOINCREMENT, data TEXT);");
db.execSQL(sql);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
if (oldVersion == 1 && newVersion == 2) {
String sql = "drop table items;";
db.execSQL(sql);
sql = ("CREATE TABLE items (_id INTEGER PRIMARY KEY AUTOINCREMENT, data TEXT);");
db.execSQL(sql);
}
}
}
主要活动
public class test3 extends Activity {
private final String MY_DEBUG_TAG = "test2";
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
TextView text = new TextView(this);
try {
URL url = new URL("http://achdre.freehostia.com/example.xml");// URL of XML document to be parsed
InputSource source = new InputSource(url.openStream());
SAXParserFactory LearParseFactory = SAXParserFactory.newInstance();
SAXParser LearParser = LearParseFactory.newSAXParser(); //SAX Parser object
XMLReader LearReader = LearParser.getXMLReader(); // Object that scans the XML document
RSSHandler LearHandler = new RSSHandler(this); //Handler object
LearReader.setContentHandler(LearHandler);
LearReader.parse(source); //Parses the XML document
String temp=LearHandler.getData();
text.setText("12345");
LearHandler.insertItem("android"); //inserting test string to DB
}
catch (Exception e) {
text.setText("Error: " + e.getMessage());
Log.e(MY_DEBUG_TAG, "Parsing Error", e);
}
this.setContentView(text);
}}
和清单文件
<uses-sdk android:minSdkVersion="8" />
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
<application android:icon="..." android:label="..."android:debuggable="true">
<activity android:name=".test3"android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
android:label="@string/app_name"android:debuggable="true">
编辑:我设法使其运行。错误都是在我尝试解析的xml和我的处理程序类回调事件上。无需更多的援助
这是相当多的处理。你可以通过添加一个虚拟的“Hello World!”来验证你的活动是否正在加载?文本域?此外,您可以尝试在try块中尽早设置断点(在Eclipse中双击左边距),然后在Debug中运行应用程序。 – 2011-03-01 01:31:33
我认为它的加载正常,因为在我的主要活动中创建的'LearHandler'对象的SQLite数据库已正确创建,并且主要的命令实际上是在数据库中插入数据,因为我可以看到当我检索并打开 至于断点和调试运行我;我会在早上做,并给你答案 感谢您的帮助 – pmantz 2011-03-01 03:32:25
我在调试中运行应用程序。我可以确定发生了什么事情,因为我可以使用'text.setText(“1234”);' 而不是当我尝试 'String temp = LearHandler.getData(); 'text.setText(“12345”);'这样我的处理程序类 有什么问题,除此之外,我真的不知道用调试运行找到什么 – pmantz 2011-03-01 12:12:54