zoukankan      html  css  js  c++  java
  • webservice学习笔记(查询手机归属地)

    获取号码归属地,感谢code-pig童鞋的教程,再说一句安卓5.0的默认EditText好漂亮。



    package com.example.phonenum;
    
    import org.ksoap2.SoapEnvelope;
    import org.ksoap2.serialization.SoapObject;
    import org.ksoap2.serialization.SoapSerializationEnvelope;
    import org.ksoap2.transport.HttpTransportSE;
    import org.ksoap2.transport.HttpsTransportSE;
    import android.app.Activity;
    import android.os.Bundle;
    import android.os.Handler;
    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 {
    
      //定义获取手机信息的SoapAction与命名空间,作为常量
    	static final String AddressnameSpace = "http://WebXml.com.cn/";  
        static final String Addressurl = "http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx";  
        static final String Addressmethod = "getMobileCodeInfo";  
        static final String AddresssoapAction = "http://WebXml.com.cn/getMobileCodeInfo";
       //定义相关控件Id
        private EditText phoneNum ;
        private Button ok ;
        private TextView text ;
        private String result ;
        
        private Handler handler = new Handler() {
        	public void handleMessage(android.os.Message msg) {
        		if(msg.what == 0x123) {
        			System.out.println("归属地查询成功") ;
        			text.setText(result);
        		}
        	};
        } ;
    	@Override
    	protected void onCreate(Bundle savedInstanceState) {
    		super.onCreate(savedInstanceState);
    		setContentView(R.layout.activity_main);
    		this.phoneNum = (EditText)findViewById(R.id.phonenum) ;
    		this.text = (TextView) findViewById(R.id.text) ;
    		this.ok = (Button) findViewById(R.id.ok) ;
    		this.ok.setOnClickListener(new OnClickListener() {
    			
    			@Override
    			public void onClick(View v) {
    				// TODO Auto-generated method stub
    				new Thread() {
    					@Override
    					public void run() {
    						// TODO Auto-generated method stub
    						getAddress() ;
    					}
    
    				}.start();
    			}
    		});
    	}
    	void getAddress() {
    		SoapObject soapObject = new SoapObject(AddressnameSpace, Addressmethod) ;
    		soapObject.addProperty("mobileCode",this.phoneNum.getText().toString()) ;
    		soapObject.addProperty("userid","") ;
    		SoapSerializationEnvelope envelop = new SoapSerializationEnvelope(SoapEnvelope.VER11) ;
    		envelop.bodyOut = soapObject ;
    		envelop.dotNet = true ;
    		envelop.setOutputSoapObject(soapObject);
    		HttpTransportSE httpTransportSE = new HttpTransportSE(Addressurl);  
    		System.out.println("号码设置完毕,马上开启查询服务");
    		try {
    			httpTransportSE.call(AddresssoapAction, envelop);
    			System.out.println("服务调用成功");
    		}catch(Exception e) {
    			e.printStackTrace();
    			System.out.println("服务调用失败");
    		}
    		SoapObject object = (SoapObject)envelop.bodyIn ;
    		result = object.getProperty(0).toString() ;
    		handler.sendEmptyMessage(0x123) ;
    		System.out.println("查询成功") ;
    		
    	}
    
    }
    


    布局

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        tools:context="com.example.phonenum.MainActivity" >
    
       <EditText 
           android:id="@+id/phonenum"
           android:layout_width="fill_parent"
           android:layout_height="wrap_content"
           android:inputType="number"/>
       
       <Button 
           android:id="@+id/ok"
           android:layout_marginTop="10dp"
           android:layout_width="fill_parent"
           android:text="查询"
           android:layout_height="wrap_content"/>
       
       <TextView 
           android:id="@+id/text"
           android:layout_width="fill_parent"
           android:layout_height="wrap_content"/>
       
       
       
    
    </LinearLayout>
    


  • 相关阅读:
    LeetCode数学系列(3)——快速幂算法(50题)
    LeetCode树系列(3)——200题岛屿数量
    Arrays.sort()详解
    图表示学习系列(1)——GCN学习笔记:第一部分,详细讲解GCN
    LeetCode动态规划系列(3)——编辑距离问题求解
    深度学习系列(9)——node2vec算法中的alias采样介绍
    LeetCode数学系列(2)——解决约瑟夫问题
    Java数据结构系列(4)——队列常用方法
    LeetCode树系列(1)——广度搜索应用,图的BFS
    LeetCode树系列(2)——深度搜索运用:LeetCode695题详解
  • 原文地址:https://www.cnblogs.com/emoji/p/4436828.html
Copyright © 2011-2022 走看看