IWA and WebServices

730 views
Skip to first unread message

essequadra

unread,
Apr 12, 2012, 12:12:57 PM4/12/12
to codenameone...@googlegroups.com
Hi, I've a problem to access a .NET web service where integrated windows authentication is activated. I use connectionManager and connectionRequest and  I would like to know where is intended to set username and password.

Thank you.

Shai Almog

unread,
Apr 12, 2012, 1:37:03 PM4/12/12
to codenameone...@googlegroups.com
Hi,
there is currently no builtin support for SOAP based web services. You can construct the XML manually and parse it using the XML parser but there is no explicit support for that.

We intend to offer broader webservice support in the future.

Right now I suggest you create a server proxy that exposes a simplified REST interface.

f...@smsgroup.com

unread,
Jan 11, 2013, 2:12:40 PM1/11/13
to codenameone...@googlegroups.com
I am trying to connect to a SOAP web service also, and do not want to create REST interface at this time.  I have not problem in creating the XML document for the SOAP request.  The problem I am having is that I can not see how to attach the SOAP document, which I have built as a string, to the ConnectionRequest and then send it to the server.  Can you point me to the correct methods or steps to do this?
Thank you

Eric Coolman

unread,
Jan 11, 2013, 2:41:14 PM1/11/13
to codenameone...@googlegroups.com
The soap document will need to go out in the payload of the request.  You'll need to extend the ConnectionRequest object and override buildRequestBody()

Eric

f...@smsgroup.com

unread,
Jan 11, 2013, 5:40:52 PM1/11/13
to codenameone...@googlegroups.com
Eric,
Thank you that helped a lot. 

But I seem to be missing something else. 

I am getting a 415: Unsupported Media Type error message.  I have set the "Content Type" to "application/soap+xml; charset=utf-8", but that did not seem to help.  Can anyone share some light on what else I might need to set?  Below is a sample of the code.
public class webServices extends ConnectionRequest{

    public webServices() {
        this.setUrl("https://silverlight.smsgroup.com/e4services/beta");
        this.setContentType("application/soap+xml; charset=utf-8");
        this.setPost(true);
        NetworkManager.getInstance().addToQueue(this);
    }

    @Override
    protected void readResponse(InputStream input) throws IOException {
        byte[] bi = Util.readInputStream(input);
        System.out.println(new String(bi));
    }

    @Override
    protected void buildRequestBody(OutputStream os) throws IOException {
        String body = "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:q0=\"urn:smsgroup:sys:session:ConnectToSession\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"> <soapenv:Body> <q0:serverLoginEmployee_json><q0:cLogin>flb</q0:cLogin><q0:cPassword>flb</q0:cPassword></q0:serverLoginEmployee_json></soapenv:Body></soapenv:Envelope>";
       
        os.write(body.getBytes("UTF-8"));
        super.buildRequestBody(os);
    }
}

Eric Coolman

unread,
Jan 11, 2013, 8:36:09 PM1/11/13
to codenameone...@googlegroups.com
Strange, that looks fine, try viewing the network traffic in the simulator (click Simulate, Network Monitor) to see if the headers are getting set as you expect.

I just simulated your post using curl with:

curl -X POST -D @data.xml --header "Content-Type=application/soap+xml; charset=utf-8" https://silverlight.smsgroup.com/e4services/beta

Which recognizes the Content-Type (without it, I got the same 415).  The above gave me a "400: Missing SOAPAction header" which sounds more reasonable because I didn't pass that. 

Eric Coolman

unread,
Jan 11, 2013, 8:47:08 PM1/11/13
to codenameone...@googlegroups.com
One issue I do see in the code is that super.buildRequestBody(os) should be called at the top of your method, as it set the request arguments.  If you're calling addArgument() at all, you'll have extra data at the end of the payload.

Eric

Gordons

unread,
Jan 23, 2013, 6:38:13 AM1/23/13
to codenameone...@googlegroups.com
Please guys,

What's the update on this issue as I am faced with same issue of connecting to a SOAP webservice? Is there a better way to achieve this? has someone scaled through the hurdle yet?

Thanks.

f...@smsgroup.com

unread,
Jan 23, 2013, 9:53:01 AM1/23/13
to codenameone...@googlegroups.com
Gordons,
I have gotten my web services working.  The main problem is that currently the framework does not have a way to create and write a XML document.  I do believe you can modify a previously existing one.  Attached is a class that I wrote to help me make calls to my web services.  I do not if it will work for you, but it might help you get to where you want to be.

