下載jar包,放在libs下android
經過Project Structure添加jar依賴web
成功後就會在build.gradle下添加服務器
final static String SERVICE_NS = "http://ws.service.mService.et.cn/"; final static String SERVICE_URL = "http://192.168.9.59:8080/DSer/service/SysService?WSDL"; private EditText txt1; private EditText txt2; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); //注意下面兩行 StrictMode.ThreadPolicy policy= new StrictMode.ThreadPolicy.Builder().permitAll().build(); StrictMode.setThreadPolicy(policy); setContentView(R.layout.activity_cxf); Button button = findViewById(R.id.cxf_button1); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { //Toast.makeText(CxfActivity.this,"ceshi",Toast.LENGTH_LONG).show(); getCxf(); } }); txt1 = findViewById(R.id.cxf_EditText1); txt2 = findViewById(R.id.cxf_EditText2); } public void getCxf() { //調用的方法 String methodName = "getEtUser"; //建立httpTransportSE傳輸對象 HttpTransportSE ht = new HttpTransportSE(SERVICE_URL); ht.debug = true; //使用soap1.1協議建立Envelop對象 SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11); //實例化SoapObject對象 SoapObject request = new SoapObject(SERVICE_NS, methodName); /** * 設置參數,參數名不必定須要跟調用的服務器端的參數名相同,只須要對應的順序相同便可 * */ request.addProperty("userName", "000"); //將SoapObject對象設置爲SoapSerializationEnvelope對象的傳出SOAP消息 envelope.bodyOut = request; try{ //調用webService ht.call(null, envelope); //txt1.setText("看看"+envelope.getResponse()); if(envelope.getResponse() != null){ txt2.setText("有返回"); SoapObject result = (SoapObject) envelope.bodyIn; SoapObject soap = (SoapObject) result.getProperty(0); //這個例子是返回一個對象。 String name =soap.getProperty("name").toString(); String userName =soap.getProperty("userName").toString(); String department =soap.getProperty("department").toString(); txt1.setText("返回值 = "+ name+ " "+userName + " " +department); }else{ txt2.setText("無返回"); } }catch (Exception e) { e.printStackTrace(); } }
AndroidManifest.xml下添加ide
<uses-permission android:name="android.permission.INTERNET" />