Android使用WCF的服务程序之入门

最近项目在进行准本上线,但是由于操作人员不能长久的盯着PC端的程序或者由于人不在电脑旁不能解决紧急问题,因此设计由PC端程序和手机精简客户端共同来触发管理。

由于PC端的程序是以.NET 为基础框架进行开发的。因此尝试着使用Android来调用服务程序完成中间的功能链接。流程如下所述:

  1. 首先准备好提供数据以及提供数据处理的服务接口,然后发布
  2. 创建Android精简客户端,然后使用网络访问程序进行调用服务
  3. 通过POST或者GET 来进行服务程序的调用,并获取调用结果
  4. 数据刷新并提示结果

创建WCF服务程序

1.创建一个契约 包含 getdatas()方法并实现

2.修改web配置文件。具体如下:

 <system.serviceModel>
    <behaviors>
      <serviceBehaviors>
        <behavior name="">
          <!-- 为避免泄漏元数据信息,请在部署前将以下值设置为 false -->
          <serviceMetadata httpGetEnabled="true"/>
          <!-- 要接收故障异常详细信息以进行调试,请将以下值设置为 true。在部署前设置为 false 以避免泄漏异常信息 -->
          <serviceDebug includeExceptionDetailInFaults="false"/>
        </behavior>
      </serviceBehaviors>
      <endpointBehaviors>
        <behavior name="httpBehavior">
          <webHttp/>
        </behavior>
      </endpointBehaviors>
    </behaviors>
    <serviceHostingEnvironment aspNetCompatibilityEnabled="false" multipleSiteBindingsEnabled="true"/>
    <services>
      <service name="WcfServiceDemo01.Service1">
        <endpoint address="" behaviorConfiguration="httpBehavior" binding="webHttpBinding" contract="WcfServiceDemo01.IService1"/>
      </service>
    </services>
  </system.serviceModel>

3.发布为服务

4.创建Android应用程序

5.使用HttpUrlConnection访问已经发布的服务

private final static String SERVICE_URI = "http://120.24.65.182/Service1.svc";
String connString=SERVICE_URI + "/GetDatas";
            URL url=new URL(connString);
            HttpURLConnection conn= (HttpURLConnection) url.openConnection();
            conn.setRequestMethod("GET");
            InputStream is = null;
            conn.connect();
            if (conn.getResponseCode() >= 200 && conn.getResponseCode() < 400) {
                // Create an InputStream in order to extract the response object
                is = conn.getInputStream();
            }
            else {
                is = conn.getErrorStream();
            }
            String dd=is.toString();
            BufferedReader br = new BufferedReader(new InputStreamReader(is));
            String response = "";
            String readLine = null;
            while((readLine =br.readLine()) != null){
                //response = br.readLine();
                response = response + readLine;
            }
            is.close();
            br.close();
            conn.disconnect();

            Message msg=new Message();
            msg.obj=response;
            msg.what=1;
            handler.sendMessage(msg);
6.在handler程序中刷新TextView控件,运行结果如下。

图一:访问网络之前:


图二 访问网络之后



                    

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值