Android 访问WebService

通过几个例子和自己的修改实现了Android访问WebService

[1].[代码] [Java]代码 跳至 [1] [2]

?
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();
     }

[2].[文件] webService.zip ~ 2MB    下载(189) 跳至 [1] [2]



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值