CERCA
PER MODELLO
FullScreen Chatbox! :)

Utente del giorno: carotix con ben 2 Thanks ricevuti nelle ultime 24 ore
Utente della settimana: carotix con ben 6 Thanks ricevuti negli ultimi sette giorni
Utente del mese: carotix con ben 24 Thanks ricevuti nell'ultimo mese

Visualizzazione dei risultati da 1 a 2 su 2
Discussione:

CONNESSIONE A WEB SERVICE (caso particolare)

Se questa discussione ti è stata utile, ti preghiamo di lasciare un messaggio di feedback in modo che possa essere preziosa in futuro anche per altri utenti come te!
  1. #1
    Baby Droid


    Registrato dal
    May 2011
    Messaggi
    2

    Ringraziamenti
    0
    Ringraziato 0 volte in 0 Posts
    Predefinito

    CONNESSIONE A WEB SERVICE (caso particolare)

    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
    2
    package omar.prova2;
    3
    import java.io.IOException;
    4
    import org.ksoap2.SoapEnvelope;
    5
    import org.ksoap2.serialization.SoapObject;
    6
    import org.ksoap2.serialization.SoapPrimitive;
    7
    import org.ksoap2.serialization.SoapSerializationEnvelope;
    8
    import org.ksoap2.transport.AndroidHttpTransport;
    9
    import org.xmlpull.v1.XmlPullParserException;
    10
    import omar.prova2.R;
    11
    import android.app.Activity;
    12
    import android.os.Bundle;
    13
    import android.widget.TextView;
    14
    @SuppressWarnings("deprecation")
    15
    public class prova2 extends Activity 
    16
    {   private static final String SOAP_ACTION = "http://tempuri.org/CelsiusToFahrenheit"; //preso dall'xml
    17
        private static final String METHOD_NAME = "CelsiusToFahrenheit"; //nome del metodo
    18
        private static final String NAMESPACE = "http://tempuri.org/"; //preso dall'xml
    19
        private static final String URL = "http://www.w3schools.com/webservices/tempconvert.asmx"; //url all'xml
    20
        private TextView tv;
    21
        /** Called when the activity is first created. */
    22
        @Override
    23
        public void onCreate(Bundle savedInstanceState) {
    24
            super.onCreate(savedInstanceState);
    25
            setContentView(R.layout.main);
    26
            tv=(TextView)findViewById(R.id.Result);
    27
            SoapObject Request = new SoapObject(NAMESPACE, METHOD_NAME);  
    28
            Request.addProperty("Celsius","32" ); 
    29
            SoapSerializationEnvelope soapEnvelope= new SoapSerializationEnvelope(SoapEnvelope.VER11);
    30
            soapEnvelope.dotNet=true;
    31
            soapEnvelope.setOutputSoapObject(Request);
    32
            AndroidHttpTransport aht=new AndroidHttpTransport(URL);     
    33
            try {   aht.call(SOAP_ACTION, soapEnvelope);
    34
                    SoapPrimitive resultString = (SoapPrimitive) soapEnvelope.getResponse();
    35
                    tv.setText("Result:" + resultString);
    36
                }
    37
            catch (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) è:
    16
     
    1
    <v:Envelope 
    2
        xmlns:i="http://www.w3.org/2001/XMLSchema-instance" 
    3
        xmlns:d="http://www.w3.org/2001/XMLSchema" 
    4
        xmlns:c="http://schemas.xmlsoap.org/soap/encoding/" 
    5
        xmlns: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) è:
    16
     
    1
    <soap:Envelope 
    2
        xmlns:soap="http://www.w3.org/2003/05/soap-envelope" 
    3
        xmlns: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:

    1
     
    1
    <?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> <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
    53
     
    1
    2
    package omar.prova3;
    3
    import java.io.IOException;
    4
    import org.ksoap2.SoapEnvelope;
    5
    import org.ksoap2.serialization.PropertyInfo;
    6
    import org.ksoap2.serialization.SoapObject;
    7
    import org.ksoap2.serialization.SoapSerializationEnvelope;
    8
    import org.ksoap2.transport.AndroidHttpTransport;
    9
    import org.xmlpull.v1.XmlPullParserException;
    10
    import omar.prova3.R;
    11
    import android.app.Activity;
    12
    import android.os.Bundle;
    13
    import android.widget.TextView;
    14
    @SuppressWarnings("deprecation")
    15
    public class prova3 extends Activity 
    16
    {
    17
            private static final String SOAP_ACTION = "http://www.mustcoursemaker.net/WebServices/GetTicket"; //preso dall'xml
    18
            private static final String ELEMENT_NAME = "Request"; //nome del metodo
    19
            private static final String METHOD_NAME = "GetTicket"; //nome del metodo
    20
        private static final String NAMESPACE = "http://www.mustcoursemaker.net/WebServices/"; //preso dall'xml
    21
        private static final String URL = "http://www.mustcoursemaker.net/direct/webservices/wslogin.asmx"; //url all'xml
    22
        private TextView tv;
    23
        /** Called when the activity is first created. */
    24
        @Override
    25
        public void onCreate(Bundle savedInstanceState) {
    26
            super.onCreate(savedInstanceState);
    27
            setContentView(R.layout.main);
    28
            tv=(TextView)findViewById(R.id.Result);
    29
            
    30
            SoapObject data = new SoapObject(NAMESPACE, ELEMENT_NAME);
    31
            data.addProperty("UserID","x");
    32
            data.addProperty("Password","x");
    33
            data.addProperty("LangID","x");
    34
            data.addProperty("SourceSystemCode","x");
    35
                    
    36
            SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
    37
            request.addProperty("Request", data);
    38
                    
    39
            SoapSerializationEnvelope soapEnvelope= new SoapSerializationEnvelope(SoapEnvelope.VER11);
    40
            soapEnvelope.bodyOut = request;
    41
            soapEnvelope.dotNet=true;
    42
    43
    44
            AndroidHttpTransport aht=new AndroidHttpTransport(URL);     
    45
            try {   aht.debug=true;
    46
                            aht.call(SOAP_ACTION, soapEnvelope);
    47
                            Object resultString = (Object) soapEnvelope.getResponse();
    48
                                    tv.setText("Result:" + resultString);
    49
                    }
    50
                    catch (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
    Ultima modifica di OmarLuigi; 13-05-11 alle 15:04

  2.  
  3. #2
    Baby Droid


    Registrato dal
    May 2011
    Messaggi
    2

    Ringraziamenti
    0
    Ringraziato 0 volte in 0 Posts
    Predefinito

    Ho risolto.
    In pratica al codice corretto che avevo postato... Mancava un istruzione xD Un istruzione che, però, era fondamentale in quanto la struttura XML prevede campi incapsulati fra loro (in particolare, come detto nel post, è presente il campo <Request> all'interno di <GetTicket>)
    L'istruzione a cui mi riferisco è "soapEnvelope.implicitType=true"...
    Essa praticamente dichiara il fatto che all'interno della busta SOAP che si invierà sono appunto presenti tali campi incapsulati...
    Ed in questo modo nel documento XML sarà presnete il campo <Request> privo di attributi! (e non come prima in cui c'era <Request namespace='' ecc>

    Insomma si fa così!!! Ho provato la stessa metodologia con un latro WS che restituisce una risposta ben più complicata... E funziona!!!!

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire risposte
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Torna su
Privacy Policy