【发布时间】:2014-06-05 13:19:40
【问题描述】:
我正在尝试在 Android 中使用 WCF 服务。我已将服务托管为
http://localhost:8080/UserService.svc
这是我在 web.config web.config 中的 serviceModel 部分:
<system.serviceModel>
<bindings>
<basicHttpBinding>
<binding name="SimpleBasic">
<security mode="None"/>
</binding>
<binding name="BasicOverHttps">
<security mode="Transport"/>
</binding>
</basicHttpBinding>
</bindings>
<services>
<service behaviorConfiguration="AndroidService.Service1Behavior" name="AndroidService.Service1">
<endpoint address="" binding="wsHttpBinding" contract="AndroidService.IService1">
<identity>
<dns value="localhost"/>
</identity>
</endpoint>
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
</service>
<service behaviorConfiguration="AndroidService.UserServiceBehavior" name="AndroidService.UserService">
<endpoint address="ws" binding="wsHttpBinding" contract="AndroidService.IUserService">
<identity>
<dns value="localhost"/>
</identity>
</endpoint>
<endpoint address="basic" binding="basicHttpBinding" bindingConfiguration="SimpleBasic" contract="AndroidService.IUserService"/>
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
<endpoint address="web" binding="webHttpBinding" contract="AndroidService.IUserService" />
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="AndroidService.Service1Behavior">
<serviceMetadata httpGetEnabled="true"/>
<serviceDebug includeExceptionDetailInFaults="false"/>
</behavior>
<behavior name="AndroidService.UserServiceBehavior">
<serviceMetadata httpGetEnabled="true"/>
<serviceDebug includeExceptionDetailInFaults="false"/>
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
我可以浏览。我还检查了 WebServiceStudio 中服务的一种方法 GetUser 的输出。结果很好。然后我创建了一个 Android 客户端应用程序,我正在尝试使用它。
HttpClient httpClient = new DefaultHttpClient();
try
{
String url = "http://localhost:8080/UserService.svc/GetUser?name=Nitish";
HttpGet method = new HttpGet( new URI(url) );
HttpResponse response = httpClient.execute(method);
if ( response != null )
{
Log.i( "login", "received " + getResponse(response.getEntity()) );
}
else
{
Log.i( "login", "got a null response" );
}
} catch (IOException e) {
Log.e( "error", e.getMessage() );
} catch (URISyntaxException e) {
Log.e( "error", e.getMessage() );
}
catch (Exception e) {
// TODO: handle exception
Log.e( "error", e.getMessage() );
}
我在HttpResponse response = httpClient.execute(method); 中得到如下异常
Connection to http://localhost:8080 refused
我正在设备上运行应用程序,并在 mainefest 中添加了以下权限:
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
我通过this solution 尝试在我的IP 地址上托管该服务,但没有成功。
【问题讨论】:
-
您是否在 android 设备上本地托管 WCF 服务?
-
我在本地托管。
-
Localhost 在 android 上不起作用,试试 10.0.0.1 之类的。
-
本地?本地在您的 android 设备上还是本地在您的开发 PC 上或本地在您的家庭服务器上?
-
本地在我的开发 PC 上。
标签: android web-services wcf