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;}}


五、程序的运行结果如下

 

原创粉丝点击