A simple example of Android WebService calling Weather Forecast

Source: Internet
Author: User

Here is an example of self-built online: http://express.ruanko.com/ruanko-express_34/technologyexchange5.html

However, this example is not illustrated on the Internet, and some situations are different, so I wrote it again.

1. obtain and use the KSOAP package

The Android SDK does not provide a library to call WebService. Therefore, you must use a third-party SDK to call WebService. The WebService library of PC version is very rich, but these are too large for Android. There are some sdks suitable for WebService clients on mobile phones. KSOAP2 is commonly used.

KSOAP2 address: http://code.google.com/p/ksoap2-android/

The latest I downloaded is: ksoap2-android-assembly-2.5.4-jar-with-dependencies.jar

Note:

I made a low-level error when using ksoap2-android: The import org. ksoap2 cannot be resolved.
At that time, when I analyzed this problem, I thought it was a problem with Eclipse. I couldn't find many methods,
Actually the ksoap2-android-assembly-2.5.4-jar-with-dependencies.jar file I downloaded was wrong and led to a detour.

In http://code.google.com/p/ksoap2-android/wiki/HowToUse? On the tm = 2 page, right-click the link and save it as a plain text Html file with the same name. Instead of what we want.

I am in
Http://code.google.com/p/ksoap2-android/source/browse/m2-repo/com/google/code/ksoap2-android/ksoap2-android-assembly/2.5.4/ksoap2-android-assembly-2.5.4-jar-with-dependencies.jar point View raw file to download the corresponding file correctly.

 

 

Select our project, right-click Build Path-> Add External Archives... Add this downloaded package

 

After adding it, right-click our project and choose Build Path> Configure Build Path Libraries from the shortcut menu. The following figure is displayed:

 

2. Call WebService in the following steps

 

1. Specify the WebService namespace and call Method

Import org. ksoap2.serialization. SoapObject;
Private static final String NAMESPACE = "http://WebXml.com.cn /";
Private static final String METHOD_NAME = "getWeatherbyCityName ";

SoapObject rpc = new SoapObject (NAMESPACE, METHOD_NAME); the first parameter of the SoapObject class represents the WebService NAMESPACE. You can find the WebService NAMESPACE from the WSDL document.
The second parameter indicates the name of the WebService method to be called.

2. Set the parameter value of the method to be called. If there is no parameter, it can be omitted. The code for setting the parameter value of the method is as follows:

Rpc. addProperty ("theCityName", "Beijing"); note that although the 1st parameters of the addProperty method represent the parameter names of the called methods, however, this parameter value is not necessarily the same as the method parameter name in the WebService class of the server. You only need to set the Parameter order to be the same.

3. Generate the SOAP request information that calls the Webservice method.

SoapSerializationEnvelope envelope = new SoapSerializationEnvelope (SoapEnvelope. VER11 );
Envelope. bodyOut = rpc;
Envelope. dotNet = true;
Envelope. setOutputSoapObject (rpc); when creating a SoapSerializationEnvelope object, you must use the SoapSerializationEnvelope class constructor to set the SOAP Protocol version number.
This version number must be set according to the WebService version number on the server.
After creating a SoapSerializationEnvelope object, do not forget to set the bodyOut attribute of the SOAPSoapSerializationEnvelope class,
The value of this attribute is the SoapObject created in step 1.

4. Create an HttpTransportsSE object.

Do not use AndroidHttpTransport ht = new AndroidHttpTransport (URL) here; this is a class to expire

Private static String URL = "http://www.webxml.com.cn/webservices/weatherwebservice.asmx ";
HttpTransportSE ht = new HttpTransportSE (URL); ht. debug = true; 5. call the WebService method using the call method.

Private static String SOAP_ACTION = "http://WebXml.com.cn/getWeatherbyCityName ";
Ht. call (SOAP_ACTION, envelope); some people on the Internet say that the first parameter of call here is null, but after my test, null is not acceptable.
The 2nd parameters are the SoapSerializationEnvelope object created in step 1.

6. Obtain the returned results of the WebService method.

There are two methods:

1. Use the getResponse method to obtain the returned data.

Private SoapObject detail;
Detail = (SoapObject) envelope. getResponse (); 2. Use bodyIn and getProperty.

Private SoapObject detail;
SoapObject result = (SoapObject) envelope. bodyIn;
Detail = (SoapObject) result. getProperty ("getWeatherbyCityNameResult"); 7. An error occurs during the execution, prompting you that you do not have the permission to access the network.

You need to modify the AndroidManifest. xml file and grant the corresponding permissions.

Simply put, add the following line of Configuration:

The complete AndroidManifest. xml file is as follows:

 

Note: In Android, The system. out. print () output item is written for debugging.

In the menu: Window --> show view --> other --> Find Android and select Logcat to view the output,
If you want to see the output of system. out. print () in a separate window, click the green "+" on the logcat interface,

Fill in System. out in the Filter name and By log tag, so that you can view the output of system. out. print () on a separate interface !!

1.0 "encoding =" UTF-8 "?>
Http://schemas.android.com/apk/res/android"
Package = "ghj1976.MyWeather" android: versionCode = "1"
Android: versionName = "1.0" type = "codeph" text = "/codeph">

@ Drawable/icon "android: label =" @ string/app_name ">
. MyWeatherActivity "android: label =" @ string/app_name ">

Android. intent. action. MAIN "/>
Android. intent. category. LAUNCHER "/>


 
Android. permission. INTERNET ">

The complete code is as follows:

Package ghj1976.MyWeather;

Import java. io. UnsupportedEncodingException;

Import android. app. Activity;
Import android. OS. Bundle;
Import android. view. View;
Import android. widget. Button;
Import android. widget. Toast;

Import org. ksoap2.SoapEnvelope;
Import org. ksoap2.serialization. SoapObject;
Import org. ksoap2.serialization. SoapSerializationEnvelope;
// Import org. ksoap2.transport. AndroidHttpTransport;
Import org. ksoap2.transport. HttpTransportSE;

Public class MyWeatherActivity extends Activity {

Private Button okButton;

/** Called when the activity is first created .*/
@ Override
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. main );

OkButton = (Button) this. findViewById (R. id. btn_Search );
OkButton. setOnClickListener (new Button. OnClickListener (){
@ Override
Public void onClick (View v ){
String city = "Beijing ";
GetWeather (city );
}

});
}

Private static final String NAMESPACE = "http://WebXml.com.cn /";

// WebService address
Private static String URL = "http://www.webxml.com.cn/webservices/weatherwebservice.asmx ";

Private static final String METHOD_NAME = "getWeatherbyCityName ";

Private static String SOAP_ACTION = "http://WebXml.com.cn/getWeatherbyCityName ";

Private String weatherToday;

Private SoapObject detail;

Public void getWeather (String cityName ){
Try {
System. out. println ("rpc ------");
SoapObject rpc = new SoapObject (NAMESPACE, METHOD_NAME );
System. out. println ("rpc" + rpc );
System. out. println ("cityName is" + cityName );
Rpc. addProperty ("theCityName", cityName );


SoapSerializationEnvelope envelope = new SoapSerializatio

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.