SoFunction
Updated on 2025-04-07

Example of Android development method calling WebService

This article describes the method of Android development calling WebService. Share it for your reference, as follows:

WebService is a remote call standard based on the SOAP protocol. Through webservice, different operating system platforms, different languages, and different technologies can be integrated into one. There is no library for calling WebService in the Android SDK, so it is necessary to use a third-party SDK to call WebService. The PC version of WEbservice client library is very rich, such as Axis2, CXF, etc., but these development packages are too large for the Android system and may not be easy to port to the Android system. Therefore, these development packages are not within our consideration. There are some SDKs suitable for mobile WebService clients, and the most commonly used ones are Ksoap2, which can be used from/p/ksoap2-android/downloads/listDownload; copy the downloaded ksoap2-android-assembly-2. package to the lib directory of the Eclipse project, and of course it can also be placed in other directories. Also reference this jar package in the Eclipse project.

The specific method of calling the webservice is:

(1) Specify the namespace of the webservice and the method name called, such as:

SoapObject request =new SoapObject(http://service,"getName");

The first parameter of the SoapObject class represents the namespace of the WebService, and the namespace of the WebService can be found from the WSDL document. The second parameter indicates the name of the WebService method to be called.

(2) Set the parameter value of the calling method. If there are no parameters, it can be omitted. The code for setting the parameter value of the method is as follows:

("param1","value");
("param2","value");

It should be noted that although the first parameter of the addProperty method represents the parameter name of the calling method, the parameter value does not necessarily correspond to the method parameter name in the WebService class on the server, as long as the order of the parameters is set is consistent.

(3) Generate SOAP request information that calls the Webservice method. This information is described by the SoapSerializationEnvelope object, and the code is:

SoapSerializationEnvelope envelope=new
SoapSerializationEnvelope(SoapEnvelope.VER11);
 = request;

When creating a SoapSerializationEnvelope object, you need to set the version number of the SOAP protocol through the construction method of the SoapSerializationEnvelope class. This version number needs to be set according to the version number of the server WebService. After creating the SoapSerializationEnvelope object, don't forget to set the bodyOut property of the SOAPSoapSerializationEnvelope class. The value of this property is the SoapObject object created in the first step.

(4) Create an HttpTransportsSE object. Through the HttpTransportsSE class constructor, you can specify the URL of the WSDL document of WebService:

HttpTransportSE ht=new HttpTransportSE("http://192.168.18.17:80
/axis2/service/SearchNewsService?wsdl");

(5) Use the call method to call the WebService method, code:

(null,envelope);

The first parameter of the Call method is generally null, and the second parameter is the SoapSerializationEnvelope object created in step 3.

(6) Use the getResponse method to get the return result of the WebService method, code:

SoapObject soapObject =( SoapObject) ();

The following is a simple example of implementing a weather viewing function:

public class WebService extends Activity {
private static final String NAMESPACE = "/";
// WebService addressprivate static String URL = "/
webservices/";
private static final String METHOD_NAME = "getWeatherbyCityName";
private static String SOAP_ACTION = "/
getWeatherbyCityName";
private String weatherToday;
private Button okButton;
private SoapObject detail;
@Override
public void onCreate(Bundle savedInstanceState) {
 (savedInstanceState);
 setContentView();
 okButton = (Button) findViewById();
 (new () {
 public void onClick(View v) {
 showWeather();
 }
 });
}
private void showWeather() {
 String city = "Wuhan";
 getWeather(city);
}
@SuppressWarnings("deprecation")
public void getWeather(String cityName) {
try {
 ("rpc------");
 SoapObject rpc = new SoapObject(NAMESPACE, METHOD_NAME);
 ("rpc" + rpc);
 ("cityName is " + cityName);
 ("theCityName", cityName);
 AndroidHttpTransport ht = new AndroidHttpTransport(URL);
  = true;
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
 SoapEnvelope.VER11);
  = rpc;
  = true;
 (rpc);
 (SOAP_ACTION, envelope);
 SoapObject result = (SoapObject) ;
 detail = (SoapObject) result
 .getProperty("getWeatherbyCityNameResult");
 ("result" + result);
 ("detail" + detail);
 (, (),
 Toast.LENGTH_LONG).show();
 parseWeather(detail);
 return;
} catch (Exception e) {
 ();
 }
}
private void parseWeather(SoapObject detail)
 throws UnsupportedEncodingException {
 String date = (6).toString();
 weatherToday = "today:" + (" ")[0];
 weatherToday = weatherToday + "\nWeather:" + (" ")[1];
 weatherToday = weatherToday + "\nTemperature:"
 + (5).toString();
 weatherToday = weatherToday + "\nWind:"
 + (7).toString() + "\n";
 ("weatherToday is " + weatherToday);
 (, weatherToday,
 Toast.LENGTH_LONG).show();
 }
}

For more information about Android related content, please check out the topic of this site:Summary of the usage of basic Android components》、《Android View View Tips Summary》、《Android resource operation skills summary》、《Summary of Android operating json format data skills》、《Android development introduction and advanced tutorial》、《Android programming activity operation skills summary"and"Android control usage summary

I hope this article will be helpful to everyone's Android programming design.