在 XMLPullParser 中始终获取 0,而 Web 服务包含的值为 float

发布于 2024-11-06 00:22:01 字数 2931 浏览 4 评论 0原文

我有一个用于评级的网络服务,它返回float,而使用XML解析器检索它总是返回null[0]。我不知道。我的代码如下:

public static String parse(InputStream is) 
        {
            XmlPullParserFactory factory = null;
            try {
                factory = XmlPullParserFactory.newInstance();
            } catch (XmlPullParserException e1) {
                // TODO Auto-generated catch block
                Log.v(TAG,"Exception in Factory pull");
            }

            factory.setNamespaceAware(true);

            XmlPullParser parser = null;

            try {
                parser = factory.newPullParser();
            } catch (XmlPullParserException e1) {
                Log.v(TAG,"Exception in parser pull");
            }

            String rating = "";
            //XmlPullParser parser = Xml.newPullParser();
            Log.v("XMLParser:", parser.toString());
            try 
            {
                // auto-detect the encoding from the stream
                parser.setInput(is, null);
                int eventType = parser.getEventType();
                //Message currentMessage = null;
                boolean done = false;
                while (eventType != XmlPullParser.END_DOCUMENT && !done)
                {
                    String name = null;
                    switch (eventType)
                    {
                        case XmlPullParser.START_DOCUMENT:
                            break;
                        case XmlPullParser.START_TAG:
                            name = parser.getName();
                            if (name.equalsIgnoreCase("Score"))
                            {
                                rating = parser.nextText();
                                Log.v("Rating", rating);
                                done = true;
                            }   
                            break;
                        case XmlPullParser.TEXT:
                            Log.v(TAG,"Node value:"+parser.getText());
                        case XmlPullParser.END_TAG:
                            break;
                    }
                    eventType = parser.next();
                }
            } 
            catch (Exception e) 
            {
                rating = "0";
            }
            return rating;

        }

我的 Web 服务 xml 是:

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
  <soap:Body>
    <GetRatingDResponse xmlns="http://tempuri.org/">
      <GetRatingDResult>
        <Votes>int</Votes>
        <Score>float</Score>
      </GetRatingDResult>
    </GetRatingDResponse>
  </soap:Body>
</soap:Envelope>

任何人都可以告诉我这段代码有什么问题吗?

I have a webservice for rating it return <Score>float</Score> while retrieving this using XML parser always return null[0]. I don't have any idea. My code is below:

public static String parse(InputStream is) 
        {
            XmlPullParserFactory factory = null;
            try {
                factory = XmlPullParserFactory.newInstance();
            } catch (XmlPullParserException e1) {
                // TODO Auto-generated catch block
                Log.v(TAG,"Exception in Factory pull");
            }

            factory.setNamespaceAware(true);

            XmlPullParser parser = null;

            try {
                parser = factory.newPullParser();
            } catch (XmlPullParserException e1) {
                Log.v(TAG,"Exception in parser pull");
            }

            String rating = "";
            //XmlPullParser parser = Xml.newPullParser();
            Log.v("XMLParser:", parser.toString());
            try 
            {
                // auto-detect the encoding from the stream
                parser.setInput(is, null);
                int eventType = parser.getEventType();
                //Message currentMessage = null;
                boolean done = false;
                while (eventType != XmlPullParser.END_DOCUMENT && !done)
                {
                    String name = null;
                    switch (eventType)
                    {
                        case XmlPullParser.START_DOCUMENT:
                            break;
                        case XmlPullParser.START_TAG:
                            name = parser.getName();
                            if (name.equalsIgnoreCase("Score"))
                            {
                                rating = parser.nextText();
                                Log.v("Rating", rating);
                                done = true;
                            }   
                            break;
                        case XmlPullParser.TEXT:
                            Log.v(TAG,"Node value:"+parser.getText());
                        case XmlPullParser.END_TAG:
                            break;
                    }
                    eventType = parser.next();
                }
            } 
            catch (Exception e) 
            {
                rating = "0";
            }
            return rating;

        }

My Web service xml is :

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
  <soap:Body>
    <GetRatingDResponse xmlns="http://tempuri.org/">
      <GetRatingDResult>
        <Votes>int</Votes>
        <Score>float</Score>
      </GetRatingDResult>
    </GetRatingDResponse>
  </soap:Body>
</soap:Envelope>

Can any body tell me what is wrong with this code?

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文