android通过ksoap2调用java写的CXF 的webservice

如何添加ksoap2就不多说了,直接代码。

1.webservice:对应地址为http://192.168.1.119:8081/CXFTest/userService?wsdl

android通过ksoap2调用java写的CXF 的webservice_第1张图片

2.Activity中的代码

package wolf.cxf;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class AndroidCXFActivity extends Activity {
    /** Called when the activity is first created. */
	
	
	private EditText phoneSecEditText;  
    private TextView resultView;  
    private Button queryButton;  
	
	
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
    	super.onCreate(savedInstanceState);  
        setContentView(R.layout.main);  
  
        phoneSecEditText = (EditText) findViewById(R.id.phone_sec);  
        resultView = (TextView) findViewById(R.id.result_text);  
        queryButton = (Button) findViewById(R.id.query_btn);  

        queryButton.setOnClickListener(new View.OnClickListener() {
        	@Override  
            public void onClick(View v) {  
                // 姓名(段)  
                String phoneSec = phoneSecEditText.getText().toString().trim();  
                // 简单判断用户输入的姓名是否合法  
                if ("".equals(phoneSec)) {  
                    // 给出错误提示  
                    phoneSecEditText.setError("您输入的姓名有误!");  
                    phoneSecEditText.requestFocus();  
                    // 将显示查询结果的TextView清空  
                    resultView.setText("");  
                    return;  
                }  
                // 查询姓名信息  
                getRemoteInfo(phoneSec);  
            }  
		});
			
			
    }
    
    public void getRemoteInfo(String phoneSec) {  
    	
        // 命名空间  
        String nameSpace = "http://service.wolf.com/";  
        // 调用的方法名称  
        String methodName = "getUserInfoByName";  
        // EndPoint  
        String endPoint = "http://192.168.1.119:8081/CXFTest/userService?wsdl";  
  
        // 指定WebService的命名空间和调用的方法名  
        SoapObject rpc = new SoapObject(nameSpace, methodName);  
       
        // 设置需调用WebService接口需要传入的两个参数mobileCode、userId  
        rpc.addProperty("uname", phoneSec);  
  
        // 生成调用WebService方法的SOAP请求信息,并指定SOAP的版本  
        SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER10);  
        
        envelope.bodyOut = rpc;  
        envelope.setOutputSoapObject(rpc);  
  
        HttpTransportSE transport = new HttpTransportSE(endPoint);  
        try {  
            // 调用WebService  
            transport.call(null, envelope);
            if(envelope.getResponse()!=null){
            	  // 获取返回的数据  
                SoapObject object = (SoapObject) envelope.getResponse(); 
                
                String result = object.getProperty("userName").toString();  
                
                // 将WebService返回的结果显示在TextView中  
                resultView.setText(result);  
            }
        } catch (Exception e) {  
            e.printStackTrace(); 
           
        }  
  
       
        
    }  

}
3,结果显示

android通过ksoap2调用java写的CXF 的webservice_第2张图片

4.注意点****:在AndroidManifest.xml中添加<uses-permission android:name="android.permission.INTERNET" />


你可能感兴趣的:(java,android,webservice,String,SOAP,button)