WCF服务Android连接

本文关键字:连接 Android 服务 WCF | 更新日期: 2023-09-27 17:59:32

我对android和wcf服务很陌生。我有一个期待。

这是安卓的功能:

private void Post(String VAL)
{
    HttpClient httpClient = new DefaultHttpClient();
    try
    {
        String aaa = "aaa";
        String url = "http://192.168.30.117:59151/IService1";
        HttpPost method = new HttpPost(url);
        StringEntity e = new StringEntity("{ '"something'":12345 }", "UTF-8");
        method.setEntity(e);
        method.setHeader("content-type", "application/json");
        method.setHeader("Accept", "application/json");
        HttpResponse response = httpClient.execute(method);
        HttpEntity entity = response.getEntity();
        if(entity != null)
        {
            System.out.println(entity);
        }
    } catch (IOException e) {
        Log.e( "error", e.getMessage() );
    }}

这是wcf的一部分:

[ServiceContract()]
public interface IService1
{   
    [OperationContract]
    [WebInvoke(Method= "POST", UriTemplate = "GetData?parameter={parameter}", ResponseFormat = WebMessageFormat.Json)]
    string GetData(string parameter);
    [OperationContract]
    CompositeType GetDataUsingDataContract(CompositeType composite);
    // TODO: Add your service operations here
}

    public string GetData(String value)
    {
        return string.Format("You entered: {0}", value);
    }

这是配置文件:

<?xml version="1.0"?>
<configuration>
  <appSettings>
    <add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />
  </appSettings>
  <system.web>
    <compilation debug="true" targetFramework="4.5.2" />
    <httpRuntime targetFramework="4.5.2"/>
  </system.web>
  <system.serviceModel>
    <behaviors>
      <serviceBehaviors>
        <behavior name="http_behavior">
          <serviceMetadata httpGetEnabled="true" />
          <serviceDebug includeExceptionDetailInFaults="true" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <services>
      <service behaviorConfiguration="http_behavior" name="WcfServiceGps.Service1">
        <endpoint address="" binding="webHttpBinding" bindingConfiguration=""
          name="GetData" contract="WcfServiceGps.IService1" />
        <endpoint address="mex" binding="mexHttpBinding" name="GetDataMex"
          contract="IMetadataExchange" />
        <host>
          <baseAddresses>
            <add baseAddress="http://192.168.30.117:59151/Service1.svc/" />
          </baseAddresses>
        </host>
      </service>
    </services>
  </system.serviceModel>
  <system.webServer>
    <modules runAllManagedModulesForAllRequests="true"/>
    <directoryBrowse enabled="true"/>
  </system.webServer>

当ıam从android 呼叫服务

HttpResponse response = httpClient.execute(method);

它给出以下异常:

WCF服务Android连接

如果您在主线程中调用api,请不要这样做。使用AsyncTask来使用api。把这些线放在你的活动中,或者把你使用的东西碎片化。

if (android.os.Build.VERSION.SDK_INT > 9) {
    StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
    StrictMode.setThreadPolicy(policy);
}

如果您在android上使用虚拟机编译应用程序,则必须在服务器上具有可见性,与使用虚拟机时相同。你会遇到这个异常,因为你的设备不在同一个网络中,而且你似乎没有部署服务器,你只是在Visual studio上编译它。。。这是什么?"59151'IIS express的端口号?。

不要在windows上加载IIS并部署服务器,然后使用ipconfig命令来了解您在本地网络中的ip地址。经常尝试使用android浏览器连接服务器(编写您的ip 192.168.X.X)。如果它有效,请尝试在android浏览器中调用REST服务,如果它也有效,您现在可以在android中测试您的REST调用。

顺便说一下,使用改装2作为REST库。