/** * 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); } }
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。