android调用wcf
主要代码是我抄袭别人的,中国人学习就是这样,抄来抄去就会了,呵呵,不过我还是支持原版,下面是原版的链接
http://blog.csdn.net/xiaanming/article/details/16871117
1.第一步,复制工具类(创建一个WebServiceUtils.java)
//直接复制代码,稍微改几个地方就能用(红色加粗部分)
//WebServiceUtils.java
1 package com.wang.oaos; 2 3 import android.os.Handler; 4 import android.os.Message; 5 6 import org.apache.http.client.HttpResponseException; 7 import org.ksoap2.SoapEnvelope; 8 import org.ksoap2.serialization.SoapObject; 9 import org.ksoap2.serialization.SoapSerializationEnvelope; 10 import org.ksoap2.transport.HttpTransportSE; 11 import org.xmlpull.v1.XmlPullParserException; 12 13 import java.io.IOException; 14 import java.util.HashMap; 15 import java.util.Iterator; 16 import java.util.Map; 17 import java.util.concurrent.ExecutorService; 18 import java.util.concurrent.Executors; 19 20 21 /** 22 * Created by Administrator on 2015/11/5. 23 */ 24 public class WebServiceUtils { 25 public static final String WEB_SERVER_URL="http://172.16.201.210/Helloservice.svc"; 26 //含有三个线程的线程池 27 private static final ExecutorService executorService= Executors.newFixedThreadPool(3); 28 //命名空间 29 private static final String NAMESPACE="http://172.16.201.210:80/"; 30 public static final String SOAP_ACTION="http://172.16.201.210:80/HelloService/HelloWord";
//改成你自己的 31 /** 32 * 33 * @param url 34 * WebService服务器地址 35 * @param methodName 36 * WebService的调用方法名 37 * @param properties 38 * WebService的参数 39 * @param webServiceCallBack 40 * 回调接口 41 */ 42 public static void callWebService(String url,final String methodName, 43 HashMap<String,String> properties, 44 final WebServiceCallBack webServiceCallBack){ 45 //创建HttpTransportSE对象,传递WebService服务器地址 46 final HttpTransportSE httpTransportSE=new HttpTransportSE(url); 47 //创建SoapObject对象 48 SoapObject soapObject=new SoapObject(NAMESPACE,methodName); 49 //SoapObject添加参数 50 if(properties!=null){ 51 for(Iterator<Map.Entry<String,String>>it=properties.entrySet() 52 .iterator();it.hasNext();){ 53 Map.Entry<String,String>entry=it.next(); 54 soapObject.addProperty(entry.getKey(),entry.getValue()); 55 } 56 } 57 //实例化SoapSerializationEnvelope,传入WebService的SOAP协议的版本号 58 final SoapSerializationEnvelope soapSerializationEnvelope = new SoapSerializationEnvelope(SoapEnvelope.VER11); 59 //设置是否调用的是.net开发的WebService 60 soapSerializationEnvelope.setOutputSoapObject(soapObject); 61 soapSerializationEnvelope.dotNet=true; 62 httpTransportSE.debug=true; 63 64 //用于子线程与主线程通信的Handler 65 final Handler mHandler=new Handler() { 66 @Override 67 public void handleMessage(Message msg){ 68 super.handleMessage(msg); 69 webServiceCallBack.callBack((SoapObject)msg.obj); 70 } 71 }; 72 73 //开启线程去访问WebService 74 executorService.submit(new Runnable() { 75 @Override 76 public void run() { 77 SoapObject resultSoapObject=null; 78 try{ 79 httpTransportSE.call(SOAP_ACTION,soapSerializationEnvelope); 80 if(soapSerializationEnvelope.getResponse()!=null){ 81 //获取服务器响应返回的SoapObject 82 resultSoapObject=(SoapObject) soapSerializationEnvelope.bodyIn; 83 } 84 }catch (HttpResponseException e){ 85 e.printStackTrace(); 86 }catch (IOException e){ 87 e.printStackTrace(); 88 }catch (XmlPullParserException e){ 89 e.printStackTrace(); 90 }finally { 91 //将获取的消息利用Handler发送的主线程 92 mHandler.sendMessage(mHandler.obtainMessage(0,resultSoapObject)); 93 } 94 } 95 }); 96 } 97 98 public interface WebServiceCallBack{ 99 public void callBack(SoapObject result); 100 } 101 }
2.将包ksoap2-android-assembly-2.5.2-jar-with-dependencies.jar导入到libs
3.测试,写一个MainActivity.java,xml中只需要定义一个button,id="@+id/bt"
package com.wang.oaos; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.Toast; import android.widget.ViewFlipper; import org.ksoap2.serialization.SoapObject; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); final int interval = 5000; ViewFlipper flipper = (ViewFlipper) findViewById(R.id.flipper); flipper.setFlipInterval(interval);
Button button = (Button) findViewById(R.id.bt); button.setOnClickListener(new View.OnClickListener(){ @Override public void onClick(View v) { WebServiceUtils.callWebService(WebServiceUtils.WEB_SERVER_URL, "HelloWord", null, new WebServiceUtils.WebServiceCallBack() { @Override public void callBack(SoapObject result) { if (result != null) { Toast.makeText(MainActivity.this, "返回成功" + result.getProperty(0), Toast.LENGTH_SHORT).show(); } else { Toast.makeText(MainActivity.this, "失败", Toast.LENGTH_SHORT).show(); } } }); } }); } }
最后一步加网络协议(修改AndroidManifest.xml,在后面加上<uses-permission android:name="android.permission.INTERNET"/>)
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.wang.oaos" > <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name=".MainActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> <uses-permission android:name="android.permission.INTERNET"/> </manifest>