有些时候由于项目的需要客户端用Android实现,服务器用.NET来实现,那么Android客户端怎样连接服务器呢?
当然是通过WCF来连接,今天我们就来讲讲怎样连接WCF.
工具
WCF测试工具
在进行Android客户端开发的时候,每次调用接口需要WCF测试工具去测试,因为你不知道接口是不是正常的,
以及返回信息的格式,通过WCF测试工具就可以很轻松的测试接口以及知道返回结果信息,一般WCF返回的都是XML,
可以根据返回的XML去解析,取出你所需要的数据。
WCF测试工具的下载地址
WCF测试工具
ksoap2jar包下载
我们需要下载个jar包,里面封装了很多方法
ksoap2jar包
Android客户端调用WCF
封装工具类
我们可以把调用WCF接口封装成一个一个工具类,当需要你们需要调用WCF接口,可以直接拿来用。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
| package com.example.tomorrow.mobileoa.utils;
import android.os.Handler; import android.os.Message; import android.util.Log;
import org.apache.http.client.HttpResponseException; 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;
public class WebServiceUtils {
//服务器IP地址,也可以是域名 public static final String WEB_SERVER_IP = "218.75.131.83:8012"; public static final String WEB_SERVER_URL = "http://" + WEB_SERVER_IP + "/SystemService.svc"; //命名空间 private static final String NAMESPACE = "http://www.mt178.com"; public static final String SOAP = "http://www.mt178.com/SystemService/"; // 含有5个线程的线程池 private static final ExecutorService executorService = Executors .newFixedThreadPool(5);
/** * @param url * @param methodName * @param properties * @param webServiceCallBack */ public static void callWebService(String url, final String methodName, Map properties, final WebServiceCallBack webServiceCallBack) { final HttpTransportSE httpTransportSE = new HttpTransportSE(url, 30 * 1000); 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()); } } final SoapSerializationEnvelope soapEnvelope = new SoapSerializationEnvelope( SoapEnvelope.VER11); soapEnvelope.setOutputSoapObject(soapObject); soapEnvelope.dotNet = true; httpTransportSE.debug = true; final Handler mHandler = new Handler() { @Override public void handleMessage(Message msg) { super.handleMessage(msg); webServiceCallBack.callBack((SoapObject) msg.obj); }
};
// 开启线程去访问WebService executorService.submit(new Runnable() { @Override public void run() { SoapObject resultSoapObject = null; try { httpTransportSE.call(SOAP + methodName, soapEnvelope); System.out.print(soapEnvelope.getResponse().toString()); if (soapEnvelope.getResponse() != null) { resultSoapObject = (SoapObject) soapEnvelope.bodyIn; } } catch (HttpResponseException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } catch (XmlPullParserException e) { e.printStackTrace(); } finally { mHandler.sendMessage(mHandler.obtainMessage(0, resultSoapObject)); } } }); }
public interface WebServiceCallBack { void callBack(SoapObject result); } }
|
调用WCF
前面已经封装了访问WCF接口的工具类,现在就来写怎样调用工具类
这里是调用WCF服务器的LoginAccount()方法:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| //map中存放的参数一定是序列化,否则调用WCF就回报错,我这里提供一种解决方法。 Map map = Collections.synchronizedMap(new LinkedHashMap()); //这里的key根据WCF测试工具测的参数名来填,又几个参数就添加几个 map.put(key, value);
WebServiceUtils.callWebService(WebServiceUtils.WEB_SERVER_URL, "LoginAccount", map, new WebServiceUtils.WebServiceCallBack() { @Override public void callBack(SoapObject result) { //判断服务器返回的消息 if (result != null) { //这里是根据服务器返回的XML信息进行解析 ........ } else { Log.i(TAG,"服务器返回消息为空"); } } });
|
到这为此,已经基本学会Android怎样调用WCF方法,具体的代码实现还要根据服务器返回的结果来写。
欢迎大家关注我的微信公众号,我会不定期的分享些Android开发的技巧