设为首页 加入收藏

TOP

Android调用Web服务(二)
2017-10-13 10:12:13 】 浏览:10055
Tags:Android 调用 Web 服务
.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context="com.example.soapprousage.MainActivity" > <Button android:id="@+id/btn_jlx" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="基类型调用"/> <Button android:id="@+id/btn_obj" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="对象调用"/> <TextView android:id="@+id/lbl_result" android:layout_width="match_parent" android:layout_height="match_parent" android:textAlignment="viewStart"/>" </LinearLayout>

2.2 利用Ksoap2调用wcf服务

首先把下载下来的Jar格式的Ksoap包复制到libs(自己创建)文件夹下。

声明服务调用需要的地址和方法

private String nameSpace="http://www.juame.edu";//和wcf服务契约特性的Namespace是一样的
private String url="http://172.21.212.54:8888/TestService.svc";//svc服务地址
private String soapAction="http://www.juame.edu/JuameService/Add";//操作地址
private String methodName="Add";//方法名称

上面声明的服务地址、命名空间、操作地址和方法名称都可以从服务的wsdl文档中查看,

2016_10_bab1b9f2-cb82-4092-a3e3-e3fc133b4186

下面利用ksoap2对服务进行调用的代码如下。

protected SoapObject getSoapResult(int op1,int op2){
    SoapObject outObject=new SoapObject(nameSpace,methodName);
    //添加输出参数
    outObject.addProperty("op1", op1);
    outObject.addProperty("op2",op2);
    
    SoapSerializationEnvelope serializationEnvelope=new SoapSerializationEnvelope(SoapEnvelope.VER11);//设置soap版本
    
    serializationEnvelope.bodyOut=outObject;
    serializationEnvelope.dotNet=true;//调用.NET的服务
    
    HttpTransportSE transportSE=new HttpTransportSE(url);
    transportSE.debug=true;//采用调试

    try{
        transportSE.call(soapAction, serializationEnvelope);//调用服务
        SoapObject result=(SoapObject)serializationEnvelope.bodyIn;//获取调用结果
        Log.v("happy1", "服务调用成功");
      //把结果封送到消息中去,让ui线程显示
        Bundle bundle=new Bundle();
        bundle.putString("result", result.getProperty(0).toString());//获取结果中的值
        Message message=new Message();
        message.setData(bundle);
        message.what=12;
        hander.sendMessage(message);
        return result;
        
    }catch(IOException ex){
        Log.v("sad", "IO异常");
        ex.printStackTrace();
    }catch(XmlPullParserException ex){
        Log.v("sad", "xml解析异常");
        ex.printStackTrace();
        
    }catch(Exception ex){
        Log.v("sad", "服务调用异常异常");
    }   
    return null;
}

按钮事件代码,采用多线程。在android3.0后,有关网络资源的调用代码都不能直接在主UI线程中调用,否则会出现android.os.NetworkOnMainThreadException异常。关于android中的多线程机制有时间再进行总结。

//绑定按钮事件
btnJlx.setOnClickListener(new OnClickListener() {
    
    @Override
    public void onClick(View arg0) {
        // TODO Auto-generated method stub
        Thread thread=new Thread(getSoapRequest);
        thread.start();
    }
});
//线程
Runnable getSoapRequest=new Runnable() {
    
    @Override
    public void run() {
        // TODO Auto-generated method stub
        getSoapResult(10, 20);
    }
};
//消息处理
Handler hander = new Handler() {
    @Override
    public void handleMessage(Message msg) {
        i
首页 上一页 1 2 3 4 下一页 尾页 2/4/4
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇WebView的使用及添加进度条 下一篇React-Native 之 FlexBox介绍和使..

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目