KSOAP2 Problem: addProperty kein Property-Name vorhanden

J

Jeff515

Neues Mitglied
0
Hallo,

Ich nutze KSOAP2 um einen WebService zu benutzen. Das klappt i.d.R. gut, z.B. wie in folgenden Beispiel. Mir kommt es hier insbesondere auf die Übergabe eines Attributs an. Das funktioniert ja normalerweise mit addProperty(Name,Wert):


Code:
  private static final String METHOD_NAME = "AllForwards";
  private static final String SOAP_ACTION = "http://footballpool.dataaccess.eu/data/AllForwards";
  private static final String NAMESPACE = "http://footballpool.dataaccess.eu";
  private static final String URL = "http://footballpool.dataaccess.eu/data/info.wso?WSDL";
   
  SoapObject Request = new SoapObject(NAMESPACE, METHOD_NAME);
   
  Request.addProperty("sCountryName", "Germany");
   ...
  ...
In der dazugehörigen WSDL ist genau für diese Property (sCountryName) auch ein <element>-Tag vorhanden:

Code:
  ..
  ..<xs:element name="AllForwards">
  - <xs:complexType>
  - <xs:sequence>
    <xs:element name="sCountryName" type="xs:string" /> 
    </xs:sequence>
    </xs:complexType>
    </xs:element>
  ..
  ..
Und nun zu meinem Problem. Ich möchte eine andere Methode eines WebServices nutzen, die aber im Prinzip bei der Parameterübergabe nur einen int-Wert benötigt, d.h. die Property hat keinen Namen. Das funktioniert auch grundsätzlich, z.B. bei einer Implementierung in C#:

Code:
  WebReference1.NewWebServiceService car = new WebReference1.NewWebServiceService();
  WebService_Test.WebReference1.ndsrapos result = new WebService_Test.WebReference1.ndsrapos();
   
  result = car.getLastPositionOfCar(2);
              
  Console.WriteLine(result.fuel);
Wie ihr sehen könnt, benötigt die Methode getLastPositionOfCar() nur einen int-Wert und liefert mir dann ein Ergebnis. Diese Methode möchte ich nun in Android über KSOAP2 benutzen, aber ich weiß nicht wie, da man ja wie im ersten Beispiel zu sehen immer ein Propertynamen und einen –wert braucht:

Code:
  Request.addProperty("name", 2);
In der dazugehörigen WDSL-Datei existiert übrigens kein <element>-Tag aus dem ich ggf. den Propertynamen erkennen könnte (Sorry, aber die Datei kann ich hier leider nicht veröffentlichen).

Hat jemand eine Idee, oder muss ich die WDSL überarbeiten (wurde mit JAX-WS generiert).

Danke und viele Grüße,

Daniel
 
Hallo,

ich habe das Problem jetzt folgendermaßen gelöst: Ich verwende nun HTTPCLient und schicke dieser Instanz eine POST-Anfrage an die ich vorher eine SOAP-Anfrage gehängt habe. Das klappt wunderbar!

Meine Vorgehensweise:
Zuerst habe ich soapUI für Eclipse installiert, damit kann man toll WebServices testen. Man benötigt das Tool in diesem Fall, um sich den korrekten Request generieren zu lassen. Diesen kopiert man sich dann und baut ihn in folgenden Code ein:

Code:
[B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]private[/COLOR][/SIZE][/COLOR][/SIZE][/B][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]static[/COLOR][/SIZE][/COLOR][/SIZE][/B][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]final[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] String [/SIZE][I][U][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]SOAP_ACTION = soap action;[/COLOR][/SIZE][/COLOR][/SIZE][/U][/I]
[B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]private[/COLOR][/SIZE][/COLOR][/SIZE][/B][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]static[/COLOR][/SIZE][/COLOR][/SIZE][/B][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]final[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] String [/SIZE][I][U][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]URL = Link zur WSDL;[/COLOR][/SIZE][/COLOR][/SIZE][/U][/I]
 
<!-- Hier der Request-String aus soapUI: -->
 
