【问题标题】:Android app won't return result from VB.NET WCF ServiceAndroid 应用程序不会从 VB.NET WCF 服务返回结果
【发布时间】:2010-12-01 23:13:47
【问题描述】:

这是我的 WCF 服务器代码 (VB.NET)...

Service1.svc

Public Class Service1
Implements IService1

Public Sub New()
End Sub

Public Function GetText() As String Implements IService1.GetText
    Return String.Format("YO MTV ROCKS!")
End Function

Public Function GetData(ByVal value As Integer) As String Implements IService1.GetData
    Return String.Format("You entered: {0}", value)
End Function

Public Function GetDataUsingDataContract(ByVal composite As CompositeType) As CompositeType Implements IService1.GetDataUsingDataContract
    If composite Is Nothing Then
        Throw New ArgumentNullException("composite")
    End If
    If composite.BoolValue Then
        composite.StringValue &= "Suffix"
    End If
    Return composite
End Function

End Class

IService1.vb

' NOTE: You can use the "Rename" command on the context menu to change the interface 

name "IService1" in both code and config file together.
<ServiceContract()>
Public Interface IService1
<OperationContract()> _
<WebGet(UriTemplate:="GetText", BodyStyle:=WebMessageBodyStyle.WrappedRequest, responseformat:=WebMessageFormat.Json, RequestFormat:=WebMessageFormat.Json)> _
Function GetText() As String

<OperationContract()> _
<WebGet(UriTemplate:="GetData?v={value}", responseformat:=WebMessageFormat.Json)> _
Function GetData(ByVal value As Integer) As String

<OperationContract()>
Function GetDataUsingDataContract(ByVal composite As CompositeType) As CompositeType

' TODO: Add your service operations here

End Interface

' Use a data contract as illustrated in the sample below to add composite types to service operations.

<DataContract()>
Public Class CompositeType

<DataMember()>
Public Property BoolValue() As Boolean

<DataMember()>
Public Property StringValue() As String

结束类

Web.config

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

  <system.web>
    <compilation debug="true" strict="false" explicit="true" targetFramework="4.0" />
      <customErrors mode="Off"/>
  </system.web>
  <system.serviceModel>
    <behaviors>
        <endpointBehaviors>
            <behavior name="httpBehavior">
                <webHttp />
            </behavior>
        </endpointBehaviors>
        <serviceBehaviors>
            <behavior name="">
                <serviceMetadata httpGetEnabled="true" />
                <serviceDebug includeExceptionDetailInFaults="false" />
            </behavior>
        </serviceBehaviors>
    </behaviors>
    <serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
    <services>
        <service name="HttpWcfWeb.VehicleService">
            <endpoint address=""
                behaviorConfiguration="httpBehavior"
                binding="webHttpBinding"
                contract="HttpWcfWeb.IVehicleService" />
        </service>
    </services>
  </system.serviceModel>
  <system.webServer>
    <modules runAllManagedModulesForAllRequests="true"/>
  </system.webServer>

</configuration>

这是我的安卓客户端代码...

public static final String _URL = "http://192.168.212.37:8080/Service1.svc";
protected void logIn(){


    try
    {


// Send GET request to <service>/GetText
   HttpGet request = new HttpGet(_URL + "/GetText");
   request.setHeader("Accept", "application/json");
   request.setHeader("Content-type", "application/json");

   DefaultHttpClient httpClient = new DefaultHttpClient();
   HttpResponse response = httpClient.execute(request);
   HttpEntity responseEntity = response.getEntity();

   // Read response data into buffer
   long intCount = responseEntity.getContentLength();
     char[] buffer = new char[(int)responseEntity.getContentLength()];
     InputStream stream = responseEntity.getContent();
     InputStreamReader reader = new InputStreamReader(stream);
     reader.read(buffer);
     stream.close();

     tvStatus.append("response: ");
     JSONArray plates = new JSONArray(new String(buffer));

     for (int i = 0; i < plates.length(); ++i) {
      tvStatus.append(plates.getString(i));
     }

    } catch (Exception e) {
        e.printStackTrace();
    }
}

由于某些未知原因,我可以运行 VS 2010 客户端测试,并且 WCF 主机工作正常。上面的代码什么都不返回(它应该返回一个字符串)

关于我做错了什么有什么想法吗?

【问题讨论】:

  • 你发布的代码太多了。
  • Jarrette,你能写出你是如何解决这个问题的吗?我用同样的例子尝试了几天,我和你有同样的问题,我真的不知道该怎么做

标签: .net android vb.net wcf json


【解决方案1】:

我不确定 Android(还没有从 Android 做任何 WCF 的东西)但是当我从 javascript 访问我们的一些 WCF 服务时,我必须专门将方法的输出转换为 JSON,然后再将其发送回javascript 否则 JS 将一无所获。

我想知道这可能是这种情况吗?尝试返回格式正确的 JSON 对象?

【讨论】:

  • responseEntity.getContentLength() 返回为 0,因此从服务返回的 responseEntity 为空。服务设置为发送 JSON,客户端代码设置为接收 JSON。对此非常困惑......
  • 您可以尝试发回纯文本(“text/xml”)并查看是否有效吗?还可以尝试将您的 Content-Type 更改为 "application/json; charset=utf-8"
  • 我刚刚注意到的事情。在我的 HttpResponse 中,我得到一个 statusline.statusCode=400 和一个 statusline.reasonPhrase = "Bad Request"
  • 检查这些链接。我对 WCF 不太熟悉,但它们可能会有所帮助:underground.infovark.com/2008/04/09/wcf-bad-requestrobbagby.com/rest/effective-error-handling-with-wcf-rest
  • 我认为问题在于调用该方法的语法错误。我使用的 uri 是“192.168.212.37:8080/Service1.svc/GetText”我想知道这是否是调用该方法的正确语法?
猜你喜欢
  • 1970-01-01
  • 2012-05-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-10-25
相关资源
最近更新 更多