加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

webService的使用

发布时间:2020-12-16 23:47:49 所属栏目:安全 来源:网络整理
导读:/** ?* Android平台调用WebService(手机号码归属地查询)? ?* @author inhua05 ?* ?*/ public class MainActivity extends Activity { private EditText phoneSecEditText; ? private TextView resultView; ? private Button queryButton; ? @Override prot
/** ?* Android平台调用WebService(手机号码归属地查询)? ?* @author inhua05 ?* ?*/ public class MainActivity extends Activity { private EditText phoneSecEditText; ? private TextView resultView; ? private Button queryButton; ? @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); phoneSecEditText = (EditText) findViewById(R.id.phone_sec); ? ? ? resultView = (TextView) findViewById(R.id.result_text); ? ? ? queryButton = (Button) findViewById(R.id.query_btn); ? ?? ? ? queryButton.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // 手机号码(段) ? ? ? ? ? ? ? ? ? String phoneSec = phoneSecEditText.getText().toString().trim();? ? ? ? ? ? ? ?// 简单判断用户输入的手机号码(段)是否合法 ? ? ? ? ? ? ? ? ? if ("".equals(phoneSec) || phoneSec.length() < 7) { ? ? ? ? ? ? ? ? ? ? ? // 给出错误提示 ? ? ? ? ? ? ? ? ? ? ? phoneSecEditText.setError("您输入的手机号码(段)有误!"); ? ? ? ? ? ? ? ? ? ? ? phoneSecEditText.requestFocus(); ? ? ? ? ? ? ? ? ? ? ? // 将显示查询结果的TextView清空 ? ? ? ? ? ? ? ? ? ? ? resultView.setText(""); ? ? ? ? ? ? ? ? ? ? ? return; ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? ? // 查询手机号码(段)信息 ? ? ? ? ? ? ? ? ? getRemoteInfo(phoneSec); } }); } //手机号段归属地查询 /** <s:schema elementFormDefault="qualified" targetNamespace="http://WebXml.com.cn/"> <s:element name="getMobileCodeInfo"> ? //方法名 <s:complexType> <s:sequence> <s:element minOccurs="0" maxOccurs="1" name="mobileCode" type="s:string"/> ?//参数 <s:element minOccurs="0" maxOccurs="1" name="userID" type="s:string"/> </s:sequence> </s:complexType> </s:element> <s:element name="getMobileCodeInfoResponse"> ? //返回方法名 <s:complexType> <s:sequence> <s:element minOccurs="0" maxOccurs="1" name="getMobileCodeInfoResult" type="s:string"/> </s:sequence> </s:complexType> </s:element> * @param phoneSec */ protected void getRemoteInfo(String phoneSec) { // 命名空间 ? targetNamespace="http://WebXml.com.cn/" String nameSpace = "http://WebXml.com.cn/"; ?? // 调用的方法名称 ? String methodName = "getMobileCodeInfo"; ? // EndPoint ? EndPoint通常是将WSDL地址末尾的"?wsdl"去除后剩余的部分 http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx?wsdl String endPoint = "http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx"; // SOAP Action ?SOAP Action通常为命名空间 + 调用的方法名称。 ? ? ? ? String soapAction = "http://WebXml.com.cn/getMobileCodeInfo";? ? ? ? ?? ? ? ? ? // 第一步:实例化soapObject对象,指定webService的命名空间,以及调用方法名称 ? ? ? ? SoapObject rpc = new SoapObject(nameSpace,methodName); ? ? ? ? //第二步:假如方法中有参数的话,设置调用方法的参数 ? ? ? ? rpc.addProperty("mobileCode",phoneSec); ? ? ? ? rpc.addProperty("userId",""); ? ? ? ? ?? ? ? ? ? //第三步: 生成调用WebService方法的SOAP请求信息,并指定SOAP的版本 ? ? ? ? SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER10);? ? ? ? ? envelope.bodyOut = rpc; ? ? ? ? envelope.dotNet = true; ? ? ? ? envelope.setOutputSoapObject(rpc); ? ? ? ?? ? ? ? ? //第四步:创建HttpTransportsSE对象。 ? ? ? ? HttpTransportSE transport = new HttpTransportSE(endPoint); ? ? ? ? transport.debug = true; ? ? ? ?? ? ? ? ? //第五步:使用HttpTransportsSE对象call方法调用WebService方法 ? ? ? ? try { transport.call(soapAction,envelope); } catch (IOException e) { e.printStackTrace(); } catch (XmlPullParserException e) { e.printStackTrace(); } ? ? ? ? //第六步:获得WebService方法的返回结果 1、使用getResponse方法获得返回数据 ?2、使用 bodyIn 及 getProperty。 ? ? ? ? SoapObject object = (SoapObject) envelope.bodyIn; ?// 获取返回的数据 ? ? ? ? String result = object.getProperty(0).toString(); ? ? ? ? ?? ? ? ? ? resultView.setText(result); } }

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读