webService 好久不用了,第一次使用仍是13年, 早已忘記怎麼搞了.今天看了篇博文,寫了個demo .記錄下吧!html
首先要下載skoap2 .... xxx.jar ,我用的是最新的3.6.2 , 下載地址網上不少. 隨便下載個版本就行.web
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, 前二者都好說.json
還有一種是anyType{string=xxxx; string=1232; ...} 網絡
參考:koa
http://blog.csdn.net/xf396191344/article/details/32135729 ide
http://www.cnblogs.com/bruceli-net/p/5688216.htmlui
http://blog.csdn.net/yuanyuan_186/article/details/51122579spa