码迷,mamicode.com
首页 > 移动开发 > 详细

Android作为CXF客户端调用服务端。

时间:2020-07-04 09:14:18      阅读:90      评论:0      收藏:0      [点我收藏+]

标签:eth   view   tran   服务   协议   cat   客户端   property   service   

1 加载jar

下载jar包,放在libs下
技术图片

通过Project Structure添加jar依赖
技术图片

成功后就会在build.gradle下添加
技术图片

代码

    final static String SERVICE_NS = "http://ws.service.mService.et.cn/";
    final static String SERVICE_URL = "http://192.168.9.59:8080/DSer/service/SysService?WSDL";
    private EditText txt1;
    private EditText txt2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        //注意下面两行
        StrictMode.ThreadPolicy policy= new StrictMode.ThreadPolicy.Builder().permitAll().build();
        StrictMode.setThreadPolicy(policy);

        setContentView(R.layout.activity_cxf);
        Button button = findViewById(R.id.cxf_button1);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //Toast.makeText(CxfActivity.this,"ceshi",Toast.LENGTH_LONG).show();
                getCxf();
            }
        });
        txt1 = findViewById(R.id.cxf_EditText1);
        txt2 = findViewById(R.id.cxf_EditText2);

    }

    public void getCxf() {
        //调用的方法
        String methodName = "getEtUser";
        //创建httpTransportSE传输对象
        HttpTransportSE ht = new HttpTransportSE(SERVICE_URL);
        ht.debug = true;
        //使用soap1.1协议创建Envelop对象
        SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
        //实例化SoapObject对象
        SoapObject request = new SoapObject(SERVICE_NS, methodName);
        /**
         * 设置参数,参数名不一定需要跟调用的服务器端的参数名相同,只需要对应的顺序相同即可
         * */
        request.addProperty("userName", "000");
        //将SoapObject对象设置为SoapSerializationEnvelope对象的传出SOAP消息
        envelope.bodyOut = request;
        try{
            //调用webService
            ht.call(null, envelope);
            //txt1.setText("看看"+envelope.getResponse());
            if(envelope.getResponse() != null){
                txt2.setText("有返回");
                SoapObject result = (SoapObject) envelope.bodyIn;
                SoapObject soap = (SoapObject) result.getProperty(0); //这个例子是返回一个对象。
                String name =soap.getProperty("name").toString();
                String userName =soap.getProperty("userName").toString();
                String department =soap.getProperty("department").toString();
                txt1.setText("返回值 = "+ name+ " "+userName + " " +department);
            }else{
                txt2.setText("无返回");
            }
        }catch (Exception e) {
            e.printStackTrace();
        }
    }

AndroidManifest.xml

AndroidManifest.xml下添加

<uses-permission android:name="android.permission.INTERNET" />

Android作为CXF客户端调用服务端。

标签:eth   view   tran   服务   协议   cat   客户端   property   service   

原文地址:https://blog.51cto.com/470462/2508290

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!