1

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

这是我的代码

服务

登录服务

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

登录服务.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>

我在 IIS 上托管了这项服务,并且在 dotnet 应用程序中运行良好。我用于访问此服务的 android 代码是

     private void callServiceMethod() throws IOException, XmlPullParserException 
     {
        String NAMESPACE = "http://tempuri.org/";
        String METHOD_NAME = "LoginUser";
        String SOAP_ACTION = "http://tempuri.org/LoginUser";
        String URL = "http://192.168.16.61/LoginService/LoginService.svc";

        SoapObject Request = new SoapObject(NAMESPACE, METHOD_NAME);

        PropertyInfo pi = new PropertyInfo();
        pi.setName("uname");
        pi.setValue("jayant");
        pi.setType(String.class);

        Request.addProperty(pi);

        PropertyInfo pi2 = new PropertyInfo();
        pi2.setName("password");
        pi2.setValue("jayant");
        pi2.setType(String.class);
        Request.addProperty(pi2);

        SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
        envelope.dotNet = true;
        envelope.setOutputSoapObject(Request);
        AndroidHttpTransport androidHttpTransport = new AndroidHttpTransport(URL);
        androidHttpTransport.call(SOAP_ACTION, envelope);
        SoapObject response = (SoapObject)envelope.getResponse();
        Result =  Boolean.parseBoolean(response.getProperty(0).toString()) ;
    }

运行此代码时,我的程序出现异常:XmlpullParserException : End tag expected

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

4

2 回答 2

0

试试这个

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

于 2012-11-08T12:48:21.760 回答
0

您需要为您的操作合同指定一些属性。尝试这样的事情:

    [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 中添加不必要的文本。

还可以考虑将 JSON 与 WCF 服务一起使用,因为 JSON 更轻量并且内置于 Android。JSON 主页除了在 .NET 4+ 中添加 JSON 功能非常容易。

于 2012-11-08T13:14:20.173 回答