Как читать и показывать тайский язык из RSS в Android 2.1? - PullRequest
1 голос
/ 08 июня 2011

Я использую 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 มิ.ย. นี้&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;
}

1 Ответ

0 голосов
/ 28 июля 2011

Я просто использую тайский шрифт для TextView, и он отлично работает.

...