Android中使用Soap协议简单实例

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <LinearLayout
        android:id="@+id/query_linerLayout"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <EditText
            android:id="@+id/phone_edittext"
            android:layout_width="180dp"
            android:layout_height="45dp"
            android:hint="请输入11位手机号"
            android:inputType="phone"
            android:maxLength="11" />

        <Button
            android:id="@+id/query_location_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp"
            android:text="查询归属地" />
    </LinearLayout>

第三步:编写Activity处理文件

activity的文件名为:HelloAndroidSoapActivity.java

package cn.dennishucd;

import java.io.IOException;
import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpResponseException;
import org.ksoap2.transport.HttpTransportSE;
import org.xmlpull.v1.XmlPullParserException;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class HelloAndroidSoapActivity extends Activity {
    
    @Override  
    protected void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);  
        setContentView(R.layout.main);  
        
        final EditText phoneEditText = (EditText)findViewById(R.id.phone_edittext);
        final TextView resultTextView = (TextView)findViewById(R.id.query_result_textview);
        
        Button queryBtn = (Button)findViewById(R.id.query_location_button);
        queryBtn.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View view) {
				String msg = null;
				String phone = phoneEditText.getText().toString();
				
				if (phone.length() < 11) {
					msg = "手机号码格式不对,请重新输入!";
				}
				
				msg = queryPhoneLocation(phone);
				
				if (msg == null) {
					msg = "查询服务器出错!";
				}
				
				resultTextView.setText(msg);
			}
		});
        
    }
    
    private String queryPhoneLocation(String phone) {
    	String nameSpace = "http://WebXml.com.cn/";
        String methodName = "getMobileCodeInfo";
        SoapObject soapObject = new SoapObject(nameSpace, methodName);
        
        soapObject.addProperty("mobileCode", phone);
        soapObject.addProperty("userID", "");
        
        SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11); 
        
        envelope.dotNet = true; //very important for compatibility
        envelope.bodyOut = soapObject;
        
        String url = "http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx";
        HttpTransportSE htSE = new HttpTransportSE(url);
        
        Object response = null;
        
        try {
        	String soapAction = "http://WebXml.com.cn/getMobileCodeInfo";
			htSE.call(soapAction, envelope);
			
			response = envelope.getResponse();
			
		} catch (HttpResponseException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (XmlPullParserException e) {
			e.printStackTrace();
		}
        
        
        if (response != null) {
        	Log.d("HelloAndroidSoap", "The invoke result is: " + response.toString());
        	
        	return response.toString();
        }
        else {
        	return null;
        }
    }
}
第四步:运行测试


猜你喜欢

转载自blog.csdn.net/zly19931101/article/details/54861666