在Android应用程序中使用WCF服务

在Android应用程序中使用WCF服务,android,wcf,web-services,networking,Android,Wcf,Web Services,Networking,我正在尝试在Android中使用以下WCF web服务 这是我的密码 服务 [ServiceContract] public interface ILoginService { [OperationContract] bool LoginUser(string uname, string password); } ILoginService [ServiceContract] public interface IL

我正在尝试在Android中使用以下WCF web服务

这是我的密码

服务

    [ServiceContract]
    public interface ILoginService
    {
        [OperationContract]
        bool LoginUser(string uname, string password);
    }
ILoginService

    [ServiceContract]
    public interface ILoginService
    {
        [OperationContract]
        bool LoginUser(string uname, string password);
    }
LoginService.svc.cs

  public class LoginService : ILoginService
    {
        public bool LoginUser(string uname, string password)
        {
            if (uname == password)
                return true;
            else
                return false;
        }
    }
web.config

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

  <appSettings>
    <add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />
  </appSettings>
  <system.web>
    <compilation debug="true" targetFramework="4.5" />
    <httpRuntime targetFramework="4.5"/>
  </system.web>
  <system.serviceModel>

      <services>
        <service name="LoginService.LoginService">
          <endpoint binding="basicHttpBinding" contract="LoginService.ILoginService" ></endpoint>  
      </service>
      </services>

      <behaviors>
      <serviceBehaviors>
        <behavior>

          <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/>
          <serviceDebug includeExceptionDetailInFaults="false"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>
     <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
  </system.serviceModel>

  <system.webServer>
    <defaultDocument>
       <files>
        <clear />
        <add value="LoginService.svc" />
      </files>
    </defaultDocument>
    <modules runAllManagedModulesForAllRequests="true"/>
    <directoryBrowse enabled="true"/>
   <handlers>
      <add name="svc-ISAPI-2.0" path="*.svc" verb="*" modules="IsapiModule" scriptProcessor="C:\Windows\Microsoft.NET\Framework\v4.0.30319\aspnet_isapi.dll" resourceType="File" preCondition="classicMode,runtimeVersionv4.0,bitness32"/>
      <add name="svc-Integrated" path="*.svc" verb="*" type="System.ServiceModel.Activation.HttpHandler" resourceType="File" preCondition="integratedMode"/>
    </handlers>
  </system.webServer>

</configuration>
运行此代码时,我的程序出现异常:XmlpullParserException:End标记应为

请告诉我我哪里做错了? 谢谢

试试这个

public InputStream getResponse(String url,String params)
{
    InputStream is = null;
    try
    {

        HttpPost request = new HttpPost(url);
        request.setHeader("Accept", "application/xml");
        request.setHeader("Content-type", "application/xml");
        StringEntity entity = new StringEntity(params.toString());
        request.setEntity(entity);
        DefaultHttpClient httpClient = new DefaultHttpClient();
        HttpResponse response = httpClient.execute(request);

        String ss1=EntityUtils.toString(response.getEntity());

        Log.v("log", ss1);

        is = new ByteArrayInputStream(ss1.getBytes("UTF-8"));
    }
    catch (Exception e)
    {
        e.printStackTrace();
    }

    return is;
}
传递url http:XXXXXXXXX/LoginService/LoginService.svc/LoginUser params是作为字符串的XML体
并在XML解析中传递inputStream

您需要为操作契约指定一些属性。 试着这样做:

    [OperationContract()]
[XmlSerializerFormat(Style = OperationFormatStyle.Document, Use = OperationFormatUse.Literal)]
[WebInvoke(BodyStyle = WebMessageBodyStyle.Wrapped, Method = "POST", RequestFormat = WebMessageFormat.Xml, ResponseFormat = WebMessageFormat.Xml, UriTemplate = "/LoginUser?uname={username}&password={password})]
bool LoginUser(string uname, string password);
请确保不要使用
WebMessageBodyStyle.Bare
,因为这会将不必要的文本添加到XML中


也考虑使用JWSON与WCF服务,因为JSON较轻且内置于Android。此外,在.NET 4+中添加JSON功能非常简单。

您的服务是soap还是Advanced restfult?问:因为wcf服务是高级restfull而不是soap。对不起,我对此不太了解。我怎么知道呢?因为这是一个WCF服务,它必须是restfull。您的输入格式XML体或json体是什么?在哪里传递参数以及如何传递?像乌梅和帕索沃德