1

Androidで次のWCF Webサービスを使用しようとしています

これは私のコードです

サービス

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>

私はこのサービスを 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:終了タグが必要です

どこが間違っているのか教えてください。ありがとう

4

2 に答える 2

0

Operation コントラクトにいくつかの属性を指定する必要があります。次のようなことを試してください:

    [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.BareXML に不要なテキストが追加されるため、 を使用しないでください。

また、JSON は非常に軽量で Android に組み込まれているため、WCF サービスで JSON を使用することも検討してください。JSON ホームページJSON 機能の追加は、.NET 4+ では非常に簡単です。

于 2012-11-08T13:14:20.173 に答える
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 に答える