[SIZE=2]String reqStr = [/SIZE]
[LEFT][SIZE=2][COLOR=#2a00ff][SIZE=2][COLOR=#2a00ff]"<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:ws=\"http:///\">"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]+[/SIZE][/LEFT]
 
[LEFT][SIZE=2][COLOR=#2a00ff][SIZE=2][COLOR=#2a00ff]"<soapenv:Header/><soapenv:Body>soapenv:Body></soapenv:Envelope>"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2];[/SIZE][/LEFT]
 
 
[LEFT][SIZE=2]String resp = [/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]null[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2];[/SIZE][/LEFT]
 
[LEFT][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]try[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] {[/SIZE][/LEFT]
 
[LEFT][SIZE=2]HttpClient httpclient = [/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]new[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] DefaultHttpClient();[/SIZE][/LEFT]
 
[LEFT][SIZE=2]HttpPost post = [/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]new[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] HttpPost( [/SIZE][I][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]URL[/COLOR][/SIZE][/COLOR][/SIZE][/I][SIZE=2] );[/SIZE]

//[COLOR=#3f7f5f](Hier auskommentiert, da die Action nicht immer benötigt wird!)[/COLOR]
[LEFT][SIZE=2][COLOR=#3f7f5f][SIZE=2][COLOR=#3f7f5f]//post.setHeader("[U]soapaction[/U]", SOAP_ACTION); [/COLOR][/SIZE][/COLOR][/SIZE]
[SIZE=2]post.setHeader([/SIZE][SIZE=2][COLOR=#2a00ff][SIZE=2][COLOR=#2a00ff]"Content-Type"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2], [/SIZE][SIZE=2][COLOR=#2a00ff][SIZE=2][COLOR=#2a00ff]"text/xml; charset=utf-8"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]);[/SIZE][/LEFT]
[/LEFT]

 
 
[LEFT][SIZE=2]StringEntity entity = [/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]new[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] StringEntity(reqStr);[/SIZE][/LEFT]
 
[LEFT][SIZE=2]post.setEntity(entity); [/SIZE][/LEFT]
 
[LEFT][SIZE=2]HttpResponse response = httpclient.execute(post);[/SIZE]

[SIZE=2]HttpEntity e = response.getEntity();[/SIZE]
[LEFT][SIZE=2][COLOR=#3f7f5f][SIZE=2][COLOR=#3f7f5f]//System.out.[U]println[/U]("response"+e.toString());[/COLOR][/SIZE][/COLOR][/SIZE]
[B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]if[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] (e != [/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]null[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2]) {[/SIZE]
[SIZE=2]InputStream instream = e.getContent();[/SIZE]
[SIZE=2]InputStreamReader read = [/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]new[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] InputStreamReader( instream, [/SIZE][SIZE=2][COLOR=#2a00ff][SIZE=2][COLOR=#2a00ff]"UTF-8"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] ); [/SIZE]
[SIZE=2]StringBuilder sb = [/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]new[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] StringBuilder(); [/SIZE]
[B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]int[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] ch = read.read(); [/SIZE]
[B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]while[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2]( ch != -1 ){ [/SIZE]
[SIZE=2]sb.append(([/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]char[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2])ch); [/SIZE]
[SIZE=2]ch = read.read(); [/SIZE]
[SIZE=2]} [/SIZE]
[SIZE=2]resp = sb.toString();[/SIZE]
[SIZE=2]read.close();[/SIZE]
[SIZE=2]}[/SIZE]
[SIZE=2]}[/SIZE]
[B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]catch[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2](Exception e){[/SIZE][/LEFT]
[/LEFT]

 
 
 
[LEFT][SIZE=2]}[/SIZE][/LEFT]

Die Antwort des WebService erhält man als String so in "resp". Nun diesen String nur durchsuchen und sich die benötigten Informationen rausholen!​

Viele Grüße,
Daniel​
 

Ähnliche Themen

Vapeilas91
  • Vapeilas91
Antworten
3
Aufrufe
698
swa00
swa00
2
Antworten
6
Aufrufe
261
24559
2
W
  • WuDiDong
Antworten
3
Aufrufe
767
jogimuc
J
Zurück
Oben Unten