2017-06-23 53 views
0

1.基於如何在Android中正確保存和解析XML UTF-8(Ü,Ä,Ö)?

我的代碼是基於本教程在這裏: https://www.youtube.com/watch?v=40mYDQkK44A https://github.com/FoamyGuy/StackSites

所以,如果你想了解發生了什麼,只是看看整個代碼在那裏,我的天堂它沒有改變很多。

2. MY XML

<event> 
    <name>Reunion</name> 
    <date>Freitag 07.07.2017 </date> 
    <link>EVENT LINK</link> 
    <about>Über eine Woche ist es her, seit .... ÄÜÖ :-) </about> 
    <image>IMAGE LINK</image> 
</event> 

3. XML

URLConnection ucon = url.openConnection(); 
    /************************************************ 
    * Define InputStreams to read from the URLConnection. 
    ************************************************/ 
    InputStream is = ucon.getInputStream(); 
    BufferedInputStream bis = new BufferedInputStream(is); 
    /************************************************ 
    * Define OutputStreams to write to our file. 
    ************************************************/ 

    BufferedOutputStream bos = new BufferedOutputStream(fos); 
    byte data[] = new byte[1024]; 
    //long total = 0; 
    int count; 
    //loop and read the current chunk 

    while ((count = bis.read(data)) != -1) { 
     //keep track of size for progress. 
     //total += count; 

     //write this chunk 
     bos.write(data, 0, count); 
    } 
    //Have to call flush or the file can get corrupted. 
    bos.flush(); 
    bos.close(); 

4的下載讀取XML

 // Get our factory and PullParser 
     XmlPullParserFactory factory = XmlPullParserFactory.newInstance(); 
     XmlPullParser xpp = factory.newPullParser(); 

     // Open up InputStream and Reader of our file. 
     FileInputStream fis = ctx.openFileInput("Events.xml"); 
     BufferedReader reader = new BufferedReader(new InputStreamReader(fis,"UTF-8")); 


     // point the parser to our file. 
     xpp.setInput(reader); 

     // get initial eventType 
     int eventType = xpp.getEventType(); 

     // Loop through pull events until we reach END_DOCUMENT 
     while (eventType != XmlPullParser.END_DOCUMENT) { 
      // Get the current tag 
      String tagname = xpp.getName(); 

      // React to different event types appropriately 
      switch (eventType) { 
       case XmlPullParser.START_TAG: 
        if (tagname.equalsIgnoreCase(KEY_SITE)) { 
         // If we are starting a new <site> block we need 
         //a new StackSite object to represent it 
         curStackSite = new StackSite(); 

        } 
        break; 

       case XmlPullParser.TEXT: 
        //grab the current text so we can use it in END_TAG event 
        curText = xpp.getText(); 

        break; 

       case XmlPullParser.END_TAG: 
        if (tagname.equalsIgnoreCase(KEY_EVENT)) { 
         // if </event> then we are done with current Event 
         // add it to the list. 
         stackSites.add(curStackSite); 
        } else if (tagname.equalsIgnoreCase(KEY_NAME)) { 
         // if </name> use setName() on curSite 
         curStackSite.setName(curText); 
        } else if (tagname.equalsIgnoreCase(KEY_LINK)) { 
         // if </link> use setLink() on curSite 
         curStackSite.setLink(curText); 
        } else if (tagname.equalsIgnoreCase(KEY_ABOUT)) { 
         // if </about> use setAbout() on curSite 
         curStackSite.setAbout(curText); 
        } else if (tagname.equalsIgnoreCase(KEY_IMAGE_URL)) { 
         // if </image> use setImgUrl() on curSite 
         curStackSite.setImgUrl(curText); 
        } else if (tagname.equalsIgnoreCase(KEY_DATE)) { 
         // if </date> use setDate() on curSite 
         curStackSite.setDate(curText); 
        } 
        break; 

5.我的問題

基本上,這段代碼中的所有內容都像它應該那樣工作,除了在TextView中顯示像「Ö,Ä,Ü」這樣的字符。 TextView總是給我一個「?」而不是「Ü,Ä,..」。所以我試圖用谷歌給我提供的每一種方式解決這個問題,但我無法解決這個問題。我試着改變緩衝區「byte data [] = new byte [1024];」到更大的東西,而不是BufferedInputStream我試着InputStreamReader ecc。

我觀察到的結果是:用記事本在「ANSI」中保存我的XML代碼確實工作正常,用「UTF-8」保存它根本不起作用,我沒有從我的讀者那裏得到任何東西。

我也想展示一些表情符號,但這是問題的第二部分。顯示「Ü,Ä,Ö」首先會更重要。

顯示/佈局

這裏是顯示頁面的我的XML文件:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout 
android:padding="10dp" 
android:layout_height="wrap_content" 
android:layout_width="wrap_content" 
android:background="@drawable/schalt2" 
xmlns:android="http://schemas.android.com/apk/res/android" 

    > 

    <ImageView 
    android:layout_height="wrap_content" 
    android:layout_width="match_parent" 
    android:id="@+id/iconImg" 

    /> 

    <TextView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:id="@+id/event_title" 
    android:textColor="@android:color/white" 
    android:padding="4dp" 
    android:textStyle="bold" 
    android:textSize="22sp" 
    android:layout_below="@id/iconImg" 
    /> 

    <TextView 
    android:layout_height="wrap_content" 
    android:layout_width="wrap_content" 
    android:id="@+id/event_date" 
    android:textStyle="bold" 
    android:textSize="14sp" 
    android:padding="5dp" 
    android:textColor="@android:color/white" 
    android:layout_below="@id/event_title"/> 

    <TextView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:id="@+id/eventdescriptlong" 
    android:layout_below="@id/event_title" 
    android:padding="28dp" 
    android:textColor="@android:color/white" 
    /> 

所以,如果我想使用這樣一個TextView:

<TextView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:id="@+id/eventdescriptlong" 
    android:layout_below="@id/event_title" 
    android:text="ÜÖÄ" 
    android:textColor="@android:color/white" 
    /> 

它得到的正確顯示。

+0

怎麼樣的顯示組件的代碼頁? –

+0

感謝您的時間 - 顯示需要的地方在這兩個文件:https://github.com/FoamyGuy/StackSites/blob/master/src/com/makemyandroidapp/example/stacksites/StackSite.java和https:// github.com/FoamyGuy/StackSites/blob/master/src/com/makemyandroidapp/example/stacksites/SitesAdapter.java –

+0

我是指你的佈局XML,尤其是在'TextView's使用的字體... –

回答

0

解決它,問題如下:

保存在UTF-8字符集的文件,解析它局部地不會在XML的前面添加這個3個字節

https://i.stack.imgur.com/c7uqI.png[enter圖像此處描述] [1 ]

所以你絕對需要添加此行: fis.skip(3);

希望這可以幫助你們:)

相關問題