Options

Use Dynamic NAV web services with Android (Java)

elbartoelbarto Member Posts: 3
edited 2012-06-13 in NAV Three Tier
Hello guys,

since a few days I try to use web services with the Android SDK. Normally it should not be a big problem, because Android supports SOAP and I never had any problem to use web services from webpages. But NAV makes problems.
I haven't got any problems to establish a connection to the webservice, it seems that the request doesn't work...

Take a look:
String great = "";
String namespace = "urn:microsoft-dynamics-schemas/codeunit/CU_Vendor";
String url = "http://mch:7047/DynamicsNAV/WS/CRONUS%20International%20Ltd./Codeunit/CU_Vendor";	
String soap_action = "urn:microsoft-dynamics-schemas/Codeunit/CU_Vendor:Getvendor";
String method_name = "Getvendor";
	    
try
{
		SoapObject request = new SoapObject(namespace, method_name);    
		SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
		        
		 envelope.dotNet = true;   
		 envelope.setOutputSoapObject(request);   
		 HttpTransportSE transport = new HttpTransportSE(url);    
		 transport.call(soap_action, envelope); // Receive Error here!
	         SoapObject result = (SoapObject) envelope.getResponse();
	         great = result.toString();
}
catch (Exception e)
{
			great = e.toString();
}
return great; // Out there is a DialogAlert which manages output.

I get following Errormessage:
org.xmlpull.v1.xmlpullparserexception:
unexpected type (position: END_DOCUMENT null@1:0 in
Java.io.inputstreamreader@44c4d2f0)


The Codeunit "CU_Vendor" has got "Getvendor" a function which returns the name of the first vendor.
I use the ksoap2-Library.
The same code works perfectly with web services from webpages.

Has anybody a idea to solve my problem?

Greetings:

El Barto

Comments

  • Options
    ara3nara3n Member Posts: 9,255
    I only this link about connecting from java language to nav webservice. I'm guessing you've already enabled NTLM authentication in config file.

    http://blogs.msdn.com/b/freddyk/archive ... -java.aspx
    Ahmed Rashed Amini
    Independent Consultant/Developer


    blog: https://dynamicsuser.net/nav/b/ara3n
  • Options
    elbartoelbarto Member Posts: 3
    Yes, it's already done.
    I think "transport" is not able to handle the xml-response of SOAP-Action.

    Very strange. Sadly Android doesn't support xml.bind completely...
    According to my knowledge and my great web research, I have to use ksoap2-Libraries.
  • Options
    zasarafljivaczasarafljivac Member Posts: 2
    Hello everyone,

    I have successfully used service from Java application (thanks to freddyk blog). What I am trying to do now, is to use ksoap2 library to access web service from android, but I am getting the same error message like elbarto.

    Did anyone solve this problem, or does anybody have simple working example of “how to connect NAV web service from android”, that is willing to share?

    Here is my code:
    import android.app.Activity;
    import android.os.Bundle;
    import android.util.Log;
    import android.widget.TextView;
    import org.ksoap2.SoapEnvelope;
    import org.ksoap2.serialization.PropertyInfo;
    import org.ksoap2.serialization.SoapObject;
    import org.ksoap2.serialization.SoapPrimitive;
    import org.ksoap2.serialization.SoapSerializationEnvelope;
    import org.ksoap2.transport.HttpTransportSE;
    
    public class ProjekatActivity extends Activity {
    
    	private final String NAMESPACE = "urn:microsoft-dynamics-schemas/page/customer/";
    	private final String URL = "http://xxx.xxx.xxx.xxx:7047/DynamicsNAV/WS/CRONUS%20Hrvatska%20d.o.o/Page/Customer";   
    	private final String SOAP_ACTION = "http://xxx.xxx.xxx.xxx:7047/DynamicsNAV/WS/CRONUS%20Hrvatska%20d.o.o/Page/Customer:Read";
    	private final String METHOD_NAME = "Read";
    
        
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
            
            SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME); 
            TextView tv = new TextView(this);
    
            String custID = "10000";
            PropertyInfo custProp = new PropertyInfo();
            custProp.setName("No");
            custProp.setValue(custID);
            custProp.setType(String.class);
            request.addProperty(custProp);
            
            SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
            envelope.dotNet = true;
            envelope.setOutputSoapObject(request);
            HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
     
            try {
                androidHttpTransport.call(SOAP_ACTION, envelope);
                SoapPrimitive response = (SoapPrimitive)envelope.getResponse();
                tv.setText(response.toString());
                setContentView(tv); 
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
    
    Where xxx.xxx.xxx.xxx is my local ip address.

    In the AndroidManifest.xml I added a line
    <uses-permission android:name="android.permission.INTERNET" />
    
    Customer page is published through web service form in classic client.

    Application fails on this line of code:
    androidHttpTransport.call(SOAP_ACTION, envelope);
    

    Any help would be appreciated.
  • Options
    ashishmlsashishmls Member Posts: 2
    public String namespace = "urn:microsoft-dynamics-schemas/page/testcard";
    public String url = "http://11.112.111.121:7047/DynamicsNAV/WS/andriod/Page/TestCard?wsdl";
    public String soap_action = "urn:microsoft-dynamics-schemas/page/testcard:Read";
    public String method_name = "Read";

    protected Void doInBackground(Void... params) {
    try {
    SoapObject request = new SoapObject(namespace, method_name);
    //request.addProperty("Username","testuser");
    //request.addProperty("Password","test@123");
    SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
    envelope.dotNet = true;
    envelope.setOutputSoapObject(request);

    NTLMTransport ntlm = new NTLMTransport();
    ntlm.debug = true;
    ntlm.setCredentials(url, "testuser", "test@123", "android", "");

    ntlm.call(soap_action, envelope); // Receive Error here!
    //SoapObject result = (SoapObject) envelope.bodyIn;
    SoapPrimitive result1 = (SoapPrimitive) envelope.getResponse();

    //great = response.toString();
    grt11 = result1.toString();
    //System.out.println(great);
    System.out.println(grt11);

    } catch (Exception e) {
    e.printStackTrace();
    //great = e.toString();
    //System.out.println(great);
    grt11 = e.toString();
    System.out.println(grt11);
    }
    return null;
    }


    Got Error:
    Attempt to invoke virtual method 'java.lang.String org.ksoap2.serialization.SoapPrimitive.toString()' on a null object reference
    Any help would be greatly appreciated.
  • Options
    zohaibu95@hotmail.comzohaibu95@hotmail.com Member Posts: 223
    What if you make an intermediate layer, a web service which gets the SOAP response from NAV and convert that SOAP response in JSON/XML and the android get the data from that web service in either JSON or XML, as android is good in parsing json / xml response.
    Best Regards
    Zohaib Ahmed
    Dynamics NAV ERP Technical Consultant.

    please like / agree / verify my answer, if it was helpful for you. thanks.
Sign In or Register to comment.