文章目录
本篇博客是Android调用WCF服务的简单实例。
实例
本例以简单的一个创建通知的WCF接口为例讲述Android与WCF的交互。
首先可使用WcfTestClient测试下WCF接口的CreateNotice方法,可参照上篇Java调用WCF的博客。
新建WCFService调用WCF的工具类如下
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091 |
package com.carlos.wcf; import android.os.Handler;import android.os.Message; import org.ksoap2.SoapEnvelope;import org.ksoap2.serialization.SoapObject;import org.ksoap2.serialization.SoapSerializationEnvelope;import org.ksoap2.transport.HttpTransportSE;import org.xmlpull.v1.XmlPullParserException; import java.io.IOException;import java.util.Iterator;import java.util.Map;import java.util.concurrent.ExecutorService;import java.util.concurrent.Executors; * 访问WCF工具类 * Created by carlos on 2015/11/28. */public class { public static final String WCF_URL="";//引号类改为自己的 public static final String NAMESPACE="";//引号类改为自己的 public static final String SOAP="";//引号类改为自己的 private static final ExecutorService executorService= Executors.newFixedThreadPool(3); * 调用WCF方法 * @param methodName * @param properties * @param wcfServiceCallBack */ public static void callWCFService(final String methodName,Map properties, final WCFServiceCallBack wcfServiceCallBack){ // 创建HttpTransportSE对象,传递WCFService服务器地址 final HttpTransportSE httpTransportSE=new HttpTransportSE(WCF_URL); SoapObject soapObject=new SoapObject(NAMESPACE,methodName); if(properties!=null){ for(Iterator<Map.Entry<String,Object>> it=properties.entrySet().iterator();it.hasNext();){ Map.Entry<String,Object> entry=it.next(); soapObject.addProperty(entry.getKey(),entry.getValue()); } } // 实例化SoapSerializationEnvelope,传入WebService的SOAP协议的版本号 final SoapSerializationEnvelope soapEnvelope=new SoapSerializationEnvelope(SoapEnvelope.VER11); soapEnvelope.bodyOut=soapObject; // 设置是否调用的是.Net开发的WCFService soapEnvelope.setOutputSoapObject(soapObject); soapEnvelope.dotNet=true; httpTransportSE.debug=true; // 用于子线程与主线程通信的Handler final Handler mHandler=new Handler(){ public void handleMessage(Message msg) { super.handleMessage(msg); // 将返回值回调到callBack的参数中 wcfServiceCallBack.callBack((SoapObject) msg.obj); } }; // 开启线程去访问WCFService executorService.submit(new Runnable() { public void run() { SoapObject resultSoapObject=null; try { httpTransportSE.call(SOAP+methodName,soapEnvelope); if(soapEnvelope.getResponse()!=null){ resultSoapObject= (SoapObject) soapEnvelope.bodyIn; } } catch (IOException e) { e.printStackTrace(); } catch (XmlPullParserException e) { e.printStackTrace(); }finally { // 将获取的消息利用Handler发送到主线程 mHandler.sendMessage(mHandler.obtainMessage(0,resultSoapObject)); } } }); } * 回调接口 */ public interface WCFServiceCallBack{ public void callBack(SoapObject result); }} |
MainActivity类里面调用该方法打印出返回的WCF结果
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960 |
package com.carlos.wcf; import android.os.Bundle;import android.support.v7.app.AppCompatActivity;import android.util.Log;import android.view.View;import android.widget.Button;import android.widget.Toast; import org.ksoap2.serialization.SoapObject; import java.util.HashMap; public class MainActivity extends AppCompatActivity { private Button b_get_WCFResult; private final static String TAG="WCFResult"; protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); b_get_WCFResult= (Button) findViewById(R.id.b_get_WCFResult); b_get_WCFResult.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { getWCFResult(); } }); } private void getWCFResult(){ HashMap<String,Object> map=new HashMap<>(); map.put("title", "提醒"); map.put("content", "晚上写篇博客"); WCFService.callWCFService("CreateNotice", map, new WCFService.WCFServiceCallBack() { public void callBack(SoapObject result) { if(result!=null){ Toast.makeText(MainActivity.this,result.toString(),Toast.LENGTH_SHORT).show(); Log.i(TAG, result.toString()); SoapObject soapObject=(SoapObject)result.getProperty("result"); Log.i(TAG,soapObject.toString()); if(soapObject.toString().equals("anyType{}")){ Log.i(TAG,"没有结果"); }else{ for(int i=0;i<soapObject.getPropertyCount();i++){ Log.i("WCFResult",soapObject.getProperty(i).toString()); } } } } }); }} |
点击获取WCF结果的按钮后控制台输出的结果
原文地址:https://www.cnblogs.com/petewell/p/11607171.html
时间: 2024-10-13 22:32:42