欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android 調(diào)用WCF實(shí)例詳解

 更新時(shí)間:2016年11月26日 11:49:52   投稿:lqh  
這篇文章主要介紹了Android 調(diào)用WCF實(shí)例詳解的相關(guān)資料,這里提供了實(shí)例代碼及實(shí)現(xiàn)效果圖,需要的朋友可以參考下

Android 調(diào)用WCF實(shí)例

1. 構(gòu)建服務(wù)端程序

using System.ServiceModel;

namespace yournamespace
{
  [ServiceContract(Name = "HelloService", Namespace = "http://www.master.haku")]
  public interface IHello
  {
    [OperationContract]
    string SayHello();
  }
}


namespace YourNameSpace
{
  public class YourService  
  {
   public string SayHello(string words)
   {
      return "Hello " + words;
   }
  }
}

2. 構(gòu)建IIS網(wǎng)站宿主

  YourService.svc

<%@ServiceHost Debug="true" Service="YourNameSpace.YourService"%>

  Web.config

<?xml version="1.0" encoding="utf-8"?>
<configuration>
 <system.serviceModel>
  <serviceHostingEnvironment>
   <serviceActivations >
    <add relativeAddress="YourService.svc" service="YourNameSpace.YourService"/>
   </serviceActivations >
  </serviceHostingEnvironment >

  <bindings>
   <basicHttpBinding>
    <binding name="BasicHttpBindingCfg" closeTimeout="00:01:00"
      openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00"
      bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard"
      maxBufferPoolSize="524288" maxReceivedMessageSize="2147483647"
      messageEncoding="Text" textEncoding="utf-8" useDefaultWebProxy="true"
      allowCookies="false">
     <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384"
       maxBytesPerRead="4096" maxNameTableCharCount="16384" />
     <security mode="None">
      <transport clientCredentialType="None" proxyCredentialType="None"
        realm="" />
      <message clientCredentialType="UserName" algorithmSuite="Default" />
     </security>
    </binding>
   </basicHttpBinding>
  </bindings>
  
  <services>
   <service name="YourNameSpace.YourService" behaviorConfiguration="ServiceBehavior">
    <host>
     <baseAddresses>
      <add baseAddress="http://localhost:59173/YourService"/>
     </baseAddresses>
    </host>
    <endpoint binding="basicHttpBinding" contract="YourNameSpace.你的服務(wù)契約接口">
     <identity>
      <dns value="localhost" />
     </identity>
    </endpoint>
   </service>
  </services>

  <behaviors>
   <serviceBehaviors>
    <behavior name="ServiceBehavior">
     <serviceMetadata httpGetEnabled="true" />
     <serviceDebug includeExceptionDetailInFaults="true" />
    </behavior>
   </serviceBehaviors>
  </behaviors>
 </system.serviceModel>
 <system.web>
  <compilation debug="true" />
 </system.web>
</configuration>

3. 寄宿服務(wù)

  把網(wǎng)站發(fā)布到web服務(wù)器, 指定網(wǎng)站虛擬目錄指向該目錄

  如果你能夠訪問(wèn)http://你的IP:端口/虛擬目錄/服務(wù).svc

  那么,恭喜你,你的服務(wù)端成功了! 

4. 使用ksoap2調(diào)用WCF

  去ksoap2官網(wǎng)

  http://code.google.com/p/ksoap2-android/ 下載最新jar

 5. 在Eclipse中新建一個(gè)Java項(xiàng)目,測(cè)試你的服務(wù)

  新建一個(gè)接口, 用于專門讀取WCF返回的SoapObject對(duì)象

  ISoapService



package junit.soap.wcf;

import org.ksoap2.serialization.SoapObject;

public interface ISoapService {
  SoapObject LoadResult();
}


   HelloService



package junit.soap.wcf;

import java.io.IOException;
import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
import org.xmlpull.v1.XmlPullParserException;

public class HelloService implements ISoapService {
  private static final String NameSpace = "http://www.master.haku";
  private static final String URL = "http://你的服務(wù)器/虛擬目錄/你的服務(wù).svc";
  private static final String SOAP_ACTION = "http://www.master.haku/你的服務(wù)/SayHello";
  private static final String MethodName = "SayHello";
  
  private String words;
  
  public HelloService(String words) {
    this.words = words;
  }
  
  public SoapObject LoadResult() {
    SoapObject soapObject = new SoapObject(NameSpace, MethodName);
    soapObject.addProperty("words", words);
    
    SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11); // 版本
    envelope.bodyOut = soapObject;
    envelope.dotNet = true;
    envelope.setOutputSoapObject(soapObject);
    
    HttpTransportSE trans = new HttpTransportSE(URL);
    trans.debug = true; // 使用調(diào)試功能
    
    try {
      trans.call(SOAP_ACTION, envelope);
      System.out.println("Call Successful!");
    } catch (IOException e) {
      System.out.println("IOException");
      e.printStackTrace();
    } catch (XmlPullParserException e) {
      System.out.println("XmlPullParserException");
      e.printStackTrace();
    }
    
