Http://sunwei-07.iteye.com/blog/1139814

Source: Internet
Author: User

Recently I learned how to access WebService in Android. Record the process

1. Set up the simulator to access the Internet
In Windows, After configuring the adroid environment variable (such as adding D: \ android-sdk-windows-1.0_r1 \ tools to the system variable path), enter in the command line window:

Emulator

After the android simulator is started, enter:

ADB Shell

Enter the ADB shell mode:

Write the network connection proxy settings to the configuration database. If your Internet proxy IP address is 10.193.xx.xx:
Sqlite3
/Data/COM. Android. providers. Settings/databases/settings. DB "insert
Into system values (99, 'HTTP _ proxy', '10. 193. xx. xx: 1080 ')"

Check whether the system settings have been successfully changed:
Sqlite3
/Data/COM. Android. providers. Settings/databases/settings. DB "select *
From System"

Note the following points. If you cannot get online, check whether the DNS settings are correct.
Getprop: Check whether net. dns1 is correct. If it is set to PC, it will be OK.
Setprop net. dns1 XXX. The specific value depends on your PC.

2. Publish webserice (I added a simple digital addition/subtraction service to Tomcat through axis2)

1. Download the war file of axis2 and throw it to Tomcat webapps.
2. Write service class
Package Rong. Service;

/***/
/**
* Calculator operation
*
* @ Author rongxinhua
*
*/
Public class calculateservice {

/***/
/**
* Addition operation
*
* @ Param x
* Add
* @ Param y
* Addend
* @ Return X and Y's sum
*/
Public float plus (float X, float y ){
Return X + Y;
}

/***/
/**
* Subtraction
*
* @ Param x
* Subtrahend
* @ Param y
* Subtrahend
* @ Return X and Y
*/
Public float minus (float X, float y ){
Return x-y;
}

/***/
/**
* Multiplication
*
* @ Param x
* Multiplier
* @ Param y
* Multiplier
* @ Return the product of X and Y
*/
Public float multiply (float X, float y ){
Return x * Y;
}

/***/
/**
* Division operation
*
* @ Param x
* Divisor
* @ Param y
* Divisor
* @ Return X and Y
*/
Public float divide (float X, float y ){
Return x/y;
}

}

3.Package the service arr file and drop it to the WEB-INF \ service \ of axis2

For example: (E: \ J2EE \ tomcat6 \ webapps \ axis2 \ WEB-INF \ Services \ calculateservice. AAR)

4.For Android, write an interface and simply display the value returned by WebService when a textview is required.

Package com. suncsdn;

Import java. Io. ioexception;

Import org. ksoap2.soapenvelope;
Import org. ksoap2.serialization. soapobject;
Import org. ksoap2.serialization. soapserializationenvelope;
Import org. ksoap2.transport. httptransportse;
Import org. xmlpull. v1.xmlpullparserexception;

Import Android. App. activity;
Import Android. OS. Bundle;
Import Android. widget. textview;

Public class Hello extends activity {
/** Called when the activity is first created .*/
@ Override
Public void oncreate (bundle savedinstancestate ){
Super. oncreate (savedinstancestate );
Setcontentview (R. layout. Main );

Textview t = (textview) findviewbyid (R. Id. text1 );

String MSG = getservicestr ();

T. settext (MSG = NULL? "No value returned": MSG );
}

Private string getservicestr (){

// WebService namespace
String namespace = "http://service.rong ";
// Server publishing address
String url = "http: // 192.168.1.121: 8080/axis2/services/calculateservice ";
// Function name
String methodname = "plus ";
// Return Value
String xmlmessage = NULL;

Try {
// Create an httptransportse object. You can specify the URL of the WebService by using the HTTP transportse class constructor.
Httptransportse transport = new httptransportse (URL );

Transport. DEBUG = true;

// Specify the WebService namespace and function name
Soapobject = new soapobject (namespace, methodname );

// Set the value of the call method parameter
Soapobject. addproperty ("X", 1 );
Soapobject. addproperty ("Y", 1 );

Soapserializationenvelope envelope = new soapserializationenvelope (
Soapenvelope. ver10 );
Envelope. bodyout = transport;
Envelope. setoutputsoapobject (soapobject );

// Call the WebService method using the call Method
Transport. Call (null, envelope );

Soapobject sb = (soapobject) envelope. bodyin;
// Obtain the XML string returned from the server
Xmlmessage = sb. tostring ();

} Catch (ioexception e ){
// Todo auto-generated Catch Block
E. printstacktrace ();
} Catch (xmlpullparserexception e ){
// Todo auto-generated Catch Block
E. printstacktrace ();
}

Return xmlmessage;
}
}

To facilitate the direct attaching of the AAR File

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.