webService 很久不用了,第一次使用还是13年, 早已忘记怎么搞了.今天看了篇博文,写了个demo .记录下吧!
首先要下载skoap2 .... xxx.jar ,我用的是最新的3.6.2 , 下载地址网上很多. 随便下载个版本就行.
public class MainActivity extends AppCompatActivity { private Button btn; @Override protected void onCreate(Bundle savedInstanceState) { StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build(); StrictMode.setThreadPolicy(policy); super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); btn = (Button) findViewById(R.id.button); btn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { new Thread(){ @Override public void run() { getRemoteInfo(); } }.start(); } }); } /** * 得到远程数据 */ private void getRemoteInfo() { //String nameSpace = "http://WebXml.com.cn/";//命名空间 String nameSpace = "http://WebXml.com.cn/";//命名空间 //String methodName = "getMobileCodeInfo";//调用的方法名 String methodName = "getOpenFundString";//调用的方法名 //String endPoint = "http://www.webxml.com.cn/WebServices/MobileCodeWS.asmx"; String endPoint = "http://ws.webxml.com.cn/WebServices/ChinaOpenFundWS.asmx"; //String soapAction = "http://WebXml.com.cn/getMobileCodeInfo"; String soapAction = "http://WebXml.com.cn/getOpenFundString"; //指定webservice的命名空间和调用方法 SoapObject rpc = new SoapObject(nameSpace,methodName); //需要传入的参数 //rpc.addProperty("theCityName","郑州"); //rpc.addProperty("",null); //生产调用webService方法的SOAP请求信息,并指定SOAP的版本 ,这个版本 根据 asdl文件中是1.1还是1.2协议来指定的. 一般VER11或者VER12都可以,以前还有VER10 SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER12); envelope.bodyOut = rpc; envelope.dotNet = true;//设置是否调用的是dotNet开发的webService //(new MarshalBase64()).register(envelope); // 等价于envelope.bodyOut = rpc; envelope.setOutputSoapObject(rpc); HttpTransportSE transportSE = new HttpTransportSE(endPoint); transportSE.debug = true; try { //调用webService transportSE.call(soapAction,envelope); transportSE.call(null,envelope); if(envelope.getResponse()!=null){ //获取返回的结果 // SoapObject object = (SoapObject) envelope.bodyIn; // List<String> list= new ArrayList<>(); // int count = object.getPropertyCount(); // for (int i=0;i<count;i++){ // SoapObject subObject = (SoapObject) object.getProperty(i); // String name = subObject.getPropertyAsString(i); // // list.add(name); // } // Log.e("集合长度",list.size()+""); String result = String.valueOf(envelope.getResponse()); Log.e("返回结果--------->",result); Message message = Message.obtain(); message.obj = result; handler.sendMessage(message); } } catch (Exception e) { e.printStackTrace(); } } private Handler handler = new Handler(){ @Override public void handleMessage(Message msg) { super.handleMessage(msg); String result = (String) msg.obj; } }; }
代码中有一大段注释,这里可以 获取网络数据并解析,一般情况下获取的数据有3种样式,xml 或者 json, 前两者都好说.
还有一种是anyType{string=xxxx; string=1232; ...}
参考:
http://blog.csdn.net/xf396191344/article/details/32135729
http://www.cnblogs.com/bruceli-net/p/5688216.html
http://blog.csdn.net/yuanyuan_186/article/details/51122579