Android 访问WebService

来源:互联网 发布:windows 域环境搭建 编辑:程序博客网 时间:2024/05/11 09:37

通过几个例子和自己的修改实现了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
privatestatic final String NAMESPACE = "http://WebXml.com.cn/";
    // WebService地址
    privatestatic final String URL = "http://www.webxml.com.cn/webservices/weatherwebservice.asmx";
    privatestatic final String METHOD_NAME = "getWeatherbyCityName";
    privatestatic final String SOAP_ACTION = "http://WebXml.com.cn/getWeatherbyCityName";
 
    privateString weatherToday;
    privateButton mBtnOk;
    privateTextView mTvInfo;
    privateEditText mEtCityName;
    privateSoapObject detail;
    privateHandler handler = newHandler() {
        @Override
        publicvoid handleMessage(Message msg) {
 
            switch(msg.what) {
            case0:
                mTvInfo.setText(weatherToday);
                break;
 
            default:
                break;
            }
 
        }
 
    };
 
    @Override
    protectedvoid 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(newOnClickListener() {
 
            @Override
            publicvoid onClick(View v) {
                finalString cityName = mEtCityName.getText().toString().trim();
                 
                newThread(newRunnable() {
 
                    @Override
                    publicvoid run() {
                        getWeather(cityName);
                    }
                }).start();
 
            }
        });
         
    }
 
    privatevoid getWeather(String cityName) {
 
        try{
 
            SoapObject request = newSoapObject(NAMESPACE, METHOD_NAME);
            request.addProperty("theCityName", cityName);
 
            SoapSerializationEnvelope envelope = newSoapSerializationEnvelope(
                    SoapEnvelope.VER11);
            envelope.bodyOut = request;
            envelope.dotNet = true;
            envelope.setOutputSoapObject(request);
 
            HttpTransportSE ht = newHttpTransportSE(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();
        }
 
    }
 
    privatevoid 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]



0 0
原创粉丝点击