Android调用Webservice实现手机号码归属地查询

一、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;// 点击查询

	@Override
	protected 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() {

			@Override
			public void onClick(View v) {
				String tel = telText.getText().toString();
				String wsdlUrl = "http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx";
				String webMethod = "getMobileCodeInfo";
				// 因为结果只有一条所以不用soapObject
				Object 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);
	 * 
	 * }
	 */
	@Override
	public 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;
	}

}


五、程序的运行结果如下

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值