我正在尝试制作一个显示来自网站的新闻提要的应用程序,因此我获取输入流并使用 SAX 在文档中解析它,但它返回 SAX 异常,它无法确定此 Stream 的编码类型。在此之前,我尝试将网站的流手动放入 XML 文件中并读取文件,它可以工作,但是当直接从 Internet 流式传输时,它会抛出该异常,这是我的代码:
public final class MyScreen extends MainScreen {
protected static RichTextField RTF = new RichTextField("Plz Wait . . . ",
Field.FIELD_BOTTOM);
public MyScreen() {
// Set the displayed title of the screen
super(Manager.NO_VERTICAL_SCROLL);
setTitle("Yalla Kora");
Runnable R = new Runnable();
R.start();
add(RTF);
}
private class Runnable extends Thread {
public Runnable() {
// TODO Auto-generated constructor stub
ConnectionFactory factory = new ConnectionFactory();
ConnectionDescriptor descriptor = factory
.getConnection("http://www.yallakora.com/arabic/rss.aspx?id=0");
HttpConnection httpConnection;
httpConnection = (HttpConnection) descriptor.getConnection();// Connector.open("http://www.yallakora.com/pictures/main//2011/11/El-Masry-807-11-2011-21-56-7.jpg");
Manager mainManager = getMainManager();
RichList RL = new RichList(mainManager, true, 2, 1);
InputStream input;
try {
input = httpConnection.openInputStream();
Document document;
DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory
.newInstance();
DocumentBuilder docBuilder;
try {
docBuilder = docBuilderFactory.newDocumentBuilder();
docBuilder.isValidating();
try {
document = docBuilder.parse(input);
document.getDocumentElement().normalize();
NodeList item = document.getElementsByTagName("item");
int k = item.getLength();
for (int i = 0; i < k; i++) {
Node value = item.item(i);
NodeList Data = value.getChildNodes();
Node title = Data.item(0);
Node link = Data.item(1);
Node date = Data.item(2);
Node discription = Data.item(5);
Node Discription = discription.getFirstChild();
String s = Discription.getNodeValue();
int mm = s.indexOf("'><BR>");
int max = s.length();
String imagelink = s.substring(0, mm);
String Khabar = s.substring(mm + 6, max);
String Date = date.getFirstChild().getNodeValue();
String Title = title.getFirstChild().getNodeValue();
String Link = link.getFirstChild().getNodeValue();
ConnectionFactory factory1 = new ConnectionFactory();
ConnectionDescriptor descriptor1 = factory1
.getConnection(imagelink);
HttpConnection httpConnection1;
httpConnection1 = (HttpConnection) descriptor1
.getConnection();
InputStream input1;
input1 = httpConnection1.openInputStream();
byte[] bytes = IOUtilities.streamToBytes(input1);
Bitmap bitmap = Bitmap.createBitmapFromBytes(bytes,
0, -1, 1);
;
RL.add(new Object[] { bitmap, Title, Khabar, Date });
add(new RichTextField(link.getNodeValue(),
Field.NON_FOCUSABLE));
}
RTF.setText("");
} catch (SAXException e) {
// TODO Auto-generated catch block
RTF.setText("SAXException " + e.toString());
e.printStackTrace();
}
} catch (ParserConfigurationException e) {
// TODO Auto-generated catch block
RTF.setText("ParserConfigurationException " + e.toString());
e.printStackTrace();
}
} catch (IOException e) {
RTF.setText("IOException " + e.toString());
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}}
有任何想法吗 ??