Questo codice FUNZIONANTE si connette al Web Service, richiama il metodo desiderato passandogli una temperatura in celcius, visualizza ciò che gli è stato restituito dal Web Service, ovvero la temperatura in Fharenhait.
1
2package omar.prova2;
3import java.io.IOException;
4import org.ksoap2.SoapEnvelope;
5import org.ksoap2.serialization.SoapObject;
6import org.ksoap2.serialization.SoapPrimitive;
7import org.ksoap2.serialization.SoapSerializationEnvelope;
8import org.ksoap2.transport.AndroidHttpTransport;
9import org.xmlpull.v1.XmlPullParserException;
10import omar.prova2.R;
11import android.app.Activity;
12import android.os.Bundle;
13import android.widget.TextView;
14"deprecation") (
15public class prova2 extends Activity
16{ private static final String SOAP_ACTION = "http://tempuri.org/CelsiusToFahrenheit"; //preso dall'xml
17private static final String METHOD_NAME = "CelsiusToFahrenheit"; //nome del metodo
18private static final String NAMESPACE = "http://tempuri.org/"; //preso dall'xml
19private static final String URL = "http://www.w3schools.com/webservices/tempconvert.asmx"; //url all'xml
20private TextView tv;
21/** Called when the activity is first created. */
22
23public void onCreate(Bundle savedInstanceState) {
24super.onCreate(savedInstanceState);
25setContentView(R.layout.main);
26tv=(TextView)findViewById(R.id.Result);
27SoapObject Request = new SoapObject(NAMESPACE, METHOD_NAME);
28Request.addProperty("Celsius","32" );
29SoapSerializationEnvelope soapEnvelope= new SoapSerializationEnvelope(SoapEnvelope.VER11);
30soapEnvelope.dotNet=true;
31soapEnvelope.setOutputSoapObject(Request);
32AndroidHttpTransport aht=new AndroidHttpTransport(URL);
33try { aht.call(SOAP_ACTION, soapEnvelope);
34SoapPrimitive resultString = (SoapPrimitive) soapEnvelope.getResponse();
35tv.setText("Result:" + resultString);
36}
37catch (IOException e) {tv.setText(e.toString());} catch (XmlPullParserException e) {tv.setText(e.toString());}
38}
39}
40
Il problema è che non è questo il Web Service su cui devo lavorare, ed adoperando tale codice sul mio, esso non riconosce la richiesta che gli invio e mi risponde con campi nulli.
In particolare la richiesta XML che gli invio (recuperata tramite uno sniffer http) è:
161<v:Envelope
2xmlns:i="http://www.w3.org/2001/XMLSchema-instance"
3xmlns:d="http://www.w3.org/2001/XMLSchema"
4xmlns:c="http://schemas.xmlsoap.org/soap/encoding/"
5xmlns:v="http://schemas.xmlsoap.org/soap/envelope/"
6>
7<v:Header/>
8<v:Body>
9<GetTicket xmlns="http://www.mustcoursemaker.net/WebServices/" id="o0" c:root="1">
10<UserID i:type="d:string">x</UserID>
11<Password i:type="d:string">x</Password>
12<LangID i:type="d:string">x</LangID>
13<SourceSystemCode i:type="d:string">x</SourceSystemCode>
14</GetTicket>
15</v:Body>
16</v:Envelope>
Mentre quella che si aspetta (recuperata tramite una combinazione di sniffer e SOAPUI) è:
161<soap:Envelope
2xmlns:soap="http://www.w3.org/2003/05/soap-envelope"
3xmlns:web="http://www.mustcoursemaker.net/WebServices/"
4>
5<soap:Header/>
6<soap:Body>
7<web:GetTicket>
8<web:Request>
9<web:UserID>ax</web:UserID>
10<web:Password>x</web:Password>
11<web:LangID>x</web:LangID>
12<web:SourceSystemCode>x</web:SourceSystemCode>
13</web:Request>
14</web:GetTicket>
15</soap:Body>
16</soap:Envelope>
Come si può notare la differenza sostanziale sta nel tag REQUEST, che nella richiesta funzionante è presente, mentre in quella non funzionante no. Andando nella descrizione del metodo nel web service:
11<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> <GetTicket xmlns="http://www.mustcoursemaker.net/WebServices/"> <Request> <UserID>string</UserID> <Password>string</Password> <LangID>string</LangID> <SourceSystemCode>string</SourceSystemCode> </Request> </GetTicket> </soap:Body> </soap:Envelope>
Si può notare che effettivamente è presente il campo Request.
ORA, COME POSSO INSERIRE QUESTO CAMPO?
E' DAVVERO SOLO QUESTO IL PROBLEMA? (non possono centrare problemi di compatibilità fra RCP, LITERAL, DOCUMENT, ENCODED???)
Ho tentato ad aggiungere Request tramite questo codice
531
2package omar.prova3;
3import java.io.IOException;
4import org.ksoap2.SoapEnvelope;
5import org.ksoap2.serialization.PropertyInfo;
6import org.ksoap2.serialization.SoapObject;
7import org.ksoap2.serialization.SoapSerializationEnvelope;
8import org.ksoap2.transport.AndroidHttpTransport;
9import org.xmlpull.v1.XmlPullParserException;
10import omar.prova3.R;
11import android.app.Activity;
12import android.os.Bundle;
13import android.widget.TextView;
14"deprecation") (
15public class prova3 extends Activity
16{
17private static final String SOAP_ACTION = "http://www.mustcoursemaker.net/WebServices/GetTicket"; //preso dall'xml
18private static final String ELEMENT_NAME = "Request"; //nome del metodo
19private static final String METHOD_NAME = "GetTicket"; //nome del metodo
20private static final String NAMESPACE = "http://www.mustcoursemaker.net/WebServices/"; //preso dall'xml
21private static final String URL = "http://www.mustcoursemaker.net/direct/webservices/wslogin.asmx"; //url all'xml
22private TextView tv;
23/** Called when the activity is first created. */
24
25public void onCreate(Bundle savedInstanceState) {
26super.onCreate(savedInstanceState);
27setContentView(R.layout.main);
28tv=(TextView)findViewById(R.id.Result);
29
30SoapObject data = new SoapObject(NAMESPACE, ELEMENT_NAME);
31data.addProperty("UserID","x");
32data.addProperty("Password","x");
33data.addProperty("LangID","x");
34data.addProperty("SourceSystemCode","x");
35
36SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
37request.addProperty("Request", data);
38
39SoapSerializationEnvelope soapEnvelope= new SoapSerializationEnvelope(SoapEnvelope.VER11);
40soapEnvelope.bodyOut = request;
41soapEnvelope.dotNet=true;
42
43
44AndroidHttpTransport aht=new AndroidHttpTransport(URL);
45try { aht.debug=true;
46aht.call(SOAP_ACTION, soapEnvelope);
47Object resultString = (Object) soapEnvelope.getResponse();
48tv.setText("Result:" + resultString);
49}
50catch (IOException e) {tv.setText(e.toString());} catch (XmlPullParserException e) {tv.setText(e.toString());}
51}
52}
53
Ma mi restituisce un nuovo errore (che bello), lunghissimo da scrivere, la cui parte più importante penso sia:
" Server was unable to read Request [...] There is an error in XML document (1,315) [...] The specified type was note recognized: name='Request', namespace='http:// ecc'"
Termino allegando la sniffata di quest'ultima prova che ho fatto:
Codice (XML): [Seleziona]
<v:Envelope
xmlns:i="http://www.w3.org/2001/XMLSchema-instance"
xmlns:d="http://www.w3.org/2001/XMLSchema"
xmlns:c="http://schemas.xmlsoap.org/soap/encoding/"
xmlns:v="http://schemas.xmlsoap.org/soap/envelope/">
<v:Header />
<v:Body>
<GetTicket xmlns="http://www.mustcoursemaker.net/WebServices/" id="o0" c:root="1">
<Request i:type="n0:GetTicket" xmlns:n0="http://www.mustcoursemaker.net/WebServices/">
<UserID i:type="d:string">x</UserID>
<Password i:type="d:string">x</Password>
<LangID i:type="d:string">x</LangID>
<SourceSystemCode i:type="d:string">x</SourceSystemCode>
</Request>
</GetTicket>
</v:Body>
</v:Envelope>
PER FAVORE AIUTATEMI SONO STATO PIù ESAUSTIVO POSSIBILE. E' UNA SETTIMANA CHE CI PROVO. IN RETE NON SI TROVA DOCUMENTAZIONE DEGNA DI QUESTO NOME PER STA DIAMINE DI KSOAP2!!!
GRAZIE