1

我正在使用 2.1。我正在阅读类似的 RSS。

我认为我必须更改编码?"例如 InputStream inputStream = new ByteArrayInputStream(xmlResponse.getBytes("UTF-8"));" 或者我必须设置 TextView Font tp 泰语字体?

<item>
  <title><![CDATA[Largo Winch 2 บู๊ระห่ำเดือดกว่าเดิม]]></title>
  <author>Siritorn</author>
  <link>http://news.voicetv.co.th/entertainment/11866.html</link>
  <description><![CDATA[<p>
ภาพยนตร์แอ๊คชั่น Largo Winch 2 ยอดคนอันตรายล่าข้ามโลก ที่นำแสดงโดย โตแมร์ ซิสเลย์ และ ชารอน สโตน พร้อมเข้าฉายระเบิดความมันให้คอหนังบ้านเราได้ชมกันแล้ววันที่ 9 มิ.ย. นี้&amp;nbsp;&nbsp;</p>]]></description>
<pubDate>Wed, 08 Jun 2011 13:11:46 +0700</pubDate>
</item>

我正在使用以下代码阅读此 RSS。

DataFetcher public ArrayList fetchRSSItems(String URL){

    ArrayList<RSSItem> items = null;

    try {
        HttpGet http = new HttpGet(URL);

        String response = this.getSendRequest(http);
        if(response.length() > 0){
            DataParser parser = new DataParser();
            items  = parser.parseDataStories(response);
        }

    } catch (Exception e) {
        Log.e("Exception", "Message = "+e.toString());
        e.printStackTrace();
    }

    return items;
}

private String getSendRequest(HttpGet http) {

    try {
        DefaultHttpClient client = new DefaultHttpClient();

        HttpResponse response ;
        String stringResponse = "";

        response = client.execute(http);

        InputStream inputStream = response.getEntity().getContent();
        BufferedInputStream bufferedInputStream = new BufferedInputStream(inputStream, 2048);
        ByteArrayBuffer byteArrayBuffer = new ByteArrayBuffer(2048);

        int currentBuf = 0;
        while((currentBuf = bufferedInputStream.read()) != -1){
            byteArrayBuffer.append((byte) currentBuf);
        }

        stringResponse = new String(byteArrayBuffer.toByteArray());
        return stringResponse;
    } catch (Exception e) {
        Log.e("Exception", "Message = "+e.toString());
        e.printStackTrace();
    }
    return null;
}

解析器代码

public ArrayList<RSSItem> parseDataStories(String xmlResponse){
    ArrayList<RSSItem> itemList = new ArrayList<RSSItem>();
    try{
        InputStream inputStream = new ByteArrayInputStream(xmlResponse.getBytes("UTF-8"));

        DocumentBuilderFactory fectory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = fectory.newDocumentBuilder();
        Document document = builder.parse(inputStream);

        NodeList nodeList;
        Node childNode;

        nodeList = document.getElementsByTagName("item");

        for(int i = 0; i<nodeList.getLength() ; i++){
            childNode = nodeList.item(i);
            RSSItem item = this.parseStoryItem((Element) childNode);
            if(item != null){
                System.out.println(item.toString());
                if(item.getImagePath() != null){
                    item.setImage(AsyncImageLoader.loadImageFromUrl(item.getImagePath()));
                }
                itemList.add(item);
            }
        }

    }catch (Exception e) {
        Log.e("Exception","Message = "+e.toString());
    }catch (Error e) {
        Log.e("Error","Message = "+e.toString());
    }

    return itemList;
}

private RSSItem parseStoryItem(Element theElement) {
    RSSItem currentStory = new RSSItem();
    Node childNode;
    NodeList allChildern;

    try {
        String title = "";
        String description = "";

        childNode = theElement.getElementsByTagName("link").item(0);
        String fullStoryLink = childNode.getFirstChild().getNodeValue();
        currentStory.setFullPath(fullStoryLink);

        allChildern = theElement.getElementsByTagName("title").item(0).getChildNodes();
        for (int index = 0; index < allChildern.getLength(); index++) {
            title += allChildern.item(index).getNodeValue();
        }
        currentStory.setTitle(title);

        // Read the summary of Story
        if ((theElement.getElementsByTagName("description")).getLength() > 0) {

            allChildern = theElement.getElementsByTagName("description").item(0).getChildNodes();

            for (int index = 0; index < allChildern.getLength(); index++) {
                description += allChildern.item(index).getNodeValue();
            }
            currentStory.setDescription(description);
        }

        // Get pub date if any
        if ((theElement.getElementsByTagName("pubDate")).getLength() > 0) {

            childNode = theElement.getElementsByTagName("pubDate").item(0);
            currentStory.setDate(stringFromDateString(childNode.getFirstChild().getNodeValue()));
        }

    } catch (Exception e) {
        Log.e("Exception", "parseStoryItem Message = " + e.toString());
    } catch (Error e) {
        Log.e("Error", "parseStoryItem Message = " + e.toString());
    }

    return currentStory;
}

public static String stringFromDateString(String string) {

    String datePart = string.substring(0, 25);

    return datePart;
}
4

1 回答 1

0

我只是为 TextView 使用泰语支持的字体,而且效果很好。

于 2011-07-28T05:58:55.843 回答