Android webservice

1.WebService簡介

PS:若是看完上面簡介還不是很清楚的話,那麼就算了,以前公司就用C#搭的一個WebService! 本節咱們並不討論如何去搭建一個WebService,咱們僅僅知道如何去獲取WebService提供的服務, 而後解析返回的XML數據,而後把相關數據顯示到咱們的Android設備上就好!android


2.去哪裏獲取WebService服務

網上有不少提供WebService的站點,首先找到這些站點,而後獲取相應的服務便可! 這裏選取WebXml和雲聚36wu做爲例子給你們講解下,他們的官網:web

webXmlhttp://www.webxml.com.cn/zh_cn/index.aspx服務器

之前是免費的,不過都商業化了,不少服務都要收費,可是能夠試用~ 改站點上提供了16個不一樣的Web服務,能夠根據本身的需求,查詢相應服務,調用不一樣的接口!app

webXml的相關頁面ide

相關使用次數說明:測試

雲聚36wuhttp://www.36wu.com/Servicethis

一樣也提供了不少的服務,不少手機的app都是用的這裏的接口,好比彩虹公交,手機天氣等 不過,這個也是要收費的=-=,能夠試用,不過只能一小時內發送20次請求; 點擊申請使用,得到key就能夠了!二者隨便選一個吧!google


3.第三方jar包的準備

首先若是想在Android平臺上調用WebService須要依賴於第三方類庫:ksoap2 而在Android平臺上,使用的是ksoap2 Android,一個高效,輕量級的SOAP開發包!url

jar包下載地址:https://code.google.com/p/ksoap2-android/wiki/HowToUse?tm=2spa

天朝可能上不去,這裏提供兩個百度雲的連接供你們下載使用:

2.54版本ksoap2-android 2.54.jar

3.30版本ksoap2-android 3.30.jar

若是所幸你能進入jar包的下載地址的話,那麼你會看到下面的界面:


4.獲取相關的一些參數

首先找到咱們須要獲取的服務,而後記錄相關的參數: NameSpace(命名空間),SoapAction以及URL就不用說了,其餘參數這樣找:

好比咱們這裏找的是天氣的查詢參數,點進去咱們能夠看到這樣一個參數文檔:

好比這裏咱們須要的是天氣查詢部分的功能:

先把框住的SoapAction和NameSpace拷貝下來!固然咱們能夠在這個頁面測試,另外 咱們是免費用戶,id能夠不填直接跳過,輸入後點擊調用按鈕會打開這樣一個頁面:

嘿嘿,這裏就是返回的XML,而咱們要作的也就是解析這樣一個XML,另外這裏的 .gif表明的是天氣圖標!

同理,咱們再把歸屬地查詢的看下SoapAction,NameSpace以及相關參數mark下!

以及返回後的XML數據


5.註冊並啓用相關WEB服務

點擊個人Web服務器,而後點擊試用,WebXML給咱們提供了五天的免費試用, 咱們把須要的兩個服務器開啓!

好的,記得mark下咱們本身的key哦~


6.調用WebService的代碼示例

嗯,接下來咱們來寫代碼驗證調用WebService的流程:

運行效果圖

PS:這個號碼是之前的號碼=-=,別嘗試撥打,已經換人了~ 另外天氣服務好像有寫問題,有時並不能獲取到,估計是WebXml作的一些限制, 畢竟試用...

