标签:
通过几个例子和自己的修改实现了Android访问WebService
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
|
private
static
final
String NAMESPACE = "http://WebXml.com.cn/" ; //
WebService地址 private
static
final
String URL = "http://www.webxml.com.cn/webservices/weatherwebservice.asmx" ; private
static
final
String METHOD_NAME = "getWeatherbyCityName" ; private
static
final
String SOAP_ACTION = "http://WebXml.com.cn/getWeatherbyCityName" ; private
String weatherToday; private
Button mBtnOk; private
TextView mTvInfo; private
EditText mEtCityName; private
SoapObject detail; private
Handler handler = new
Handler() { @Override public
void
handleMessage(Message msg) { switch
(msg.what) { case
0 : mTvInfo.setText(weatherToday); break ; default : break ; } } }; @Override protected
void
onCreate(Bundle savedInstanceState) { super .onCreate(savedInstanceState); setContentView(R.layout.activity_main); mBtnOk
= (Button) this .findViewById(R.id.btn_ok); mTvInfo
= (TextView) this .findViewById(R.id.tv_info); mEtCityName
= (EditText) this .findViewById(R.id.et_cityname); mBtnOk.setOnClickListener( new
OnClickListener() { @Override public
void
onClick(View v) { final
String cityName = mEtCityName.getText().toString().trim(); new
Thread( new
Runnable() { @Override public
void
run() { getWeather(cityName); } }).start(); } }); } private
void
getWeather(String cityName) { try
{ SoapObject
request = new
SoapObject(NAMESPACE, METHOD_NAME); request.addProperty( "theCityName" ,
cityName); SoapSerializationEnvelope
envelope = new
SoapSerializationEnvelope( SoapEnvelope.VER11); envelope.bodyOut
= request; envelope.dotNet
= true ; envelope.setOutputSoapObject(request); HttpTransportSE
ht = new
HttpTransportSE(URL); ht.debug
= true ; ht.call(SOAP_ACTION,
envelope); //
SoapObject result = (SoapObject) envelope.bodyIn; //
detail = (SoapObject) //
result.getProperty("getWeatherbyCityNameResult"); detail
= (SoapObject) envelope.getResponse(); parseWeather(detail); return ; }
catch
(Exception e) { e.printStackTrace(); } } private
void
parseWeather(SoapObject detail) { String
date = detail.getProperty( 6 ).toString(); System.out.println( "1
: "
+ detail.getProperty( 1 )
+ "\n" +
"2
: "
+ detail.getProperty( 2 )
+ "\n" +
"3
: "
+ detail.getProperty( 3 )
+ "\n" +
"4
: "
+ detail.getProperty( 4 )
+ "\n" +
"5
: "
+ detail.getProperty( 5 )
+ "\n" +
"6
: "
+ detail.getProperty( 6 )
+ "\n" +
"7
: "
+ detail.getProperty( 7 )
+ "\n" +
"8
: "
+ detail.getProperty( 8 )
+ "\n" ); weatherToday
= "cityName
: "
+ detail.getProperty( 1 ); weatherToday
= weatherToday + "\n今天
: "
+ date.split( "
" )[ 0 ]; weatherToday
= weatherToday + "\n天气
: "
+ date.split( "
" )[ 1 ]; weatherToday
= weatherToday + "\n气温
: " +
detail.getProperty( 5 ).toString(); weatherToday
= weatherToday + "\n风力
: " +
detail.getProperty( 7 ).toString()
+ "\n" ; handler.sendEmptyMessage( 0 ); Looper.prepare(); Toast.makeText( this ,
weatherToday, Toast.LENGTH_SHORT).show(); Looper.loop(); } |
标签:
原文地址:http://blog.csdn.net/u014311064/article/details/42675233