本節(jié)引言:
1.WebService簡介
PS:如果看完上面簡介還不是很清楚的話,那么就算了,之前公司就用C#搭的一個WebService! 本節(jié)我們并不討論如何去搭建一個WebService,我們僅僅知道如何去獲取WebService提供的服務(wù), 然后解析返回的XML數(shù)據(jù),然后把相關(guān)數(shù)據(jù)顯示到我們的Android設(shè)備上就好! 2.去哪里獲取WebService服務(wù)網(wǎng)上有很多提供WebService的站點(diǎn),首先找到這些站點(diǎn),然后獲取相應(yīng)的服務(wù)即可! 這里選取WebXml和云聚36wu作為例子給大家講解下,他們的官網(wǎng): webXml:http://www./zh_cn/index.aspx
webXml的相關(guān)頁面:
相關(guān)使用次數(shù)說明:
云聚36wu:http://www./Service
3.第三方j(luò)ar包的準(zhǔn)備
4.獲取相關(guān)的一些參數(shù)
同理,我們再把歸屬地查詢的看下SoapAction,NameSpace以及相關(guān)參數(shù)mark下!
以及返回后的XML數(shù)據(jù):
5.注冊并啟用相關(guān)WEB服務(wù)
點(diǎn)擊我的Web服務(wù)器,然后點(diǎn)擊試用,WebXML給我們提供了五天的免費(fèi)試用, 我們把需要的兩個服務(wù)器開啟!
好的,記得mark下我們自己的key哦~ 6.調(diào)用WebService的代碼示例嗯,接下來我們來寫代碼驗(yàn)證調(diào)用WebService的流程: 運(yùn)行效果圖:
PS:這個號碼是以前的號碼=-=,別嘗試撥打,已經(jīng)換人了~ 另外天氣服務(wù)好像有寫問題,有時并不能獲取到,估計(jì)是WebXml做的一些限制, 畢竟試用... 實(shí)現(xiàn)代碼: 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;
//定義獲取手機(jī)信息的SoapAction與命名空間,作為常量
private static final String AddressnameSpace = "http://WebXml.com.cn/";
//天氣查詢相關(guān)參數(shù)
private static final String Weatherurl = "http://webservice./WebServices/WeatherWS.asmx";
private static final String Weathermethod = "getWeather";
private static final String WeathersoapAction = "http://WebXml.com.cn/getWeather";
//歸屬地查詢相關(guān)參數(shù)
private static final String Addressurl = "http://webservice./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("結(jié)果顯示:\n" + result);
Toast.makeText(MainActivity.this, "獲取天氣信息成功", Toast.LENGTH_SHORT).show();
break;
case 0x002:
txt_result.setText("結(jié)果顯示:\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("天氣服務(wù)設(shè)置完畢,準(zhǔn)備開啟服務(wù)");
try {
httpTransportSE.call(WeathersoapAction, envelope);
// System.out.println("調(diào)用WebService服務(wù)成功");
} catch (Exception e) {
e.printStackTrace();
// System.out.println("調(diào)用WebService服務(wù)失敗");
}
//獲得服務(wù)返回的數(shù)據(jù),并且開始解析
SoapObject object = (SoapObject) envelope.bodyIn;
System.out.println("獲得服務(wù)數(shù)據(jù)");
result = object.getProperty(1).toString();
handler.sendEmptyMessage(0x001);
System.out.println("發(fā)送完畢,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("號碼信息設(shè)置完畢,準(zhǔn)備開啟服務(wù)");
try {
httpTransportSE.call(AddresssoapAction, envelope);
//System.out.println("調(diào)用WebService服務(wù)成功");
} catch (Exception e) {
e.printStackTrace();
//System.out.println("調(diào)用WebService服務(wù)失敗");
}
//獲得服務(wù)返回的數(shù)據(jù),并且開始解析
SoapObject object = (SoapObject) envelope.bodyIn;//System.out.println("獲得服務(wù)數(shù)據(jù)");
result = object.getProperty(0).toString();//System.out.println("獲取信息完畢,向主線程發(fā)信息");
handler.sendEmptyMessage(0x001);
//System.out.println("發(fā)送完畢,textview顯示天氣信息");
}
}
另外,別忘了導(dǎo)包和Internet的權(quán)限! <uses-permission android:name="android.permission.INTERNET"/> 參考代碼下載:WebServiceDemo.zip:下載 WebServiceDemo.zip 本節(jié)小結(jié):
|
|
|