學習自:html
http://www.cnblogs.com/kissazi2/p/3406662.htmljava
Win10android
VS 2015web
Android Studio 2.2.3json
KSOAP2的Jar包c#
衆所周知,符合W3C標準的,HTTP協議、SOAP 協議等,是沒有語言和平臺的限制的。由於實際須要咱們可能有時候要調用C#編寫的WebService,這裏咱們須要添加一個Jar包的依賴。網絡
KSOAP2點擊下載ide
咱們的WebService,創建之後,這麼一個地址用來存放咱們WebService的一些描述的信息。
http://localhost:54603/NBAWebService.asmx?wsdl
學習須要瞭解的信息this
- WebService 命名空間
- 要執行的方法名
- SOAPAction
- SOAP 的版本.
Note:
- 由於涉及到了網絡,必定要在子線程中使用。
- 訪問網絡須要加上網絡權限。
- 傳遞參數的時候,參數的名稱和順序不能亂掉。
咱們將如下面的HelloWorld方法爲例子
namespace NBAWebService { /// <summary> /// Summary description for NBAWebService /// </summary> [WebService(Namespace = "http://tempuri.org/")] [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] [System.ComponentModel.ToolboxItem(false)] // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. // [System.Web.Script.Services.ScriptService] public class NBAWebService : System.Web.Services.WebService { public MyLinqDataContext mldc = MethodHelper.GetLinq(); [WebMethod] public string HelloWorld() { return "Hello World"; } //**************************** } }
/* * 用來調用C#的WebService * */ public class ConnectionWebService { //WebService的地址 //10.0.2.2 該地址是咱們,android模擬器訪問本機時使用的IP地址 static final String URI = "http://10.0.2.2:8088/NBAWebService.asmx"; //WebService 的命名空間 static final String NAMESPACE = "http://tempuri.org/"; /* * 調用WebService的方法 * */ public static String callMethod(String methodName, Map<String, String> parameterMap) throws IOException, XmlPullParserException { /* * SOAP的對象 * namespace: 調用的命名空間 * method name = 調用的方法名 * */ SoapObject soapObject = new SoapObject(NAMESPACE, methodName); if (parameterMap != null) { //給SOAP對象傳入咱們須要的參數 for (Map.Entry<String, String> item : parameterMap.entrySet()) { soapObject.addProperty(item.getKey(), item.getValue()); } } //設置咱們的 WebService的SOAP版本 SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER12); envelope.bodyOut = soapObject; //與.net兼容 envelope.dotNet = true; //調用方法 HttpTransportSE httpTransportSE = new HttpTransportSE(URI); httpTransportSE.call(NAMESPACE + "/" + methodName, envelope); //獲取到返回值 SoapPrimitive sp = (SoapPrimitive) envelope.getResponse(); return sp.toString(); } }
這是咱們調用這個WebService是使用的基本上不會改變的參數
//WebService的地址 //10.0.2.2 該地址是咱們,android模擬器訪問本機時使用的IP地址 static final String URI = "http://10.0.2.2:8088/NBAWebService.asmx"; //WebService 的命名空間 static final String NAMESPACE = "http://tempuri.org/";
咱們方法傳遞的參數
- methodName 須要調用的WebService的方法名
- parameterMap 方法所須要的參數 格式爲 key = 參數的名稱 value = 參數的值
public static String callMethod(String methodName, Map<String, String> parameterMap) throws IOException, XmlPullParserException
指定WebService命名控件和須要調用的方法
/* * SOAP的對象 * namespace: 調用的命名空間 * method name = 調用的方法名 * */ SoapObject soapObject = new SoapObject(NAMESPACE, methodName);
添加參數, parameterMap 爲null 那麼就代表該方法不須要參數。
if (parameterMap != null) { //給SOAP對象傳入咱們須要的參數 for (Map.Entry<String, String> item : parameterMap.entrySet()) { soapObject.addProperty(item.getKey(), item.getValue()); } }
SOAP的請求信息,該信息是由SoapSerializationEnvelope 對象描述的。
//設置咱們的 WebService的SOAP版本 SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER12); envelope.bodyOut = soapObject; //與.net兼容 envelope.dotNet = true;
建立HTTPTransportsSE 對象,構造參數中傳遞,WebService的URL
call 方法調用咱們的WebService方法, 傳遞咱們的SOAPAction和咱們的SoapSerializationEnvelope 對象
通常來講 SOAPAction 是 命名空間+"/"+方法名.
//設置咱們的 WebService的URI //調用方法 HttpTransportSE httpTransportSE = new HttpTransportSE(URI); httpTransportSE.call(NAMESPACE + "/" + methodName, envelope);
獲取到返回值。
//獲取到返回值 SoapPrimitive sp = (SoapPrimitive) envelope.getResponse(); return sp.toString();
@Override public void run() { try { String jsonContent = ConnectionWebService.callMethod("HelloWorld", null); LogHelper.i(jsonContent); } catch (Exception e) { LogHelper.i("報錯了"); } }