The main issue I was having, was in getting the server to accept the request.  If you look in the constructor of the class you will see where I add a "requestHeader" and also set the content type.  Both of these are important, and it is important that the "SOAPAction" requestHeader I added have something in it.
WebServices.java

Gordons

unread,
Jan 23, 2013, 10:44:16 AM1/23/13
to codenameone...@googlegroups.com
Thanks a lot for sharing this with me. I will tweak it to suit my need and I will let you know when it works

Thanks once again.

f...@smsgroup.com

unread,
Mar 7, 2013, 3:49:57 PM3/7/13
to codenameone...@googlegroups.com
I had someone send me an email regarding this today, so I decided to post the code in line, instead of just as an attachment.

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.sms.myapp.libraries;

import com.codename1.io.ConnectionRequest;
import com.codename1.io.NetworkManager;
import com.codename1.io.Util;
import com.codename1.ui.Dialog;
import com.codename1.ui.events.ActionEvent;
import com.codename1.ui.events.ActionListener;
import com.codename1.util.StringUtil;
import com.codename1.xml.Element;
import com.codename1.xml.XMLParser;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author flb
 */
abstract public class WebServices extends ConnectionRequest{
   List<ActionListener> listeners = new ArrayList<ActionListener>();

    @Override
    public void addResponseListener(ActionListener a) {
        listeners.add(a);
    }

    abstract public String getInputParams();
    abstract public void loadData();
   
    private boolean waitFor = false;

    /**
     * Set the value of waitFor
     *
     * @param waitFor new value of waitFor
     */
    public void setWaitFor(boolean waitFor) {
        this.waitFor = waitFor;
    }

    private String urn;

    /**
     * Get the value of urn
     *
     * @return the value of urn
     */
    public String getUrn() {
        return urn;
    }

    /**
     * Set the value of urn
     *
     * @param urn new value of urn
     */
    public void setUrn(String urn) {
        this.urn = urn;
    }
    private String operation;

    /**
     * Get the value of operation
     *
     * @return the value of operation
     */
    public String getOperation() {
        return operation;
    }

    /**
     * Set the value of operation
     *
     * @param operation new value of operation
     */
    public void setOperation(String operation) {
        this.operation = operation;
    }
   
        private String responseString;

    /**
     * Get the value of responseString
     *
     * @return the value of responseString
     */
    public String getResponseString() {
        return responseString;       
    }

    /**
     * Set the value of responseString
     *
     * @param responseString new value of responseString
     */
    private void setResponseString(String responseString) {
        this.responseString = StringUtil.replaceAll(responseString,"\\n", "\n");
    }

    private Element XML;

    /**
     * Get the value of XML
     *
     * @return the value of XML
     */
    public Element getXML() {
        return XML;
    }

    /**
     * Set the value of XML
     *
     * @param XML new value of XML
     */
    private void setXML(byte[] bi) {
        InputStream input = new ByteArrayInputStream(bi);
        XMLParser xp = new XMLParser();
        Element doc = xp.parse(new InputStreamReader(input));
       
        this.XML = doc.getChildAt(0).getChildAt(0);
       
        this.setResult(this.getValue("result"));
    }

        private String result;

    /**
     * Get the value of result
     *
     * @return the value of result
     */
    public String getResult() {
        return result;
    }

    /**
     * Set the value of result
     *
     * @param result new value of result
     */
    protected void setResult(String result) {
        this.result = result;
    }

    public WebServices() {
        this.addRequestHeader("SOAPAction", " ");
        this.setContentType("text/xml;charset=UTF-8");
        this.setPost(true);

    }

    @Override
    protected void readResponse(InputStream input) throws IOException {
        byte[] bi = Util.readInputStream(input);
               
        this.setResponseString(new String(bi));
        this.setXML(this.getResponseString().getBytes("UTF-8"));

        this.loadData();
       
        ActionEvent e = new ActionEvent(this);
       
        for (ActionListener l : listeners)
            l.actionPerformed(e);

    }

   
    @Override
    protected void buildRequestBody(OutputStream os) throws IOException {
            String headerOpen = "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" "
                          + "xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" "
                          + "xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" "
                          + "xmlns:q0=\"urn:" + this.getUrn() + "\">"
                          + "<soapenv:Body>";
            String headerClose = "</soapenv:Body></soapenv:Envelope>";
            String requestOpen = "<q0:" + this.getOperation() + ">";
            String requestClose = "</q0:" + this.getOperation() + ">";
           
            String body = headerOpen
                        + requestOpen
                        + this.getInputParams()
                        + requestClose
                        + headerClose;
           
            os.write(body.getBytes("UTF-8"));
    }
 
