Today, I have taken a look at other people's weather forecasts for the Android platform. I have also modified the weather forecasts and implemented the formats I want to implement. I 'd like to share them with you:
First import third-party package: ksoap2-android-assembly-2.4-jar-with-dependencies.jar
Main program code:
Package com. sebservice;
Import java. Io. ioexception;
Import java. Io. unsupportedencodingexception;
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. App. alertdialog;
Import Android. App. alertdialog. Builder;
Import Android. OS. Bundle;
Import Android. View. view;
Import Android. View. View. onclicklistener;
Import Android. widget. Button;
Import Android. widget. Toast;
Public class websactivity 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. Bt1 );
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 {
Soapobject RPC = new soapobject (namespace, method_name );
Rpc. addproperty ("thecityname", cityname );
Soapserializationenvelope envelope = new soapserializationenvelope (soapenvelope. ver11 );
Envelope. bodyout = RPC;
Envelope. DOTNET = true;
Envelope. setoutputsoapobject (RPC );
Httptransportse ht = new httptransportse (URL );
Ht. DEBUG = true;
Ht. Call (soap_action, envelope );
Detail = (soapobject) envelope. getresponse ();
Getw (detail, city );
Return;
} Catch (exception e ){
E. printstacktrace ();
}
}
Private void getw (soapobject detail, string local) throws unsupportedencodingexception
{
String STR = detail. getproperty (6). tostring ();
String show = "City:" + local;
Show = show + "\ n today:" + Str. Split ("") [0];
Show = show + "\ n weather:" + Str. Split ("") [1];
Show = show + "\ n wind level:" + detail. getproperty (7). tostring (). Split ("") [0];
Show = show + "\ n" + detail. getproperty (10). tostring (). Split ("") [0];
Show = show + "\ n tip: \ n" + detail. getproperty (11). tostring (). Split ("") [0];
Builder Al = new alertdialog. Builder (this );
Al. settitle ("Weather Forecast ");
Al. setpositivebutton ("OK", null );
Al. setmessage (show. tostring ());
Al. Create (). Show ();
}
}
The layout file content is:
<? XML version = "1.0" encoding = "UTF-8"?>
<Linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"
Android: Orientation = "vertical">
<Textview
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: text = "@ string/Hello"/>
<Button
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: Id = "@ + ID/Bt1"
Android: text = "viewing weather in Beijing"
/>
</Linearlayout>
3. add permissions in androidmanifest. xml
<? XML version = "1.0" encoding = "UTF-8"?>
<Manifest xmlns: Android = "http://schemas.android.com/apk/res/android"
Package = "com. sebservice"
Android: versioncode = "1"
Android: versionname = "1.0" type = "codeph" text = "/codeph">
<Uses-SDK Android: minsdkversion = "10"/>
<Application
Android: icon = "@ drawable/ic_launcher"
Android: Label = "@ string/app_name">
<Activity
Android: Label = "@ string/app_name"
Android: Name = ". websactivity">
<Intent-filter>
<Action Android: Name = "android. Intent. Action. Main"/>
<Category Android: Name = "android. Intent. Category. launcher"/>
</Intent-filter>
</Activity>
</Application>
<Uses-Permission Android: Name = "android. Permission. Internet"> </uses-Permission>
</Manifest>