【发布时间】:2017-06-23 09:29:27
【问题描述】:
1.基于
我的代码基于此教程: https://www.youtube.com/watch?v=40mYDQkK44A https://github.com/FoamyGuy/StackSites
因此,如果您想了解正在发生的事情,只需查看那里的整个代码,我并没有对其进行太多更改。
2。我的 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 总是给我一个“?”而不是“Ü,Ä,..”。所以我尝试用 Google 提供给我的每一种方式来解决这个问题,但我无法解决这个问题。
我尝试更改缓冲区“byte data[] = new byte[1024];”到更大的东西,而不是 BufferedInputStream 我尝试了 InputStreamReader ecc。
我观察到的是:用记事本将我的 XML 保存在“ANSI”代码中确实可以正常工作,用“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"
/>
显示正确。
【问题讨论】:
-
显示组件的代码页呢?
-
感谢您的宝贵时间 - 显示发生在这两个文件中:github.com/FoamyGuy/StackSites/blob/master/src/com/… AND github.com/FoamyGuy/StackSites/blob/master/src/com/…
-
我指的是您的布局 XML,尤其是
TextViews 使用的字体... -
我用 XML 文件编辑了我的帖子,希望这有助于您理解。
标签: java android xml utf-8 xml-parsing