    public void PostRequest() {
        Boolean err = false;
       
        if (this.getUrl() == null) {
            Dialog.show("Error", "The URL of the web service is not set", "OK", null);
            err = true;
        }
       
        if (this.getUrn() == null) {
            Dialog.show("Error", "The URN of the web service is not set", "OK", null);
            err = true;
        }
       
        if (this.getOperation() == null) {
            Dialog.show("Error", "The operation of the web service is not set", "OK", null);
            err = true;
        }
       
        if (err == false) {
            if (this.waitFor == true) {
                NetworkManager.getInstance().addToQueueAndWait(this);
            } else {
                NetworkManager.getInstance().addToQueue(this);
            }
        }
    }
   
    public String getValue(String tagName){
        String value = "";
        Element el = this.getXML().getFirstChildByTagName(tagName);

        if (el != null && el.getNumChildren() > 0){
            Element text = el.getChildAt(0);
           
            if (text.isTextElement()) {
                value = text.getText();
            }
        }
       
        return value;
    }
}
Thank you

Agada Godwin

unread,
Mar 7, 2013, 3:56:41 PM3/7/13
to codenameone...@googlegroups.com
ought to have told you since, i was able to walk around the code and got mine working. Thanks for the insight and help.


--
--
You received this message because you are subscribed to the Google
Groups "CodenameOne Discussions" group.
 
For more information about Codename One please visit http://www.codenameone.com/
 
To post to this group, send email to
codenameone...@googlegroups.com
To unsubscribe from this group, send email to
codenameone-discu...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/codenameone-discussions?hl=en?hl=en
 
---
You received this message because you are subscribed to a topic in the Google Groups "CodenameOne Discussions" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/codenameone-discussions/QOfW_1o8vDs/unsubscribe?hl=en-US.
To unsubscribe from this group and all its topics, send an email to codenameone-discu...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Freddy Boisseau

unread,
Mar 7, 2013, 3:57:46 PM3/7/13
to codenameone...@googlegroups.com
You are welcome.

allenmu...@gmail.com

unread,
Sep 3, 2015, 1:01:58 PM9/3/15
to CodenameOne Discussions
Godwin, could you show us how you to call your WebServices class from CodenameOne code?

I have an IIS Web Service and I am trying to call it from CodenameOne.

Thanks.

Godwin

unread,
Sep 4, 2015, 2:34:41 AM9/4/15
to CodenameOne Discussions, allenmu...@gmail.com
First, I had to use SOAP UI to load and test the Webservice, then I got the direct URL for the methods from the URL on each method. This is the URL I set in my connection request. After this, I copy the whole XML from each method in SOAP UI and set it as my parameter in buildRequestBody.
Example::::

    public void fetchAllMs() {
       
InputParameter = "<soapenv:Envelope xmlns:soapenv='http://schemas.xmlsoap.org/soap/envelope/' xmlns:tem='http://tempuri.org/'>"
               
+ "<soapenv:Header/>"
               
+ "<soapenv:Body>"
               
+ "<tem:FetchM/>"
               
+ "</soapenv:Body>"
               
+ "</soapenv:Envelope>";

       
final ConnectionRequest request = new ConnectionRequest() {

           
@Override
           
protected void buildRequestBody(OutputStream os) throws IOException {

                os
.write(InputParameter.getBytes("UTF-8"));
           
}
           
// **************** Get the status of the connection        

           
@Override
           
protected void readHeaders(Object connection) throws IOException {
                status
= getHeader(connection, "status");
               
System.out.println("The status of the connection: " + status);

           
}
           

           
@Override
           
protected void readResponse(InputStream input) throws IOException {


                status
= String.valueOf(getResponseCode());

                deResult
= Result.fromContent(input, Result.XML);

               


           
}
       
};

       
final NetworkManager manager = NetworkManager.getInstance();
       
Command cancel = new Command("Cancel") {
           
@Override
           
public void actionPerformed(ActionEvent evt) {
                manager
.killAndWait(request);
               
//do Option2
           
}
       
};

       
InfiniteProgress ip = new InfiniteProgress();
       
Dialog dlg = ip.showInifiniteBlocking();
        dlg
.setBackCommand(cancel);

       
//String url = "https://api.parse.com/1/classes/MFBs";
        request
.setUrl(app_Settings.get("App_URL").toString());
        request
.setContentType("text/xml;charset=UTF-8");

        request
.setFailSilently(true);//stops user from seeing error message on failure
        request
.setPost(true);
        request
.setDuplicateSupported(true);
        request
.setDisposeOnCompletion(dlg);

        manager
.start();

        manager
.addToQueueAndWait(request);
   
}

This is how I was able to get it to work
Message has been deleted

