Android调用Webservice实现手机号码归属地查询
来源:互联网 发布:java 线程 通信 编辑:程序博客网 时间:2024/05/20 22:00
一、UI设计如下
二、注意要在AndroidManifest.xml中添加互联网权限
<uses-permission android:name="android.permission.INTERNET"/>
三、 编写SoapUtil类访问WebService
(1)发布手机归属地的Webservice的地址是
http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx
(2)libs拷贝第三方提供的soap包
ksoap2-android-assembly-2.4-jar-with-dependencies.jar
(3)编写SoapUtil类
package com.example.soap;import java.io.IOException;import org.ksoap2.SoapEnvelope;import org.ksoap2.serialization.SoapObject;import org.ksoap2.serialization.SoapSerializationEnvelope;import org.ksoap2.transport.HttpTransportSE;import org.xmlpull.v1.XmlPullParserException;public class SoapUtil {public static Object doTransport(String wsdlUrl,String webMethod,String mobileCode){String namespace = "http://WebXml.com.cn/";//命名空间SoapObject soapObject=new SoapObject(namespace, webMethod);//创建SoapObject对象//为webMethod方法设置方法参数,这里的"mobileCode",一定是发布后的soapObject.addProperty("mobileCode", mobileCode);soapObject.addProperty("userID",null);SoapSerializationEnvelope soapSerializationEnvelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);//创建SoapSerializationEnvelope对象,并设置Webservice版本号soapSerializationEnvelope.bodyOut = soapObject;soapSerializationEnvelope.dotNet=true;soapSerializationEnvelope.setOutputSoapObject(soapObject);HttpTransportSE httpTransportSE = new HttpTransportSE(wsdlUrl);//String SOAP_ACTION = namespace+webMethod;String SOAP_ACTION = "http://WebXml.com.cn/"+webMethod;System.out.println(SOAP_ACTION);try {httpTransportSE.call(SOAP_ACTION, soapSerializationEnvelope);if(soapSerializationEnvelope.getResponse() != null){//因为返回的结果只有一条,所以不用soapObject,直接用Object即可Object result = soapSerializationEnvelope.getResponse();return result;}} catch (IOException e) {e.printStackTrace();} catch (XmlPullParserException e) {e.printStackTrace();}return null;}}
四、 编写控制器MainActivity.java
package com.example.webservice_tel;import com.example.soap.SoapUtil;import android.os.Bundle;import android.app.Activity;import android.view.Menu;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.EditText;import android.widget.TextView;public class MainActivity extends Activity {private EditText telText;// 输入手机号码private TextView telCurrentinfo;// 显示手机号码归属地信息private Button button_query;// 点击查询@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);telText = (EditText) findViewById(R.id.editContent);telCurrentinfo = (TextView) findViewById(R.id.textView_detail);button_query = (Button) findViewById(R.id.button_query);// 时间处理button_query.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {String tel = telText.getText().toString();String wsdlUrl = "http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx";String webMethod = "getMobileCodeInfo";// 因为结果只有一条所以不用soapObjectObject result = SoapUtil.doTransport(wsdlUrl, webMethod, tel);// getProperty()获取属性值,不用,因为结果只有一条String telCondition = result.toString();telCurrentinfo.setText(telCondition);}});}/* * public void getMobileCodeInfo(View view) { Toast.makeText(this, "hello", * 5).show(); String theTelNumber = telText.getText().toString(); String * wsdlUrl = "http://www.webxml.com.cn/webservices/weatherwebservice.asmx"; * String webMethod = "getWeatherbyCityName"; SoapObject result = * (SoapObject) SoapUtil.doTransport(wsdlUrl, webMethod, theTelNumber); * * String icon = result.getProperty(9).toString(); * System.out.println("icon=" + icon); String imageUrl = * "http://www.webxml.com.cn/pro_images/" + icon; try { * telIconShow.setImageBitmap(ImageTool .getBitmap(new URL(imageUrl))); } * catch (MalformedURLException e) { e.printStackTrace(); } * * String telCondtion = result.getProperty(0).toString(); * telCurrentinfo.setText(telCondtion); * * } */@Overridepublic 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;}}
五、程序的运行结果如下
- Android调用webservice实现 手机号码归属地查询
- android 调用webservice实现手机号码归属地查询
- Android调用Webservice实现手机号码归属地查询
- 我的Android进阶之旅------>Android通过调用Webservice实现手机号码归属地查询
- WebService 实现手机号码归属地查询
- webservice-实现手机号码归属地的查询
- Android利用WebService查询手机号码归属地
- Android利用WebService查询手机号码归属地
- webservice---手机号码归属地查询
- Android中手机号码归属地查询实现
- Android中手机号码归属地查询实现
- 简单的 Android 调用WebService 实现号码归属地查询
- Android 调用webservice(ksoap-2-Android)手机号码归属地查询
- java WebService实现手机号码归属地查询功能
- Java WebService服务+WSDL 实现手机号码归属地查询
- Android使用HttpURLConnect、HttpClient访问WebService查询手机号码归属地
- Android手机号码归属地查询
- Android手机号码归属地查询
- Object-C学习——类的静态成员
- HTML格式8
- 网络编程TCP
- 步步惊“芯”——软核处理器内部设计分析
- ogre font的load与使用方式
- Android调用Webservice实现手机号码归属地查询
- java文件上传
- 向上取整和向下取整
- Oracle 11g 安装JVM
- 绝妙的取样
- 系统性能调优6篇
- Winform应用程序强制退出
- 启动Activity时,不让EditText弹出输入法-解决办法
- 反射机制