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>


原文地址:https://www.cnblogs.com/emoji/p/4436828.html