Android调用WebService,其实这有点类似于一些给咱们供给原始数据API服务的数据渠道,比方聚合数据!而WebService则用到了XML和SOAP,经过HTTP协议即可完结与长途机器的交互!
1.WebService简介
本节咱们并不讨论怎么去搭建一个WebService,咱们只是知道怎么去获取WebService供给的服务,然后解析回来的XML数据,然后把相关数据显现到咱们的Android设备上就好!
2.去哪里获取WebService服务
网上有许多供给WebService的站点,首要找到这些站点,然后获取相应的服务即可!这儿选取WebXml和云聚36wu作为例子给咱们讲解下,他们的官网:
webXml:www.webxml.com.cn/zh_cn/index…
曾经是免费的,不过都商业化了,许多服务都要收费,可是能够试用~改站点上供给了16个不同的Web服务,能够依据自己的需求,查询相应服务,调用不同的接口!
webXml的相关页面:
相关使用次数阐明:
云聚36wu:www.36wu.com/Service
相同也供给了许多的服务,许多手机的app都是用的这儿的接口,比方彩虹公交,手机气候等不过,这个也是要收费的=-=,能够试用,不过只能一小时内发送20次恳求;点击申请使用,取得key就能够了!两者随意选一个吧!
3.第三方jar包的预备
首要假如想在Android渠道上调用WebService需求依赖于第三方类库:ksoap2而在Android渠道上,使用的是ksoap2 Android,一个高效,轻量级的SOAP开发包!
jar包下载地址:code.google.com/p/ksoap2-an…
天朝可能上不去,这儿供给两个百度云的链接供咱们下载使用:
2.54版别:ksoap2-android 2.54.jar
3.30版别:ksoap2-android 3.30.jar
假如所幸你能进入jar包的下载地址的话,那么你会看到下面的界面:
4.获取相关的一些参数
比方咱们这儿找的是气候的查询参数,点进去咱们能够看到这样一个参数文档:
比方这儿咱们需求的是气候查询部分的功能:
先把框住的SoapAction和NameSpace拷贝下来!当然咱们能够在这个页面测验,别的咱们是免费用户,id能够不填直接跳过,输入后点击调用按钮会打开这样一个页面:
嘿嘿,这儿便是回来的XML,而咱们要做的也便是解析这样一个XML,别的这儿的.gif代表的是气候图标!
同理,咱们再把归属地查询的看下SoapAction,NameSpace以及相关参数mark下!
以及回来后的XML数据:
5.注册并启用相关WEB服务
点击我的Web服务器,然后点击试用,WebXML给咱们供给了五天的免费试用,咱们把需求的两个服务器敞开!
6.调用WebService的代码示例
嗯,接下来咱们来写代码验证调用WebService的流程:
运行效果图:
完成代码:
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的权限!
<uses-permission android:name="android.permission.INTERNET"/>