开发者

How to read and show Thai language from RSS in android 2.1?

I am using 2.1. I am reading a RSS like like one.

What I thinking is I have to change encoding? "e.g InputStream inputStream = new ByteArrayInputStream(xmlResponse.getBytes("UTF-8"));" Or I have to set TextView Font tp a Thai font?

<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 มิ.ย. นี้&nbsp;&nbsp;</p>]]></description>
<pubDate>Wed, 08 Jun 2011 13:11:46 +0700</pubDate>
</item>

I am reading this RSS with following code.

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 bufferedInputS开发者_开发百科tream = 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;
}

Code for parser

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;
}


I just use Thai supported font for TextView and its working great.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