Android based KSOAP2 call WCF Service (ii): Android article

Source: Internet
Author: User
Tags dotnet soap wsdl

The previous article completed a WCF service through a very simple example. Next, let's look at the Android side.

The Android side calls WCF, which is implemented based on the KSOAP2 package.

(This requires fan Qiang access, you know): Https://code.google.com/p/ksoap2-android/

I use the 3.3.0 version, I upload to csdn, the above address if not accessible, you can use this address: http://download.csdn.net/detail/cnryc/7695437

Source code for the program: http://download.csdn.net/detail/cnryc/7694983

1. Create a new Android Project,

Very simple, just one activity.

Increase access to the network within the Androidmanifest.xml.

<uses-permission android:name= "Android.permission.INTERNET"/>

Copy the Ksoap2-android-assembly-3.3.0-jar-with-dependencies.jar file to the Libs folder of the Android project,

Go back to eclipse, Refresh libs, click KSOAP2 Package in the Project right button, select Build Path>add to build Path, OK.

2. Add a button to the Activity_main.xml layout file:

1 <Relativelayoutxmlns:android= "Http://schemas.android.com/apk/res/android"2 Xmlns:tools= "Http://schemas.android.com/tools"3 Android:layout_width= "Match_parent"4 Android:layout_height= "Match_parent"5 Android:paddingbottom= "@dimen/activity_vertical_margin"6 Android:paddingleft= "@dimen/activity_horizontal_margin"7 Android:paddingright= "@dimen/activity_horizontal_margin"8 Android:paddingtop= "@dimen/activity_vertical_margin"9 Tools:context= "Com.imoonstal.invokewcf.MainActivity" >Ten  One     <TextView A         Android:id= "@+id/tv" - Android:layout_width= "Wrap_content" - Android:layout_height= "Wrap_content" the Android:text= "@string/hello_world" /> -     <Button -         Android:layout_width= "Wrap_content" - Android:layout_height= "Wrap_content" + Android:layout_below= "@id/tv" - Android:text= "Invoke" + Android:onclick= "Btnclick" /> A  at </Relativelayout>

3, add the Btnclick method of the button in Mainactivity.java and an inner class that implements Runnable interface.

It seems that Android does not allow the main thread to access the network from the 3.0 SDK, so a thread is started to access the WCF service.

   Public void Btnclick (View v) {        new  Thread (runnable). Start ();    }         New Runnable () {        @Override        publicvoid  run () {                        callwebservice ();        }            };

Then increase the four basic information required to invoke a WCF service:

    Static String NameSpace = "http://tempuri.org/";     Static String url= "http://10.0.2.2:8733/WCFLibrary/TestService/";     Static String soap_action= "http://tempuri.org/ITestService/GetString";     Static String methodname= "GetString";

Namespace is a namespace that can be specified in a WCF program and, if not specified, the default is http://tempuri.org/

The URL is the endpoint inside the service, which is in the configuration file in the previous article. It is important to note that the native address of Android is not 127.0.0.1 but 10.0.2.2

SOAPAction is the name of the namespace plus the method

MethodName is the name of the method that needs to be called.

The contents can be found in the WSDL address. Dot below? The WSDL file gets the contents of the WSDL:

Click on the top of the connection to get a WSDL file, as follows:

The above four basic information can be found here.

Then complete the Callwebservice () method:

 Public voidCallwebservice () {//Specifies the namespace and method name of the callSoapobject request =NewSoapobject (NameSpace, MethodName); //set parameters to be passed to the calling interfaceRequest.addproperty ("Value", "Imoonstal"); //generates SOAP request information for the calling method and specifies the version of SoapSoapserializationenvelope envelope =NewSoapserializationenvelope (SOAPENVELOPE.VER10); //The following two sentences are the same function, write a sentence on the lineEnvelope.bodyout =request;                Envelope.setoutputsoapobject (Request); //sets whether to invoke the dotnet developed WebServiceEnvelope.dotnet =true; Httptransportse Transport=NewHttptransportse (URL); Try {              //calledTransport.call (soap_action, envelope); } Catch(Exception e) {e.printstacktrace (); }            //get the returned dataSoapobject Object =(Soapobject) Envelope.bodyin; if(NULL==object) {            return; }        //gets the returned resultString result = Object.getproperty (0). toString ();    SYSTEM.OUT.PRINTLN (result); }

Run, Show results:

Gets the result that the call succeeded.

Summarize:

1. Android calls WCF, which is actually called WCF-published WebService, which is the WSDL address, so it is also possible to call other webservice.

2, the WebService four basic information are: the namespace, the calling method name, the endpoint and the soap Action.

When you access the WSDL in the browser, you can get the namespace, the method name of the call, as described above.

The URL, or endpoint, can be obtained from the WCF configuration file, or the WSDL address is stripped of the "? wsdl" at the end.

The SOAP action is typically the name of the method called by the namespace +.

3, pass the parameter, must follow the method in the parameter order to pass in " parameter name, parameter value ".

4, Version number: can be obtained from the WSDL address,xmlns:soap12= "http://schemas.xmlsoap.org/wsdl/soap12/" indicates that the version is 12.

But actually I was in the process of debugging the code to find out if the above code

Soapserializationenvelope envelope = new Soapserializationenvelope (SOAPENVELOPE.VER10);

In the VER10 changed to VER12 will be an error message:HTTP request failed HTTP status 415

The specific reason I also did not find, tomorrow continue to find, found again to add, have master words please tell me the reason, [email protected].

and WCF binding way to use BasicHttpBinding instead of wshttpbinding, online Some people say also will report 415 error, I have not tried.

5. Address and Permissions:

Android:name= "Android.permission.INTERNET" must write to androidmanifest.xml, otherwise debugging half a day found this problem is depressed, I wrote the program often made this mistake, haha.

Native IP address of Android: 10.0.2.2 instead of 127.0.0.1 or localhost, this cannot be accessed locally.

6, please use multi-threaded asynchronous access to the network, 3.0 start Android forced asynchronous access. Even if Google does not have a mandatory requirement, it should operate asynchronously.

  

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.