2011-06-27 131 views
7

我使用KSoap2爲我的Android應用程序調用Web服務。我正在使用以下代碼來調用Web服務。Android KSoap2:如何獲取屬性名稱

SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME); 
request.addProperty("PageSize", 20); 
request.addProperty("PageIndex", currentPage); 

SoapSerializationEnvelope soapEnvelope = new SoapSerializationEnvelope(SoapEnvelope.VER11); 
soapEnvelope.dotNet = true; 
soapEnvelope.setOutputSoapObject(request); 
HttpTransportSE aht = new HttpTransportSE(URL); 

try { 
    aht.call(SOAP_ACTION, soapEnvelope); 
    SoapObject result = (SoapObject) soapEnvelope.getResponse(); 

    Log.d("resBundle", String.valueOf(resBundle)); 

    int elementCount = resSoap.getPropertyCount(); 
    for(int i = 0;i<elementCount;i++){ 
    /////////////////////how to get the property name here//////////////// 
    } 

}catch (Exception e) { 
    e.printStackTrace(); 
    return null; 
} 

我從Web服務中獲得完美的響應。響應的String.valueOf低於:

anyType{NewsID=2186; NewsSubject=Lil Wayne Shows Up to Heat Game With Mystery Chick & Drake; NewsDetail=Looks like Weezy found him a main chick! Lil Wayne showed off his mystery girl yet again, this time at the Miami Heat Eastern Conference Finals game. Wanye looked proud to be with his girl while he kept his arm around her for most of the game. Drake was also in attendance with Wanye and it looks like he was having a great time cheering on the Heat as they beat the Bulls in overtime. Chad Ochocinco was also spotted enjoying the game, but Evelyn was no where to be seen. Check out more pics from the Miami game:; NewsArtist=494; ModifiedDate=2011-05-26T12:03:04.567+01:00; CreateDate=26 May, 2011 12:03PM; ImageName=26052011120304.jpg; ImageAlt=anyType{}; ShortNewsDetail=Looks like Weezy found him a main chick! Lil Wayne showed off his mystery girl y; } 

現在,我可以很容易很容易得到的財產的價值,但我也想要得到的name屬性(例如NewSID的,NewsSubject,NewsArtist,ModifiedDate)。我如何獲得房產的名稱?

回答

15

在你迴應的地方,你可以通過difrent屬性來訪問PropertyInfo。我用下面的設置以獲取參數的名稱和他們一起去的值:

//Inside your for loop 
PropertyInfo pi = new PropertyInfo(); 
resSoap.getPropertyInfo(i, pi); 
Log.d(TAG, pi.name + " : " + resSoap.getProperty(i).toString()); 

這將創建一個PropertyInfo對象,增加了從該對象的屬性信息,然後,您可以訪問所有這一切信息。然後以格式「propertyname:propertyvalue」打印到LogCat中