Introduction to WebService for Android Development

Source: Internet
Author: User

Some netizens often ask: "How to call WebService on the Android platform "? After communication, I found that some friends even asked how to use webserivce, not to mention the "Mars" terms such as soap and WSDL related to WebService. So, I just wantBefore explaining how to call webserivce on the Android platform, let's first introduce WebServiceTo see how mysterious it is.
I remember that my master thesis included the word "Web Service". At that time, I spent a lot of time studying the application of Web Service in system integration and enterprise application integration. At work, the Web Service is used for all the projects involved. Now, I have to spend some time learning Android, and I am also a web service. It seems that Web services are everywhere and can always be found in programming languages (Note: if you have never touched WebService before, You Need To Know That WebService is not a patent of Android, ).

According to W3C definition, Web Services is a software system used to support interoperability between different machines in the network. It is a self-contained, self-described, and modular application, it can be described, published, and called in the network, and can be viewed as a network-based, distributed modular component.
Web services are built on general protocols, such as HTTP, soap, UDDI, and WSDL. These protocols have no preference in the selection of operating systems, programming languages, and object models, therefore, it has a strong vitality.
The advantage of web services is that it provides interoperability between different application platforms, which makes the combination of component-based development and web best. It is based on the HTTP protocol. Call requests and response messages can both pass through the firewall without changing the firewall settings. This avoids the problem that the firewall cannot be crossed when special ports are used for communication.

Simple understanding:Generally, WebService is a remote server that discloses a service, or is understood as a function or method, we can call the service through programming to obtain the information we need. For example, www.webxml.com.cn has published a mobile phone number for the local location query service. You only need to input a mobile phone number segment (number) when calling this service to obtain the region information of this segment immediately.
A more general understanding:By using WebService, we can call methods on the remote server just like calling local methods. We don't need to worry about whether the remote method is written in Java or in PHP or C #. We don't need to worry about whether the remote method is based on Unix or windows, that is to say, WebService has nothing to do with the platform and language.

When it comes to webserivce, you must know soap and WSDL. What is the relationship between them and webserice? As mentioned above, Web services are built on common protocols such as HTTP, soap, and WSDL.
Simple Object Access Protocol (SOAP) is a lightweight, simple, XML-based protocol, it is designed to exchange simple protocols for formatting and solidification of information in a distributed environment. That is to say, for communication and data access and transmission, it must rely on a certain protocol, and soap is a protocol that is relied on in WebService communication. Currently, the commonly used SOAP protocol has two versions: Soap 1.1 and soap 1.2.
Web Services Description Language (WSDL) is an XML language used to describe Web services. It describes the functions, interfaces, parameters, and returned values of Web Services, this allows you to easily bind and call services. It defines operations and messages related to a given web service call and response in a language-independent manner.
WSDL is what we can see. It is an XML document used to describe all aspects of a webserivce. For example, I mentioned that the www.webxml.com.cn website provides a webserivce for querying mobile phone numbers in the same region. How can we use this webserivce? Which version of the SOAP protocol is it based on? What parameters need to be passed in to call it? What value does it return? Is it a string or an XML document? All these questions can be answered in WSDL. The WSDL address of the above service is: http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx? Access IT in the browser. You will see the following XML document:

Android is developed in C # as follows:

:

(Note: The interface is simple, and some mobile phone numbers are erased)

Detailed implementation:

1. Create an android project.

2. Add web reference

3. Open the resource file resources -- layout -- Main. axml

Add controls

The Code is as follows:

<? XML version = "1.0" encoding = "UTF-8"?> <Linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android" Android: Orientation = "vertical" Android: layout_width = "fill_parent" Android: layout_height = "fill_parent" Android: minwidth = "25px" Android: minheight = "25px"> <edittext Android: Id = "@ + ID/txttel" Android: layout_width = "166.0dp" Android: layout_height = "40.0dp" Android: phonenumber = "true" Android: gravity = "TOP" Android: hint = "Enter your mobile phone number"/> <button Android: text = "" Android: layout_width = "162.7dp" Android: layout_height = "wrap_content" Android: Id = "@ + ID/btncx" Android: layout_marginright = "0.0dp"/> <textview Android: TEXT = "" Android: layout_width = "284.7dp" Android: layout_height = "83.3dp" Android: Id = "@ + ID/txtjg"/> </linearlayout>

4. Enable activity1.cs

using System;using Android.App;using Android.Content;using Android.Runtime;using Android.Views;using Android.Widget;using Android.OS;namespace Atel{    [Activity(Label = "Atel", MainLauncher = true, Icon = "@drawable/icon")]    public class Activity1 : Activity    {        int count = 1;        protected override void OnCreate(Bundle bundle)        {            base.OnCreate(bundle);            // Set our view from the "main" layout resource            SetContentView(Resource.Layout.Main);            // Get our button from the layout resource,            // and attach an event to it            Button button = FindViewById<Button>(Resource.Id.btncx);            EditText txttel = FindViewById<EditText>(Resource.Id.txttel);            TextView tjg = FindViewById<TextView>(Resource.Id.txtjg);            button.Click += delegate {                 //button.Text = string.Format("{0} clicks!", count++);                 try                {                    cn.com.webxml.webservice.MobileCodeWS motel = new cn.com.webxml.webservice.MobileCodeWS();                    string strs = motel.getMobileCodeInfo(txttel.Text.ToString(), "");                    tjg.Text = strs;                }                catch (Exception ex)                {                                    }            };        }    }}

Run the simulator and check the effect.

 

 

-- Info --------------------

Interface http://my.oschina.net/simaben/blog/109806 for other train queries

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.