如何在android 2.1中从RSS读取并显示泰语?

发布于 2024-11-14 12:56:01 字数 5228 浏览 4 评论 0原文

我用的是2.1。我正在阅读 RSS 之类的内容。

我的想法是我必须更改编码? “例如InputStream inputStream = new ByteArrayInputStream(xmlResponse.getBytes(“UTF-8”));”或者我必须将 TextView 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>

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

数据获取器 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;
}

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 มิ.ย. นี้  </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 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;
}

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

再见回来 2024-11-21 12:56:01

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文