Android calls WebService compiled by. net

Source: Internet
Author: User
Tags dotnet

From: http://blog.csdn.net/guochunyang/article/details/6203232

The jsr172 specification is used to call WebService. It is easy to use the jsr172 specification of j2_m2. it is used to generate stub and add it to the Project for calling. After debugging for half a day, it ends up failing, you can only find other components. If you use httpconnection to connect to obtain data, there will be too many encapsulated items, which is not cost-effective. Finally, I found ksoap2 and found N examples on the Internet. The examples are basically copied from each other, and none of them can be used. It is really depressing. But fortunately, at least I learned a line of thought and studied it slowly, and finally succeeded. The following is an attachment.CodeAnd description:

Attachment: ksoap2 component used in developmentKsoap2-android-assembly-2.5.2-jar-with-dependencies.jar, You can click the link to download;

1.ProgramTo connect to the network, you must add the following sentence to the androidmanifest. xml file:

<! -- This item must be set to connect to the network -->
<Uses-Permission Android: Name = "android. Permission. Internet"/>

Otherwise, an exception with no permissions will be reported;

2. Use DOTNET to write webservcie. Pay attention to the RPC mode. (1) Use System in your WebService class first. web. services. protocols. specify the attributes of soaprpcserviceattribute; (2) Specify the attributes of system in a specific WebService method. web. services. protocols. soaprpcmethodattribute attributes;

For example:

[WebService (namespace ="Http://tempuri.org/")] //Specify the WebService namespace

[Soaprpcservice] // specify the RPC Method
Public class default: system. Web. Services. WebService
{

[Soaprpcmethod, webmethod] // specify the RPC method in the specific method.
Public user helloworld (User user)
{
User US = new user ();
Us. Name = "hello" + User. Name;
Us. Age = user. Age;
Return us;
}
}

// Custom object type in DOTNET

Public class user
{
Public string name {Get; set ;}
Public int age {Get; set ;}
}

 

--------------------------------

3. The following is the call method in Android

To transmit custom types in WebService, you must inherit the kvmserializable interface.

Public class user implements kvmserializable {

Private string name = NULL;
Private int age = 0;
 
@ Override
Public object getproperty (INT arg0 ){
// Todo auto-generated method stub
Object res = NULL;
Switch (arg0 ){
Case 0:
Res = This. Name;
Break;
Case 1:
Res = This. Age;
Break;
Default:
Break;
}
Return res;
}

@ Override
Public int getpropertycount (){
// Todo auto-generated method stub
Return 2;
}

@ override
Public void getpropertyinfo (INT arg0, hashtable arg1, propertyinfo arg2) {
// todo auto-generated method stub
switch (arg0) {
case 0:
arg2.type = propertyinfo. string_class;
arg2.name = "name";
break;
case 1:
arg2.type = propertyinfo. integer_class;
arg2.name = "Age";
break;
default:
break;
}< BR >}

@ Override
Public void setproperty (INT arg0, object arg1 ){
// Todo auto-generated method stub
If (arg1 = NULL) return;
Switch (arg0 ){
Case 0:
This. Name = arg1.tostring ();
Break;
Case 1:
This. Age = integer. valueof (arg1.tostring ());
Break;
Default:
Break;
}
}
}

 

4,

// Call the specific WebService Method

Public String sayhello (){
String namespace ="Http://tempuri.org/";
String methodname = "helloworld ";
String soapaction ="Http://tempuri.org/HelloWorld";

String url ="Http: // 192.168.2.51/Default. asmx? WSDL ";// Which one does not add after? The WSDL parameter has little impact.

// Create a WebService connection object
Org. ksoap2.transport. httptransportse transport = new httptransportse (URL );
Transport. DEBUG = true; // whether the debug mode is used

// Set connection Parameters
Soapobject = new soapobject (namespace, methodname );
User user = new user ();
User. setproperty (0, "Zhi ");
User. setproperty (1, 18 );
Propertyinfo Pi = new propertyinfo ();
Pi. setname ("user"); // Parameter Name of the WebService interface, which must be the same as the name exposed by WebService in DOTNET.
Pi. setvalue (User );
Pi. settype (user. getclass ());
Soapobject. addproperty (PI); // Add custom parameters to the request object

// Set return parameters
Soapserializationenvelope envelope = new soapserializationenvelope (soapenvelope. ver11); // the SOAP Protocol version must use soapenvelope. ver11 (soap V1.1)
Envelope. DOTNET = false; // Note: This attribute is supported by the dotnetwebservice protocol. If DOTNET WebService does not specify the RPC method, use True. Otherwise, use false.
Envelope. bodyout = transport;
Envelope. setoutputsoapobject (soapobject); // you can specify the request parameters.
Envelope. addmapping (namespace, "user", user. getclass (); // required when uploading objects. The namespace parameter is specified in WebService. The name is the name of the server type, and the claszz parameter is the type of the custom class.


Try {
Transport. Call (soapaction, envelope );
Soapobject sb = (soapobject) envelope. bodyin; // The object returned by the server exists in the bodyin of envelope.
User US = (User) envelope. getresponse (); // directly convert the returned value to a known object
Return us. getname () + us. getage ();
} Catch (ioexception e ){
// Todo auto-generated Catch Block
E. printstacktrace ();
} Catch (xmlpullparserexception e ){
// Todo auto-generated Catch Block
E. printstacktrace ();
} Catch (exception ex ){
Ex. printstacktrace ();
}
Return "";
}

Related Article

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.