简体   繁体   English

带有json的Android WCF服务

[英]WCF service for android with json

I have a created a WCF service which is used by android. 我创建了一个Android使用的WCF服务。 It returns a json string but the actual string data sent by server code is different as accessed. 它返回一个json字符串,但是服务器代码发送的实际字符串数据在访问时有所不同。

My Web.Config 我的Web.Config

<?xml version="1.0"?>
<configuration>

  <appSettings>
    <add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />
  </appSettings>
  <system.web>
    <compilation debug="true" strict="false" explicit="true" targetFramework="4.5" />
    <httpRuntime targetFramework="4.5"/>
  </system.web>
  <system.serviceModel>
    <behaviors>
      <endpointBehaviors>
        <behavior name="httpBehavior">
          <webHttp />
        </behavior>
      </endpointBehaviors>
      <serviceBehaviors>
        <behavior>

          <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/>

          <serviceDebug includeExceptionDetailInFaults="false"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <services>
      <service name="WcfAndroid.Service1">
        <endpoint address=""
            behaviorConfiguration="httpBehavior"
            binding="webHttpBinding"
            contract="WcfAndroid.IService1" />

      </service>
    </services>
    <protocolMapping>
        <add binding="webHttpBinding" scheme="https" />
    </protocolMapping>    
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
  </system.serviceModel>
  <system.webServer>
    <modules runAllManagedModulesForAllRequests="true"/>
     <directoryBrowse enabled="true"/>
  </system.webServer>

</configuration>

And My WCF Service Interface : 和我的WCF服务接口:

<ServiceContract()>
Public Interface IService1

     <OperationContract()> _
        <WebGet(UriTemplate:="GetText", BodyStyle:=WebMessageBodyStyle.WrappedRequest, responseformat:=WebMessageFormat.Json, RequestFormat:=WebMessageFormat.Json)> _
        Function GetText() As String

 End Interface

I am returning a constant string from service for testing : 我从服务返回一个常量字符串进行测试:

Public Function GetText() As String Implements IService1.GetText
    '' GetjsonString()   returns   {"id": 100,"name": "item X","active": true}
    Return GetjsonString()
End Function

GetjsonString() function returns String {"id": 100,"name": "item X","active": true} GetjsonString()函数返回String {“ id”:100,“ name”:“ item X”,“ active”:true}

But when service is called it returns : 但是当服务被调用时,它返回:

{\\"id\\": 100,\\"name\\": \\"item X\\",\\"active\\": true} {\\“ id \\”:100,\\“ name \\”:\\“ item X \\”,\\“ active \\”:true}

I don't know Why it is not returning the actual string? 我不知道为什么它没有返回实际的字符串?

I an not really familiar with language so I am not pretty sure how that function works. 我对语言不是很熟悉,所以我不太确定该功能如何工作。 Do something like this: 做这样的事情:

string.replaceAll("\\", "")

to get rid of "\\"; 摆脱“ \\”;

What happened with your string is that it have escaped ". 您的字符串发生的事情是它已转义为“。

If this is not enough, and clearly this method has flaws, there is an answer to similar problem: wcf forces chars escaping in response 如果这还不够,并且显然此方法有缺陷,那么可以解决类似问题: wcf强制字符转义以响应

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM