利用ksoap2解析制作Android手机号码归属地查询

刚刚入手安卓,网上许多资料。自己完成的第一个安卓APP。比较完善。有于异常的处理。(后有完整代码下载)

其中调用的service来自http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx点击打开链接

效果图:

下面是MainActivityde 的部分代码:

这个是程序关键部分,利用ksoap2来解析webservice返回内容:

public static String getRemoteInfo(String phoneSec) throws IOException{
		// 命名空间
        String nameSpace = "http://WebXml.com.cn/";
        // 调用的方法名称
        String methodName = "getMobileCodeInfo";
        // EndPoint
        String endPoint = "http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx";
       // SOAP Action
        String soapAction = "http://WebXml.com.cn/getMobileCodeInfo";
        // 指定WebService的命名空间和调用的方法名
        SoapObject rpc = new SoapObject(nameSpace, methodName);
       // 设置需调用WebService接口需要传入的两个参数mobileCode、userId
        rpc.addProperty("mobileCode", phoneSec);
        rpc.addProperty("userId", "");
        // 生成调用WebService方法的SOAP请求信息,并指定SOAP的版本
        SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
        envelope.bodyOut = rpc;
        // 设置是否调用的是dotNet开发的WebService
        envelope.dotNet = true;
        // 等价于envelope.bodyOut = rpc;
        envelope.setOutputSoapObject(rpc);
        HttpTransportSE transport = new HttpTransportSE(endPoint);
        
        try {
               // 调用WebService
                transport.call(soapAction, envelope);
                
        } catch (Exception e) {
                e.printStackTrace();
              
        }
        // 获取返回的数据
        SoapObject object = (SoapObject) envelope.bodyIn;
        // 获取返回的结果
        String result = object.getProperty(0).toString();
        // 将WebService返回的结果显示在TextView中
        return result;
	}

注意:在安卓4.0以上主线程不允许访问网络,必须开启一个线程!

线程之间传参采用了Handler机制,这样能从线程中实时返回数据。(若是设置全局变量来传参必须得等到子线程终止才能返回结果)

Handler处理消息(子线程通常利用Handler来更新UI):

<span style="font-size:14px;">    private Handler handler = new Handler(){
		public void handleMessage(Message msg) {
             // TODO 接收消息并且去更新UI线程上的控件内容
             if(msg.what==SUCCESS) {
            	 result=String.valueOf(msg.obj);
            	 resultView.setText(result);
            	 progressDialog1.dismiss();
            	 timer.cancel();
             }
             else if(msg.what==TIME_OUT){
            	 thread.interrupt();
            	 progressDialog1.dismiss();
            	 Toast.makeText(getApplicationContext(), "请求超时!请重新查询!", Toast.LENGTH_SHORT).show();
             }
             super.handleMessage(msg);
         }
    };</span>
其中我开启了个时钟计时子线程操作时间,避免操作过长一直停留在progressDialog:

<span style="font-size:14px;color:#660000;">timer = new Timer(); 
 						timer.schedule(new TimerTask() { 
 	                         @Override 
 	                         public void run() { 
 	                        	 Message timeOutmsg = new Message(); 
 	                        	 timeOutmsg.what = TIME_OUT;
 			                     handler.sendMessage(timeOutmsg);  
 	                         } 
 	                     }, TIME_LIMIT);	</span><span style="color:#ff6600;font-size:18px;">
        			</span>

还有就是判断手机网络连接状态:

private boolean isOpenNetwork() {
		ConnectivityManager connManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
		if (connManager.getActiveNetworkInfo() != null) {
		return connManager.getActiveNetworkInfo().isAvailable();
		}
		return false;
		}
别忘了在AndroidMainfest里添加:

    <uses-permission Android:name="android.permission.INTERNET" /> 
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

完整代码下载

点击下载



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值