Android发送SOAP数据给服务器调用webservice,实现手机号归属地查询
16lz
2021-12-04
创建android工程MobileBelong,设置网络访问权限。
资源
view plain copy to clipboard print ?- <stringname="hello">HelloWorld,MainActivity!</string>
- <stringname="app_name">手机号归属地查询</string>
- <stringname="mobile">手机号</string>
- <stringname="button">查询</string>
- <stringname="error">网络连接失败</string>
布局
view plain copy to clipboard print ?- TextView
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="@string/mobile"/>
- <EditText
- android:id="@+id/mobile"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="13472283596"/>
- <Button
- android:id="@+id/button"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="@string/button"/>
- <TextView
- android:id="@+id/result"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"/>
在src目录下创建mobilesoap.xml,并将网址文档中提供的代码复制其中,如下
view plain copy to clipboard print ?- <?xmlversion="1.0"encoding="utf-8"?>
- <soap12:Envelopexmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:xsd="http://www.w3.org/2001/XMLSchema"xmlns:soap12="http://www.w3.org/2003/05/soap-envelope">
- <soap12:Body>
- <getMobileCodeInfoxmlns="http://WebXml.com.cn/">
- <mobileCode>$mobile</mobileCode>
- <userID></userID>
- </getMobileCodeInfo>
- </soap12:Body>
- </soap12:Envelope>
业务类:MobileService
注意访问目标地址是:
http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx
可以有协议中得到。
view plain copy to clipboard print ?
- packagecn.class3g.service;
- …
- publicclassMobileService{
- publicstaticStringgetMobileAddress(Stringmobile)throwsException{
- InputStreaminStream=MobileService.class.getClassLoader()
- .getResourceAsStream("mobilesoap.xml");
- byte[]data=StreamTool.readInputStream(inStream);
- Stringxml=newString(data);
- Stringsoap=xml.replaceAll("\\$mobile",mobile);
- /**
- *正则表达式$为特殊正则中的特殊符号须转义,即\$mobile
- *而\为字符串中的特殊符号,所以用两个反斜杠,即"\\{1}quot;
- */
- Stringpath="http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx";
- data=soap.getBytes();//得到了xml的实体数据
- URLurl=newURL(path);
- HttpURLConnectionconn=(HttpURLConnection)url.openConnection();
- conn.setConnectTimeout(5*1000);
- conn.setRequestMethod("POST");
- conn.setDoOutput(true);
- conn.setRequestProperty("Content-Type",
- "application/soap+xml;charset=utf-8");
- conn.setRequestProperty("Content-Length",String.valueOf(data.length));
- OutputStreamoutStream=conn.getOutputStream();
- outStream.write(data);
- outStream.flush();
- outStream.close();
- if(conn.getResponseCode()==200){
- InputStreamresponseStream=conn.getInputStream();
- returnparseXML(responseStream);
- }
- returnnull;
- }
- /**
- *解析返回xml数据
- *
- *@paramresponseStream
- *@return
- *@throwsException
- */
- privatestaticStringparseXML(InputStreamresponseStream)throwsException{
- XmlPullParserparser=Xml.newPullParser();
- parser.setInput(responseStream,"UTF-8");
- intevent=parser.getEventType();
- while(event!=XmlPullParser.END_DOCUMENT){
- switch(event){
- caseXmlPullParser.START_TAG:
- if("getMobileCodeInfoResult".equals(parser.getName())){
- returnparser.nextText();
- }
- break;
- }
- event=parser.next();
- }
- returnnull;
- }
- }
工具类StreamTool
view plain copy to clipboard print ?- packagecn.class3g.utils;
- …
- publicclassStreamTool{
- /**
- *从输入流读取数据
- *@paraminStream
- *@return
- *@throwsException
- */
- publicstaticbyte[]readInputStream(InputStreaminStream)throwsException{
- ByteArrayOutputStreamoutSteam=newByteArrayOutputStream();
- byte[]buffer=newbyte[1024];
- intlen=0;
- while((len=inStream.read(buffer))!=-1){
- outSteam.write(buffer,0,len);
- }
- outSteam.close();
- inStream.close();
- returnoutSteam.toByteArray();
- }
- }
- Activity类MobileBelongActivity
- packagecn.class3g.mobile;
- …
- publicclassMobileBelongActivityextendsActivity{
- privatestaticfinalStringTAG="MainActivity";
- privateEditTextmobileText;
- privateTextViewresultView;
- @Override
- publicvoidonCreate(BundlesavedInstanceState){
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- mobileText=(EditText)this.findViewById(R.id.mobile);
- resultView=(TextView)this.findViewById(R.id.result);
- Buttonbutton=(Button)this.findViewById(R.id.button);
- button.setOnClickListener(newView.OnClickListener(){
- @Override
- publicvoidonClick(Viewv){
- Stringmobile=mobileText.getText().toString();
- try{
- Stringaddress=MobileService.getMobileAddress(mobile);
- resultView.setText(address);
- }catch(Exceptione){
- Log.e(TAG,e.toString());
- Toast.makeText(MobileBelongActivity.this,R.string.error,1).show();
- }
- }
- });
- }
- }
更多相关文章
- Android中构建数据业务应用
- Android的四大天王
- Android存储-SharedPreferences
- 系出名门Android(9) - 数据库支持(SQLite), 内容提供器(ContentP
- Android深入浅出之Audio 第一部分 AudioTrack分析
- SSH服务器与Android通信(2)--Android客户端接收数据
- 如何查看android数据文件?
- android客户端和servlet服务端的简单登录实现
- android SQLite数据库存储数据