在资产中显示unicode文本?

时间:2012-10-03 14:46:32

标签: android

我正在构建一个使用* .txt文件形成资产的Android应用程序。但我的* .txt文件有一些unicode文本,我的手机无法显示它们。我能做什么?这是我的PlistHelper类:

public class PListHelper {

    public static String readQuizzesListFromAssets(Context context) {
        StringBuffer sb = new StringBuffer();

        BufferedReader br=null;
        try {
             br = new BufferedReader(new InputStreamReader(context.getAssets().open("quizzesList.plist"))); 
            String temp;
            while ((temp = br.readLine()) != null)
                sb.append(temp);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                br.close(); // stop reading
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
        Log.i("PListHelper", "input: "+sb.toString());

        return sb.toString();
    }


    public static String readConfigFromAssets(Context context) {
        StringBuffer sb = new StringBuffer();
        BufferedReader br=null;
        try {
             br = new BufferedReader(new InputStreamReader(context.getAssets().open("config.plist"))); 
            String temp;

            while ((temp = br.readLine()) != null)
                sb.append(temp);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                br.close(); // stop reading
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
        Log.i("PListHelper", "input: "+sb.toString());

        return sb.toString();
    }


    //Parse Response
    public static ArrayList<Quiz> parseQuizzesPlist(String input){
        ArrayList<Quiz> listOfQuizzes = null;
        ArrayList<ATriviaQuestion> listOfQuestions = null;

        //parsing of response                       
        try{            
            //XMLPullParser
            XmlPullParserFactory factory=XmlPullParserFactory.newInstance();
            factory.setNamespaceAware(true);
            XmlPullParser xpp = factory.newPullParser();

            xpp.setInput(new StringReader(input));
            int eventType = xpp.getEventType();
            String tagName = null, tagData = ""; 

            listOfQuizzes = new ArrayList<Quiz>();
            Quiz quiz = null;
            ATriviaQuestion aTQuestion = null;

            ArrayList<String> options = null;
            boolean areQuestions = false; 
            boolean isName = false, isImageName = false, isDescription = false;
            boolean isQuestion = false, areOptions = false, isAnswer = false;

            while(eventType != XmlPullParser.END_DOCUMENT){
                if(eventType == XmlPullParser.START_DOCUMENT){
                    Log.i("PlistParser", "xmlpullparser:start document");
                    tagName = "";
                }else if(eventType == XmlPullParser.END_DOCUMENT){
                    Log.i("PlistParser", "xmlpullparser:end document");
                }else if(eventType == XmlPullParser.START_TAG){
                    tagName = xpp.getName();

                    if(tagName.equals("quiz")){             
                        quiz = new Quiz();
                    }else if(tagName.equals("questions")){                      
                        listOfQuestions = new ArrayList<ATriviaQuestion>();
                        areQuestions = true;
                    }else if(tagName.equals("question"))
                        aTQuestion = new ATriviaQuestion();
                    else if(tagName.equals("array")){
                        options = new ArrayList<String>();
                    }

                }else if(eventType == XmlPullParser.END_TAG){
                    tagName=xpp.getName();

                    if(tagName.equals("quiz")){             
                        listOfQuizzes.add(quiz);
                    }else if(tagName.equals("questions")){                      
                        quiz.setQuestions(listOfQuestions);
                        areQuestions = false;
                    }else if(tagName.equals("question"))
                        listOfQuestions.add(aTQuestion);
                    else if(tagName.equals("array")){
                        if(areOptions){
                            areOptions = false;
                            aTQuestion.SetOptions(options);
                        }
                    }

                    tagName = "";tagData = "";                  
                }else if(eventType == XmlPullParser.TEXT){
                    tagData = xpp.getText();

                    //parsing questions of a quiz
                    if(areQuestions){
                        if(tagName.equals("key")){
                            if(tagData.equals("Question")){
                                isQuestion = true;                      
                            }else if(tagData.equals("Options")){
                                areOptions = true;
                            }else if(tagData.equals("Answer")){
                                isAnswer = true;                        
                            }
                        }else if(tagName.equals("string")){
                            if(isQuestion)
                                aTQuestion.SetQuestion(tagData);
                            else if(areOptions)
                                options.add(tagData);                           

                            isQuestion = false;
                        }else if(tagName.equals("integer")){
                            if(isAnswer)
                                aTQuestion.SetAnswer(Integer.valueOf(tagData));

                            isAnswer = false;
                        }
                    }

                    //parsing a quiz item
                    if(tagName.equals("key")){
                        if(tagData.equals("Name")){
                            isName = true;                      
                        }else if(tagData.equals("Description")){
                            isDescription = true;
                        }else if(tagData.equals("ImageName")){
                            isImageName = true;                     
                        }
                    }else if(tagName.equals("string")){
                        if(isName)
                            quiz.setName(tagData);
                        else if(isDescription)
                            quiz.setDescription(tagData);
                        else if(isImageName)
                            quiz.setImageName(tagData);

                        isName = false; isDescription = false; isImageName = false;
                    }

                }

                eventType = xpp.nextToken();
            }   //eventType while ends          

        }catch(Exception ex){
            Log.i("PListHelper", "Parsing exception with message->"+ex.getMessage());
        }   

        Log.i("PListHelper", "quizzes size: " + listOfQuizzes.size());

        return listOfQuizzes;

    }

    //Parse Response
    public static Config parseConfigFile(String input){
        Config config = null;

        //parsing of response                       
        try{            
            //XMLPullParser
            XmlPullParserFactory factory=XmlPullParserFactory.newInstance();
            factory.setNamespaceAware(true);
            XmlPullParser xpp = factory.newPullParser();

            xpp.setInput(new StringReader(input));
            int eventType = xpp.getEventType();
            String tagName = null, tagData = ""; 

            config = new Config();
            boolean isTimeToAnswer = false, isPointsPerRemainingSecond = false, isPointsPerCorrectAnswer = false, isPointsPerWrongAnswer = false;

            while(eventType != XmlPullParser.END_DOCUMENT){
                if(eventType == XmlPullParser.START_DOCUMENT){
                    Log.i("PlistParser", "xmlpullparser:start document");
                    tagName = "";
                }else if(eventType == XmlPullParser.END_DOCUMENT){
                    Log.i("PlistParser", "xmlpullparser:end document");
                }else if(eventType == XmlPullParser.START_TAG){
                    tagName = xpp.getName();

                    if(tagName.equals("dict"))              
                        ;

                }else if(eventType == XmlPullParser.END_TAG){
                    tagName=xpp.getName();

                    if(tagName.equals("dict"))              
                        ;

                    tagName = "";tagData = "";                  
                }else if(eventType == XmlPullParser.TEXT){
                    tagData = xpp.getText();

                    //parsing config items
                    if(tagName.equals("key")){
                        if(tagData.equals("timeToAnswer"))              
                            isTimeToAnswer = true;
                        else if(tagData.equals("pointsPerRemainingSecond"))                     
                            isPointsPerRemainingSecond = true;
                        else if(tagData.equals("pointsPerCorrectAnswer"))
                            isPointsPerCorrectAnswer = true;
                        else if(tagData.equals("pointsPerWrongAnswer"))
                            isPointsPerWrongAnswer = true;

                    }else if(tagName.equals("integer")){
                        if(isTimeToAnswer)
                            config.SetTimeToAnswer(Integer.valueOf(tagData));
                        else if(isPointsPerRemainingSecond)
                            config.SetPointsPerRemainingSecond(Integer.valueOf(tagData));
                        else if(isPointsPerCorrectAnswer)
                            config.SetPointsPerCorrectAnswer(Integer.valueOf(tagData));
                        else if(isPointsPerWrongAnswer)
                            config.SetPointsPerWrongAnswer(Integer.valueOf(tagData));

                        isTimeToAnswer = false; isPointsPerRemainingSecond = false; isPointsPerCorrectAnswer = false; isPointsPerWrongAnswer = false;
                    }

                }

                eventType = xpp.nextToken();
            }   //eventType while ends          

        }catch(Exception ex){
            Log.i("PListHelper", "Parsing exception with message->"+ex.getMessage());
        }   

        return config;

    }

1 个答案:

答案 0 :(得分:1)

使用它:

br = new BufferedReader(new InputStreamReader(context.getAssets().open("quizzesList.plist"),"UTF-8"));