實現代碼

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

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

 

    private EditText edit_param;

    private Button btn_attribution;

    private Button btn_weather;

    private TextView txt_result;

 

    private String city;

    private String number;

    private String result;

 

 

    //定義獲取手機信息的SoapAction與命名空間,做爲常量

    private static final String AddressnameSpace = "http://WebXml.com.cn/";

    //天氣查詢相關參數

    private static final String Weatherurl = "http://webservice.webxml.com.cn/WebServices/WeatherWS.asmx";

    private static final String Weathermethod = "getWeather";

    private static final String WeathersoapAction = "http://WebXml.com.cn/getWeather";

    //歸屬地查詢相關參數

    private static final String Addressurl = "http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx";

    private static final String Addressmethod = "getMobileCodeInfo";

    private static final String AddresssoapAction = "http://WebXml.com.cn/getMobileCodeInfo";

 

 

    //定義一個Handler用來更新頁面:

    private Handler handler = new Handler() {

        public void handleMessage(Message msg) {

            switch (msg.what) {

                case 0x001:

                    txt_result.setText("結果顯示:\n" + result);

                    Toast.makeText(MainActivity.this"獲取天氣信息成功", Toast.LENGTH_SHORT).show();

                    break;

                case 0x002:

                    txt_result.setText("結果顯示:\n" + result);

                    Toast.makeText(MainActivity.this"號碼歸屬地查詢成功", Toast.LENGTH_SHORT).show();

                    break;

            }

 

        }

    };

 

    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        bindViews();

    }

 

    private void bindViews() {

        edit_param = (EditText) findViewById(R.id.edit_param);

        btn_attribution = (Button) findViewById(R.id.btn_attribution);

        btn_weather = (Button) findViewById(R.id.btn_weather);

        txt_result = (TextView) findViewById(R.id.txt_result);

        btn_attribution.setOnClickListener(this);

        btn_weather.setOnClickListener(this);

    }

 

    @Override

    public void onClick(View v) {

        switch (v.getId()) {

            case R.id.btn_weather:

                new Thread() {

                    @Override

                    public void run() {

                        getWether();

                    }

                }.start();

                break;

            case R.id.btn_attribution:

                new Thread(new Runnable() {

                    public void run() {

                        getland();

                    }

                }).start();

                break;

        }

    }

 

 

    //定義一個獲取某城市天氣信息的方法:

    public void getWether() {

        result = "";

        SoapObject soapObject = new SoapObject(AddressnameSpace, Weathermethod);

        soapObject.addProperty("theCityCode:", edit_param.getText().toString());

        soapObject.addProperty("theUserID""dbdf1580476240458784992289892b87");

        SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);

        envelope.bodyOut = soapObject;

        envelope.dotNet = true;

        envelope.setOutputSoapObject(soapObject);

        HttpTransportSE httpTransportSE = new HttpTransportSE(Weatherurl);

        System.out.println("天氣服務設置完畢,準備開啓服務");

        try {

            httpTransportSE.call(WeathersoapAction, envelope);

//            System.out.println("調用WebService服務成功");

        catch (Exception e) {

            e.printStackTrace();

//            System.out.println("調用WebService服務失敗");

        }

 

        //得到服務返回的數據,而且開始解析

        SoapObject object = (SoapObject) envelope.bodyIn;

        System.out.println("得到服務數據");

        result = object.getProperty(1).toString();

        handler.sendEmptyMessage(0x001);

        System.out.println("發送完畢,textview顯示天氣信息");

    }

 

 

    //定義一個獲取號碼歸屬地的方法:

    public void getland() {

        result = "";

        SoapObject soapObject = new SoapObject(AddressnameSpace, Addressmethod);

        soapObject.addProperty("mobileCode", edit_param.getText().toString());

        soapObject.addProperty("userid""dbdf1580476240458784992289892b87");

        SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);

        envelope.bodyOut = soapObject;

        envelope.dotNet = true;

        envelope.setOutputSoapObject(soapObject);

        HttpTransportSE httpTransportSE = new HttpTransportSE(Addressurl);

        //    System.out.println("號碼信息設置完畢,準備開啓服務");

        try {

            httpTransportSE.call(AddresssoapAction, envelope);

            //System.out.println("調用WebService服務成功");

        catch (Exception e) {

            e.printStackTrace();

            //System.out.println("調用WebService服務失敗");

        }

 

        //得到服務返回的數據,而且開始解析

        SoapObject object = (SoapObject) envelope.bodyIn;//System.out.println("得到服務數據");

        result = object.getProperty(0).toString();//System.out.println("獲取信息完畢,向主線程發信息");

        handler.sendEmptyMessage(0x001);

        //System.out.println("發送完畢,textview顯示天氣信息");

    }

 

 

}

另外,別忘了導包和Internet的權限!

1

<uses-permission android:name="android.permission.INTERNET"/>

相關文章
相關標籤/搜索