andorid 调用自己写的WebService
发布时间:2020-12-16 23:34:59 所属栏目:安全 来源:网络整理
导读:直接附上我的一张贴图吧。 然后看看我的代码很简单的调用。 public class MainActivity extends Activity {Button button;TextView textView;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentV
直接附上我的一张贴图吧。
public class MainActivity extends Activity { Button button; TextView textView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); button = (Button) findViewById(R.id.ok); textView = (TextView) findViewById(R.id.textveiw); button.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { // TODO Auto-generated method stub new Thread(new Runnable() { @Override public void run() { // TODO Auto-generated method stub // 命名空间 String nameSpace = "http://service.bobo.com/"; // 调用的方法名称 String methodName = "getName"; // EndPoint String endPoint = "http://222.18.159.55:8080/webService/UserInfoPort?xsd=1"; // SOAP Action String soapAction = "http://222.18.159.55:8080/webService/UserInfoPort"; // 指定WebService的命名空间和调用的方法名 SoapObject rpc = new SoapObject(nameSpace,methodName); // 设置需调用WebService接口需要传入的参数 为arg0 arg1 ...... // rpc.addProperty("arg0","ijvajvajvas"); // 生成调用WebService方法的SOAP请求信息,并指定SOAP的版本 SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER10); envelope.bodyOut = rpc; // 设置是否调用的是dotNet开发的WebService envelope.dotNet = false; // 等价于envelope.bodyOut = rpc; envelope.setOutputSoapObject(rpc); HttpTransportSE transport = new HttpTransportSE(endPoint); try { // 调用WebService transport.call(soapAction,envelope); } catch (Exception e) { e.printStackTrace(); } // 获取返回的数据 SoapObject object = (SoapObject) envelope.bodyIn; // 获取返回的结果 final String result = object.getProperty(0).toString(); // 将WebService返回的结果显示在TextView中 runOnUiThread(new Runnable() { @Override public void run() { // TODO Auto-generated method stub textView.setText(result); } }); } }).start(); } }); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main,menu); return true; } } 最后附上我的工程代码,分为WebService服务器和andorid客户端调用 http://download.csdn.net/detail/shengbo1992/7404775 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |