Easy Tutorial
❮ Inner Lambda Final Bubble Sort ❯

7.4 Android Calling WebService

Category Android Basic Tutorial

Introduction to This Section:

>

After the previous study, data requests, data parsing, file uploads and downloads, etc., should meet everyone's basic needs for interaction with the server. What this section introduces to you is the Android call to WebService, which is actually a bit similar to some data platforms that provide us with raw data API services, such as JuHe Data! WebService uses XML and SOAP, and can complete interactions with remote machines through the HTTP protocol! Well, let's not say much, let's start with the content of this section~


1. Introduction to WebService

PS: If you are still not very clear after reading the introduction above, then forget it, the previous company used C# to build a WebService! In this section, we do not discuss how to build a WebService, we just know how to obtain the services provided by WebService, and then parse the returned XML data, and then display the relevant data on our Android devices!


2. Where to Get WebService Services

There are many websites on the Internet that provide WebService. First, find these websites and then obtain the corresponding services! Here, WebXml and YunJu 36wu are selected as examples to explain to everyone, their official websites are:

webXml: http://www.webxml.com.cn/zh_cn/index.aspx

>

It used to be free, but it has become commercialized, and many services require payment, but you can try it for free~

This site provides 16 different Web services, and you can query the corresponding services according to your needs and call different interfaces!

Related pages of webXml:

Related usage instructions:

YunJu 36wu: http://www.36wu.com/Service

>

It also provides a lot of services, and many mobile apps use the interfaces here, such as Rainbow Bus, Mobile Weather, etc. However, this also requires payment =-=, you can try it for free, but you can only send 20 requests within an hour; Click to apply for use, get the key! You can choose either one!


3. Preparation of Third-Party jar Packages

>

First, if you want to call WebService on the Android platform, you need to rely on third-party class libraries: ksoap2 And on the Android platform, ksoap2 Android is used, an efficient, lightweight SOAP development package!

Download address of jar package: https://code.google.com/p/ksoap2-android/wiki/HowToUse?tm=2

It may not be possible to access in the celestial empire, here are two Baidu Cloud links for everyone to download and use:

Version 2.54: ksoap2-android 2.54.jar

Version 3.30: ksoap2-android 3.30.jar

If you happen to be able to access the download address of the jar package, then you will see the following interface:


4. Obtain Some Relevant Parameters

>

First, find the service we need to obtain, and then record the relevant parameters: NameSpace (namespace), SoapAction and URL need not be said, other parameters are found in this way:

For example, we are looking for the weather query parameters here, and we can see such a parameter document when we click in:

For example, here we need the weather query part of the function:

First copy the enclosed SoapAction and NameSpace! Of course, we can test on this page, in addition, we are free users, id can be left blank and skipped directly, after input, click the call button to open such a page:

Hehe, this is the returned XML, and what we need to do is to parse such an XML, and the .gif here represents the weather icon!

Similarly, let's take a look at the SoapAction, NameSpace, and related parameters of the place of ownership query mark!

And the returned XML data:


5. Register and Enable Related WEB Services

>

Click on my Web server, and then click on the trial, WebXML provides us with a five-day free trial, We turn on the two servers This is an English translation of the provided Chinese text. Please find the translation below without any additional explanations or text.

soapObject.addProperty("theUserID", "dbdf1580476240458784992289892b87");
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.bodyOut = soapObject;
envelope.dotNet = true;
envelope.setOutputSoapObject(soapObject);
HttpTransportSE httpTransportSE = new HttpTransportSE(Weatherurl);
System.out.println("Weather service is set up, ready to start the service");
try {
    httpTransportSE.call(WeathersoapAction, envelope);
    //System.out.println("Successfully called the WebService");
} catch (Exception e) {
    e.printStackTrace();
    //System.out.println("Failed to call the WebService");
}

//Get the data returned by the service and start parsing
SoapObject object = (SoapObject) envelope.bodyIn;
System.out.println("Data obtained from the service");
result = object.getProperty(1).toString();
handler.sendEmptyMessage(0x001);
System.out.println("Message sent, the TextView will display the weather information");
//Define a method to get the phone number's location:
public void getland() {
    result = "";
    SoapObject soapObject = new SoapObject(AddressnameSpace, Addressmethod);
    soapObject.addProperty("mobileCode", edit_param.getText().toString());
    soapObject.addProperty("userid", "dbdf1580476240458784992289892b87");
    SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
    envelope.bodyOut = soapObject;
    envelope.dotNet = true;
    envelope.setOutputSoapObject(soapObject);
    HttpTransportSE httpTransportSE = new HttpTransportSE(Addressurl);
    //System.out.println("Phone number information is set up, ready to start the service");
    try {
        httpTransportSE.call(AddresssoapAction, envelope);
        //System.out.println("Successfully called the WebService");
    } catch (Exception e) {
        e.printStackTrace();
        //System.out.println("Failed to call the WebService");
    }

    //Get the data returned by the service and start parsing
    SoapObject object = (SoapObject) envelope.bodyIn; //System.out.println("Data obtained from the service");
    result = object.getProperty(0).toString(); //System.out.println("Information obtained, sending message to the main thread");
    handler.sendEmptyMessage(0x001);
    //System.out.println("Message sent, the TextView will display the weather information");
}

//Don't forget to import packages and Internet permissions!

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

Reference Code Download:

WebServiceDemo.zip : Download WebServiceDemo.zip


Summary of This Section:

>

Well, this section about how to use this WebService on the Android side is explained here, and next we will learn about an Android control similar to the browser - WebView, so stay tuned ~ Thank you ~!

-1.0 Android Basic Tutorial

-1.0.1 Latest Android Basic Tutorial Catalog for 2015

-1.1 Background and System Architecture Analysis

-1.2 Development Environment Setup

-1.2.1 Developing Android APP with Eclipse + ADT + SDK

-1.2.2 Developing Android APP with Android Studio

-1.3 Solving SDK Update Issues

-1.4 Genymotion Emulator Installation

-1.5.1 Git Tutorial on Basic Operations of Local Repositories

-1.5.2 Git Using GitHub to Set Up a Remote Repository

-1.6 How to Play with 9 (Nine Sister) Images

-1.7 Interface Prototype Design

-1.8 Project Related Analysis (Various Files, Resource Access)

-1.9 Android Program Signing and Packaging

-1.11 Decompiling APK to Get Code & Resources

-[2.1 Concept of View and ViewGroup](android-tutorial-view-viewgroup-

❮ Inner Lambda Final Bubble Sort ❯