Android調用webservice 接口

查詢手機號碼歸屬地的Web service爲例,它的wsdl爲java

http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx?wsdlandroid

1 在Android項目中導入Ksoap2-android jar第三方jar包web

2 Activity代碼ide

public class SecondActivity extends Activity {

	private EditText phoneSecEditText;  
    private TextView resultView;  
    private Button queryButton;  
  
    @Override  
    public void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);  
        setContentView(R.layout.activity_second);  
  
        phoneSecEditText = (EditText) findViewById(R.id.phone_sec);  
        resultView = (TextView) findViewById(R.id.result_text);  
        queryButton = (Button) findViewById(R.id.query_btn);  
  
        queryButton.setOnClickListener(new OnClickListener() {  
            @Override  
            public void onClick(View v) {  
                // 手機號碼(段)  
                String phoneSec = phoneSecEditText.getText().toString().trim();  
                // 簡單判斷用戶輸入的手機號碼(段)是否合法  
                if ("".equals(phoneSec) || phoneSec.length() < 7) {  
                    // 給出錯誤提示  
                    phoneSecEditText.setError("您輸入的手機號碼(段)有誤!");  
                    phoneSecEditText.requestFocus();  
                    // 將顯示查詢結果的TextView清空  
                    resultView.setText("");  
                    return;  
                }  
                // 查詢手機號碼(段)信息  
//                getRemoteInfo(phoneSec);  
                new Mys().execute(phoneSec) ;
            }  
        });  
    }  
  
    /** 
     * 手機號段歸屬地查詢 
     *  
     * @param phoneSec 手機號段 
     */  
    public String getRemoteInfo(String phoneSec) {  
        // 命名空間  
        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.VER10);  
  
        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中  
//        resultView.setText(result);  
        return result ;
    }  
    
   class Mys extends AsyncTask<String, Void, String>{

	@Override
	protected String doInBackground(String... params) {
		return getRemoteInfo(params[0]) ;
	}
	
	@Override
	protected void onPostExecute(String result) {
		resultView.setText(result);  
		super.onPostExecute(result);
	}
	   
   }

}

其中,spa

// 命名空間  
        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";

這些值分別爲.net

    命名空間、調用的方法名稱、EndPoint和SOAP Actioncode

    EndPoint 一般是將WSDL地址末尾的"?wsdl"去除後剩餘的部分xml

    SOAP Action一般爲命名空間 + 調用的方法名稱。    blog

如圖:接口


參考:http://blog.csdn.net/lyq8479/article/details/6428288/

相關文章
相關標籤/搜索