2016-12-01 85 views
0

我使用框架(4.5)开发了一个Restful WCF服务。此服务正在被Android客户端使用。Restful Wcf服务不返回准确的json形式的对象

我的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> 

我的接口IService1:

<OperationContract()> _ 
<WebGet(UriTemplate:="/GetEmp/{empid}", ResponseFormat:=WebMessageFormat.Json, RequestFormat:=WebMessageFormat.Json)> _ 
Function GetEmp(ByVal EmpId As String) As DataTable 

<OperationContract()> _ 
<WebGet(UriTemplate:="/GetEmpList/{empid}", ResponseFormat:=WebMessageFormat.Json, RequestFormat:=WebMessageFormat.Json)> _ 
Function GetEmpList(ByVal empid As String) As Employee 

我的服务:

Public Function GetEmp(ByVal EmpId As String) As DataTable Implements IService1.GetEmp 
    Dim table As New DataTable("mytable") 
    table.Columns.Add("Result", GetType(String)) 
    table.Columns.Add("acc", GetType(String)) 
    table.Columns.Add("name", GetType(String)) 
    table.Columns.Add("paid", GetType(Double)) 
    '' Using EmpId for Fetching data from Database 
    table.Rows.Add("True", EmpId, "Employee1", 5000) 
    table.Rows.Add("True", "2", "Employee2", 2000) 
    Return table 
End Function 
Public Function GetEmpList(empid As String) As Employee Implements IService1.GetEmpList 
    ' Using EmpId for Fetching data from Database 
    ' For Testing Manual Entry is Done 

    Dim EmpKey As String = """2016/Emp""/12" 

    Return New Employee With {.EmpID = empid, .EmpKey = EmpKey, .EmpName = "EmployeeName", .EmpPay = "20000"} 
End Function 

<DataContract()> 
Public Class Employee 
    <DataMember()> 
    Public Property EmpID As String 

    <DataMember()> 
    Public Property EmpKey As String 

    <DataMember()> 
    Public Property EmpName As String 

    <DataMember()> 
    Public Property EmpPay As Decimal 
End Class 

每一件事情是好的,同时呼吁GetEmpList,除了EmpKey。它包含许多特殊字符,如斜杠,backSlash,DoubleQuotes。在上面的代码中,我手动发送了EmpKey,它是“2016/Emp”/ 12 但它没有返回确切的关键数据。返回输出是

{ “的EmpID”: “1010”, “EmpKey”: “\” 2016/EMP \ “/ 12”, “EmpName”: “EmployeeName”, “EmpPay”:20000}

其中EmpKey不正确。

如何将实际数据发送到android客户端?我在android中使用HttpURLConnection。

回答

0

您可以从JsonObject中获取EmpKey的值。它可以解析为:

JSONObject jobj = new JSONObject(respons); 
String EmpKey = jobj.getString("EmpKey");