gpt4 book ai didi

sax - 如何为 SAX 解析器设置线程和处理程序?

转载 作者:行者123 更新时间:2023-11-29 22:19:31 38 4
gpt4 key购买 nike

我一直在尝试为 SAX 解析器设置 UI 线程和处理程序。这是我没有实现 UI 线程和处理程序的解析器:

public class AndroidXMLReader extends ListActivity {

private XMLFeed myXMLFeed = null;


/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
try {
URL xmlUrl = new URL("http://feeds.bbci.co.uk/news/uk/rss.xml");
SAXParserFactory mySAXParserFactory = SAXParserFactory.newInstance();
SAXParser mySAXParser = mySAXParserFactory.newSAXParser();
XMLReader myXMLReader = mySAXParser.getXMLReader();
XMLHandler myXMLHandler = new XMLHandler();
myXMLReader.setContentHandler(myXMLHandler);
InputSource myInputSource = new InputSource(xmlUrl.openStream());
myXMLReader.parse(myInputSource);

myXMLFeed = myXMLHandler.getFeed();

}

catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (ParserConfigurationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (SAXException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

if (myXMLFeed!=null)
{
ArrayAdapter<XMLItem> adapter =
new ArrayAdapter<XMLItem>(this,
android.R.layout.simple_list_item_1,myXMLFeed.getList());
setListAdapter(adapter);
}
}
}

然后我尝试添加 UI 线程和处理程序。 编辑 1 - 这是根据 HellBoy 的回复修改后的代码:

   public class AndroidXMLReader extends ListActivity {

private static final int THREAD_FINISHED = 0;
private XMLFeed myXMLFeed = null;
private ProgressDialog progressDialog;
private Button refreshFeed;
Handler handler = new Handler();

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

initControls();
}

public void initControls(){


refreshFeed = (Button) findViewById(R.id.refresh);

refreshFeed.setOnClickListener(new Button.OnClickListener(){
@Override
public void onClick(View v){
progressDialog = ProgressDialog.show(AndroidXMLReader.this, "",
"Please wait for few seconds...", true);
processThread();
}
});
}
protected void processThread() {
Thread t = new Thread()
{
public void run(){
getNews();
//UI();
// Sends message to the handler so it updates the UI
handler.sendMessage(Message.obtain(mHandler, THREAD_FINISHED));

; }
};
t.start();
}

private Handler mHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);

switch (msg.what) {
case THREAD_FINISHED:

progressDialog.dismiss();
break;
};

};
};

private void getNews(){
try {
URL xmlUrl = new URL("http://feeds.bbci.co.uk/news/uk/rss.xml");
SAXParserFactory mySAXParserFactory = SAXParserFactory.newInstance();
SAXParser mySAXParser = mySAXParserFactory.newSAXParser();
XMLReader myXMLReader = mySAXParser.getXMLReader();
XMLHandler myXMLHandler = new XMLHandler();
myXMLReader.setContentHandler(myXMLHandler);
InputSource myInputSource = new InputSource(xmlUrl.openStream());
myXMLReader.parse(myInputSource);

myXMLFeed = myXMLHandler.getFeed();

}

catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (ParserConfigurationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (SAXException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

if (myXMLFeed!=null){
ArrayAdapter<XMLItem> adapter =
new ArrayAdapter<XMLItem>(this,
android.R.layout.simple_list_item_1,myXMLFeed.getList());
setListAdapter(adapter);
}

}
}

目前它卡在进度对话框中,我不明白为什么...任何帮助将不胜感激。

编辑 1 - 我现在收到的错误消息是:

Only the original thread that created a view hierarchy can touch its views.

    10-04 21:39:27.570: ERROR/AndroidRuntime(17454):     at com.android.testfeed3.AndroidXMLReader.getNews(AndroidXMLReader.java:121)
10-04 21:39:27.570: ERROR/AndroidRuntime(17454): at com.android.testfeed3.AndroidXMLReader.access$2(AndroidXMLReader.java:85)
10-04 21:39:27.570: ERROR/AndroidRuntime(17454): at com.android.testfeed3.AndroidXMLReader$3.run(AndroidXMLReader.java:60)
10-04 21:39:27.645: WARN/ActivityManager(2703): Force finishing activity com.android.testfeed3/.AndroidXMLReader
10-04 21:39:33.495: ERROR/WindowManager(17454): Activity com.android.testfeed3.AndroidXMLReader has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@40527a20 that was originally added here
10-04 21:39:33.495: ERROR/WindowManager(17454): android.view.WindowLeaked: Activity com.android.testfeed3.AndroidXMLReader has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@40527a20 that was originally added here
10-04 21:39:33.495: ERROR/WindowManager(17454): at com.android.testfeed3.AndroidXMLReader$2.onClick(AndroidXMLReader.java:50)

如有任何帮助,我们将不胜感激。

最佳答案

为了更改 UI,命令必须来自创建它的线程,即主(或 UI)线程。

快速浏览一下,这一行:

if (myXMLFeed!=null)
{
ArrayAdapter<XMLItem> adapter =
new ArrayAdapter<XMLItem>(this,
android.R.layout.simple_list_item_1,myXMLFeed.getList());
setListAdapter(adapter);
}

需要放在一个单独的处理程序中,并从您的新线程调用该处理程序。

关于sax - 如何为 SAX 解析器设置线程和处理程序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7630332/

38 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com