有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java无法从XML响应中获取属性

这是我第一次使用kSoap2并真正构建一个安卓应用程序,但是我很难让它工作,我试图从列表中提取每个调用点,但是我只是因为一个空指针而崩溃,因为它找不到任何属性

以下是我试图与之合作的回应

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope
xmlns:soap="http://www.w3.org/2003/05/soap-envelope"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<soap:Body>
    <GetServiceDetailsResponse
        xmlns="http://thalesgroup.com/RTTI/2014-02-20/ldb/">
        <GetServiceDetailsResult
            xmlns:lt="http://thalesgroup.com/RTTI/2012-01-13/ldb/types"
            xmlns:lt6="http://thalesgroup.com/RTTI/2017-02-02/ldb/types"
            xmlns:lt7="http://thalesgroup.com/RTTI/2017-10-01/ldb/types"
            xmlns:lt4="http://thalesgroup.com/RTTI/2015-11-27/ldb/types"
            xmlns:lt5="http://thalesgroup.com/RTTI/2016-02-16/ldb/types"
            xmlns:lt2="http://thalesgroup.com/RTTI/2014-02-20/ldb/types"
            xmlns:lt3="http://thalesgroup.com/RTTI/2015-05-14/ldb/types">
            <lt2:generatedAt>2018-03-12T01:54:31.6803098+00:00</lt2:generatedAt>
            <lt2:serviceType>train</lt2:serviceType>
            <lt2:locationName>London St Pancras (Intl)</lt2:locationName>
            <lt2:crs>STP</lt2:crs>
            <lt2:operator>Thameslink</lt2:operator>
            <lt2:operatorCode>TL</lt2:operatorCode>
            <lt2:std>02:20</lt2:std>
            <lt2:etd>On time</lt2:etd>
            <lt2:previousCallingPoints />
            <lt2:subsequentCallingPoints>
                <lt2:callingPointList serviceType="train" serviceChangeRequired="false" assocIsCancelled="false">
                    <lt2:callingPoint>
                        <lt:locationName>West Hampstead Thameslink</lt:locationName>
                        <lt:crs>WHP</lt:crs>
                        <lt:st>02:27</lt:st>
                        <lt:et>On time</lt:et>
                    </lt2:callingPoint>
                    <lt2:callingPoint>
                        <lt:locationName>Cricklewood</lt:locationName>
                        <lt:crs>CRI</lt:crs>
                        <lt:st>02:31</lt:st>
                        <lt:et>On time</lt:et>
                    </lt2:callingPoint>
                    <lt2:callingPoint>
                        <lt:locationName>Hendon</lt:locationName>
                        <lt:crs>HEN</lt:crs>
                        <lt:st>02:34</lt:st>
                        <lt:et>On time</lt:et>
                    </lt2:callingPoint>
                    <lt2:callingPoint>
                        <lt:locationName>Mill Hill Broadway</lt:locationName>
                        <lt:crs>MIL</lt:crs>
                        <lt:st>02:38</lt:st>
                        <lt:et>On time</lt:et>
                    </lt2:callingPoint>
                    <lt2:callingPoint>
                        <lt:locationName>Elstree &amp; Borehamwood</lt:locationName>
                        <lt:crs>ELS</lt:crs>
                        <lt:st>02:42</lt:st>
                        <lt:et>On time</lt:et>
                    </lt2:callingPoint>
                    <lt2:callingPoint>
                        <lt:locationName>Radlett</lt:locationName>
                        <lt:crs>RDT</lt:crs>
                        <lt:st>02:47</lt:st>
                        <lt:et>On time</lt:et>
                    </lt2:callingPoint>
                    <lt2:callingPoint>
                        <lt:locationName>St Albans</lt:locationName>
                        <lt:crs>SAC</lt:crs>
                        <lt:st>02:53</lt:st>
                        <lt:et>On time</lt:et>
                    </lt2:callingPoint>
                    <lt2:callingPoint>
                        <lt:locationName>Harpenden</lt:locationName>
                        <lt:crs>HPD</lt:crs>
                        <lt:st>03:00</lt:st>
                        <lt:et>On time</lt:et>
                    </lt2:callingPoint>
                    <lt2:callingPoint>
                        <lt:locationName>Luton Airport Parkway</lt:locationName>
                        <lt:crs>LTN</lt:crs>
                        <lt:st>03:05</lt:st>
                        <lt:et>On time</lt:et>
                    </lt2:callingPoint>
                    <lt2:callingPoint>
                        <lt:locationName>Luton</lt:locationName>
                        <lt:crs>LUT</lt:crs>
                        <lt:st>03:08</lt:st>
                        <lt:et>On time</lt:et>
                    </lt2:callingPoint>
                    <lt2:callingPoint>
                        <lt:locationName>Leagrave</lt:locationName>
                        <lt:crs>LEA</lt:crs>
                        <lt:st>03:13</lt:st>
                        <lt:et>On time</lt:et>
                    </lt2:callingPoint>
                    <lt2:callingPoint>
                        <lt:locationName>Harlington</lt:locationName>
                        <lt:crs>HLN</lt:crs>
                        <lt:st>03:18</lt:st>
                        <lt:et>On time</lt:et>
                    </lt2:callingPoint>
                    <lt2:callingPoint>
                        <lt:locationName>Flitwick</lt:locationName>
                        <lt:crs>FLT</lt:crs>
                        <lt:st>03:22</lt:st>
                        <lt:et>On time</lt:et>
                    </lt2:callingPoint>
                    <lt2:callingPoint>
                        <lt:locationName>Bedford</lt:locationName>
                        <lt:crs>BDM</lt:crs>
                        <lt:st>03:35</lt:st>
                        <lt:et>On time</lt:et>
                    </lt2:callingPoint>
                </lt2:callingPointList>
            </lt2:subsequentCallingPoints>
        </GetServiceDetailsResult>
    </GetServiceDetailsResponse>
</soap:Body>
</soap:Envelope>

下面是我用来尝试从Soap消息中提取数据的代码

SoapObject trainServices = (SoapObject) response
            .getProperty("callingPoint");
    Log.d("gen", String.valueOf(trainServices.getPropertyCount()));
    for (int i = 0; i < trainServices.getPropertyCount(); i++)
    {
        Log.d("this", "looped2");
        Object property = trainServices.getProperty(i);
        if (property instanceof SoapObject) {
            SoapObject trainService = (SoapObject) property;
            Log.d("this", "looped");
            String std = (trainService.hasProperty("std") ? trainService
                    .getPropertyAsString("std") : null);
            String etd = (trainService.hasProperty("etd") ? trainService
                    .getPropertyAsString("etd") : null);
            String platform = (trainService.hasProperty("platform") ? trainService
                    .getPropertyAsString("platform") : null);
            String operator = (trainService.hasProperty("operator") ? trainService
                    .getPropertyAsString("operator") : null);
            String operatorCode = (trainService.hasProperty("operatorCode") ? trainService
                    .getPropertyAsString("operatorCode") : null);
            String serviceID = (trainService.hasProperty("serviceID") ? trainService
                    .getPropertyAsString("serviceID") : null);
            String sta = (trainService.hasProperty("sta") ? trainService
                    .getPropertyAsString("sta"): null);
            String eta = (trainService.hasProperty("eta") ? trainService
                    .getPropertyAsString("eta") : null);
        }
        //CallingPoints.add(new serviceDetails(locationName,"","","","","","",sta,eta,ata,etd,atd));
    }

共 (0) 个答案