allenmu...@gmail.com

unread,
Sep 6, 2015, 1:17:04 PM9/6/15
to CodenameOne Discussions, allenmu...@gmail.com
Thanks Godwing,

I tested your code but:
I can't get a different status connection than "The status of the connection: null".

I had use SOAP UI to load and test the webservice, but I don't know what I have to use in the url:
  • Have I to use the url ended in "?wsdl" or ended in ".asmx" or ended in "?op=OperationName"?
  • I configured the InputParameter like this:
    • InputParameter = "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:tem=\"http://tempuri.org/\">\n" +
                              "   <soapenv:Header/>\n" +
                              "   <soapenv:Body>\n" +
                              "      <tem:GetCurrency>\n" +
                              "         <!--Optional:-->\n" +
                              "         <tem:currencyCode>CRC</tem:currencyCode>\n" +
                              "      </tem:GetCurrency>\n" +
                              "   </soapenv:Body>\n" +
                              "</soapenv:Envelope>";
    • And my WS url is http://XX.XX.XX.XX/searchandfindws/SearchAndFindData.asmx
      • If you want I would send you the real IP to test in SOAP UI.

I will apreciate any advice.

Godwin

unread,
Sep 7, 2015, 6:38:31 AM9/7/15
to CodenameOne Discussions, allenmu...@gmail.com

Check the attached image; At the top of the page, you will find a blue arrow, use the URL you find there in your SOAP UI. While testing in Codename One, bring up your network monitor, it will help you discover more solutions to your network problem.

suman....@gmail.com

unread,
Feb 23, 2016, 7:24:53 AM2/23/16
to CodenameOne Discussions, allenmu...@gmail.com
Please look into my code ....
 I am unable to consume soap based web services ...

My Code sample:

loginButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
Log.p(" CaterId : "+catererId.getText());
Log.p(" Username : "+loginId.getText());
Log.p(" Password : "+password.getText());
                                
                                final String InputParameter =   "<soap:Envelope xmlns:soap=\"http://www.w3.org/2003/05/soap-envelope\" xmlns:tem=\"http://tempuri.org/\">" 
                                                                +"   <soap:Header/>" 
                                                                +"   <soap:Body>" 
                                                                +"      <tem:AuthenticateSupervisor>" 
                                                                +"         <tem:username>TestAbhi</tem:username>" 
                                                                +"         <tem:password>TestAbhi</tem:password>" 
                                                                +"         <tem:caterer>calihanint</tem:caterer>" 
                                                                +"      </tem:AuthenticateSupervisor>" 
                                                                +"   </soap:Body>" 
                                                                +"</soap:Envelope>";
                                ConnectionRequest r= new ConnectionRequest(){
                                    
                                     @Override
                                    protected void buildRequestBody(OutputStream os) throws IOException {

                                        os.write(InputParameter.getBytes("UTF-8"));
                                        
                                        
                                    }
                                    @Override
                                    protected void postResponse() {
                                    //    super.postResponse(); //To change body of generated methods, choose Tools | Templates.
                                    }
                                    
                                    @Override
                                    protected void readResponse(InputStream input) throws IOException {
                                      //  super.readResponse(input); //To change body of generated methods, choose Tools | Templates.
                                        XMLParser parser = new XMLParser();
                                        
                                         Element elem = parser.parse(new InputStreamReader(input));
                                         Log.p(" Came heer"+elem);
                                    }
                                   
                                };
                                r.setUrl("http://192.168.10.224:8888/CXPPostScheduleService/Service.asmx?op=");
                                r.setPost(false);
                                /*r.addArgument("username", "TestAbhi");
                                r.addArgument("password", "TestAbhi");
                                r.addArgument("caterer", "calihanint");*/
                                r.setContentType("application/soap+xml;charset=UTF-8");
                                NetworkManager.getInstance().addToQueueAndWait(r);
                                r.getResponseData();
                        }
        });
    }

  This is the code I wrote to connect .Net based web services. Unfortunately I will get response as WSDL file description in html format.

Thanks in advance.

suman....@gmail.com

unread,
Feb 23, 2016, 7:26:26 AM2/23/16
to CodenameOne Discussions, allenmu...@gmail.com, suman....@gmail.com

Please find extra "?op" on url ... Sorry for typo
                                r.setUrl("http://192.168.10.224:8888/CXPPostScheduleService/Service.asmx");

Shai Almog

unread,
Feb 23, 2016, 11:02:35 PM2/23/16
to CodenameOne Discussions, allenmu...@gmail.com, suman....@gmail.com
Reply all
Reply to author
Forward
0 new messages