[セットトップ]android呼び出し.Netのwebserviceの例携帯電話の帰属地を取得する


package com.example.usewebservice;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.annotation.SuppressLint;
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;
import android.widget.Toast;

public class MainActivity extends Activity
{
	/** 
	 * 
	 * Android    WebService(         )
	 * @author yejianping  
	 * @date 2014-4-3 
	 *    ksoap2-android-assembly-2.5.4-jar-with-dependencies.jar      lib  
	 *  
	 **/
	public EditText text ;
	public Button button;
	public TextView tx;
	public String telephone_number;
	public MyThread thread;
	public Handler handler; 
	
	@SuppressLint("HandlerLeak")
	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		text = (EditText)findViewById(R.id.editText1);
		button = (Button)findViewById(R.id.button1);
		thread = new MyThread();
		button.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v)
			{
				// TODO           
			 telephone_number = text.getText().toString().trim();
				if(telephone_number.equals("")||telephone_number.length()<7)
				{
					text.setError("        ( )  !");
					text.requestFocus();
				}
				else
				{
					new Thread(thread).start();;
				}
			}
		});
		
		handler = new Handler() {
			@SuppressLint("HandlerLeak")
			public void handleMessage(Message msg) {
				switch (msg.what) {
				case 0x01:
					Bundle bundle = new Bundle();
	    			bundle = msg.getData();
	    			Toast.makeText(MainActivity.this, bundle.getString("result"), Toast.LENGTH_SHORT).show();
				}
			}
		};
		
	}
	
	
	//    
    public  class MyThread implements Runnable
    {
	   	public void run() 
	   	{	
      		Looper.prepare();//              
      		getTelephoneInfo(telephone_number);
      		Looper.loop();//        
	   	}
     }
	
	
	
	public void getTelephoneInfo(String phone_number)
	{
		//    
		String nameSpace = "http://WebXml.com.cn/";
		//       
		String methodName = "getMobileCodeInfo";
		// webservice   
		String URL = "http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx";
		//    +  
		String soapAction = "http://WebXml.com.cn/getMobileCodeInfo";
		//   WebService            
		SoapObject rpc = new SoapObject(nameSpace, methodName);
		//      WebService           mobileCode、userId
		rpc.addProperty("mobileCode", phone_number);
		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(URL);
		try 
		{
			//   WebService
			transport.call(soapAction, envelope);
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
		 
		//        
		SoapObject object = (SoapObject) envelope.bodyIn;
		//        
		String result = object.getProperty("getMobileCodeInfoResult").toString();
		Message msg=new Message();	
		Bundle bundle = new Bundle();
		bundle.putString("result", result);
		msg.setData(bundle);
		msg.what = 0x01;
		handler.handleMessage(msg);
		//return result;
		//  WebService        TextView 
		//tx.setText(result);
	}

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

}
 
 
 
 

xml ;

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:background="@drawable/dd"
    android:layout_height="fill_parent" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="24dp"
        android:text="         "
        android:textSize="30sp"
        android:textStyle="bold" />

    <EditText
        android:id="@+id/editText1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_below="@+id/textView1"
        android:layout_marginTop="23dp"
        android:phoneNumber="true"
        android:hint="            7 "
        android:ems="10" />

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        android:text="  " />

 

</RelativeLayout>

ソースのダウンロード