    SoapObject result = (SoapObject) envelope.bodyIn;
    
    return result;
  }
}

  測(cè)試程序

package junit.soap.wcf;

import org.ksoap2.serialization.SoapObject;

public class HelloWcfTest {
  public static void main(String[] args) {
    HelloService service = new HelloService("Master HaKu");
    SoapObject result = service.LoadResult();
    
    System.out.println("WCF返回的數(shù)據(jù)是:" + result.getProperty(0));
  }
}

   經(jīng)過(guò)測(cè)試成功

   運(yùn)行結(jié)果:

   Hello Master HaKu

6. Android客戶端測(cè)試

package david.android.wcf;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import org.ksoap2.serialization.SoapObject;

public class AndroidWcfDemoActivity extends Activity {
  private Button mButton1;
  private TextView text;

  /** Called when the activity is first created. */
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    mButton1 = (Button) findViewById(R.id.myButton1);
    text = (TextView) this.findViewById(R.id.show);

    mButton1.setOnClickListener(new Button.OnClickListener() {
      @Override
      public void onClick(View v) {
        
         HelloService service = new HelloService("Master HaKu");
                SoapObject result = service.LoadResult();

        text.setText("WCF返回的數(shù)據(jù)是:" + result.getProperty(0));
      }
    });
  }
}


7. 最后運(yùn)行結(jié)果

 

感謝閱讀,希望能幫助到大家,謝謝大家對(duì)本站的支持!

相關(guān)文章

  • 深入理解Kotlin的泛型系統(tǒng)

    深入理解Kotlin的泛型系統(tǒng)

    Kotlin 泛型即 “參數(shù)化類型”,將類型參數(shù)化,可以用在類,接口,方法上。下面 這篇文章主要給大家介紹了關(guān)于Kotlin泛型系統(tǒng)的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),需要的朋友可以參考下。
    2017-12-12
  • Android實(shí)現(xiàn)倒計(jì)時(shí)方法匯總

    Android實(shí)現(xiàn)倒計(jì)時(shí)方法匯總

    這篇文章主要為大家詳細(xì)總結(jié)了Android實(shí)現(xiàn)倒計(jì)時(shí)的3種方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-01-01
  • Android數(shù)據(jù)加密之Aes加密

    Android數(shù)據(jù)加密之Aes加密

    這篇文章主要為大家詳細(xì)介紹了Android數(shù)據(jù)加密之Aes加密,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2016-09-09
  • Flutter實(shí)現(xiàn)webview與原生組件組合滑動(dòng)的示例代碼

    Flutter實(shí)現(xiàn)webview與原生組件組合滑動(dòng)的示例代碼

    這篇文章主要介紹了Flutter實(shí)現(xiàn)webview與原生組件組合滑動(dòng)的示例代碼,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2019-03-03
  • Android基于Flutter編寫文件下載管理器

    Android基于Flutter編寫文件下載管理器

    文件下載在很多類型的應(yīng)用中會(huì)涉及,例如音樂(lè)、文檔、包括圖片(只是圖片可以使用一些組件完成無(wú)感知的下載)。本篇介紹使用Flutter中的Dio下載方法完成文件的下載,需要的可以參考一下
    2022-03-03
  • RecyclerBezierChart曲線圖表繪制

    RecyclerBezierChart曲線圖表繪制

    這篇文章主要為大家介紹了RecyclerBezierChart曲線圖表繪制示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-12-12
  • Android基于CountDownTimer實(shí)現(xiàn)倒計(jì)時(shí)功能

    Android基于CountDownTimer實(shí)現(xiàn)倒計(jì)時(shí)功能

    這篇文章主要介紹了Android基于CountDownTimer實(shí)現(xiàn)倒計(jì)時(shí)功能,簡(jiǎn)單分析了基于CountDownTimer類實(shí)現(xiàn)倒計(jì)時(shí)功能的技巧,需要的朋友可以參考下
    2015-12-12
  • Android仿微信聯(lián)系人字母排序效果

    Android仿微信聯(lián)系人字母排序效果

    大家使用到的聯(lián)系人界面,幾乎都是按照字母排序,如何實(shí)現(xiàn)聯(lián)系人按字母排序?下面就為大家詳解介紹了Android仿微信聯(lián)系人按字母排序的實(shí)現(xiàn)方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2016-11-11
  • 懸浮對(duì)話框Android代碼實(shí)現(xiàn)

    懸浮對(duì)話框Android代碼實(shí)現(xiàn)

    這篇文章主要為大家詳細(xì)介紹了懸浮對(duì)話框Android代碼實(shí)現(xiàn),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2016-08-08
  • Android實(shí)現(xiàn)文字動(dòng)態(tài)高亮讀取進(jìn)度效果

    Android實(shí)現(xiàn)文字動(dòng)態(tài)高亮讀取進(jìn)度效果

    這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)文字動(dòng)態(tài)高亮讀取進(jìn)度效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-05-05

最新評(píng)論