私はこのようになります最も簡単な非常にHello WorldのWCFサービスを、持っている:Androidのクライアントを通じてWCFサービスへの接続
namespace MyWCFServices
{
public class HelloWorldService : IHelloWorldService
{
public String GetMessage()
{
return "Hello world";
}
}
}
namespace MyWCFServices
{
[ServiceContract(Namespace = "http://127.0.0.1:49359/GetMessage/")]
public interface IHelloWorldService
{
[OperationContract]
String GetMessage();
}
}
私のweb.configファイル:
<?xml version="1.0"?>
<configuration>
<system.web>
<compilation debug="false" targetFramework="4.0" />
</system.web>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true"/>
</system.webServer>
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior name="MyServiceTypeBehaviors">
<serviceMetadata httpGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="false" />
</behavior>
</serviceBehaviors>
</behaviors>
<services>
<service name="MyWCFServices.HelloWorldService"
behaviorConfiguration="MyServiceTypeBehaviors">
<host>
<baseAddresses>
<add baseAddress="http://127.0.0.1:49359/HostDevServer/HelloWorldService.svc" />
</baseAddresses>
</host>
<endpoint name="GetMessage" address="" binding="basicHttpBinding"
contract="MyWCFServices.IHelloWorldService"/>
<endpoint contract="IMetadataExchange"
binding="mexHttpBinding" address="mex"/>
</service>
</services>
</system.serviceModel>
</configuration>
そして、私のアンドロイドのjava:
HttpClient httpClient = new DefaultHttpClient();
String url = "http://10.0.2.2:49359/HostDevServer/HelloWorldService.svc/GetMessage";
//String url = "http://10.0.2.2:49359/HostDevServer/HelloWorldService.svc/GetMessage";
//String url = "http://localhost:49359/HostDevServer/HelloWorldService.svc";
//String url = "http://localhost:49359/GetMessage";
try{
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");
}
私はjava.net.SocketExceptionを取得しています:許可が拒否されました。私はすでに持っている:
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
そうか、私のWSは、接続を受け入れていないか、またはアンドロイドクライアントは間違った方法で接続されます。おそらく両方。知っている人のために、アンドロイドクライアントで10.0.2.2をlocalhost Webサーバーにリダイレクトする必要があったので、そこに問題はありません(少なくともPHP KSOAP WSに接続すると動作します)
誰でもガイドできます私?
はい、私はKSOAPを扱う方法を知っています(私はすでにSoap WS(php)に接続しているアプリを持っています)。私はもっと「直接的な」アプローチに従おうとしていましたが、今は存在しないことが分かりました。その間、私はSoap(代わりにRestまたは何か他のもの)を使用しなければならないことがわかりました。しかし、WS(WCF)側では、何をすべきか分かりません。あなたは例を参考にしていただけますか? – Tiago