2011-09-17 44 views
2

好的,我有一個WCF服務,可以讓我發現終端找不到的錯誤,而我看不出爲什麼會這樣......這是我的代碼。在Android手機的REST服務中找不到終結點

首先,接口:

public interface ISightingServiceRest 
{ 
    [OperationContract] 
    [WebInvoke(Method = "POST", 
    UriTemplate = "SaveNewSightingRest", 
    ResponseFormat = WebMessageFormat.Json, 
    RequestFormat = WebMessageFormat.Json)] 
    string SaveNewSightingRest(string sighting); 
} 

現在,實際的方法:

public string SaveNewSightingRest(string sighting) 
{ 
    string received = sighting; 
    return "hola"; 
} 

正如我調試,我要的是看到字符串 「HOLA」 在我的迴應,並在變量「received」中發送字符串。

現在,webconfig:

<?xml version="1.0"?> 
<configuration> 
    <connectionStrings> 
    <add name="LiveAndesWCF" connectionString="Data Source=BRUNO-PC\LIVEANDES; 
Initial Catalog=liveandes;Trusted_Connection=Yes;" 
providerName="System.Data.SqlClient"/> 
    <add name="LiveAndes" connectionString="Data Source=BRUNO-PC\LIVEANDES; Initial 
Catalog=liveandes;Trusted_Connection=Yes;" providerName="System.Data.SqlClient"/> 
    </connectionStrings> 
    <system.web> 
    <compilation debug="true" targetFramework="4.0"/> 
    <membership> 
     <providers> 
     <clear/> 
     <add name="AspNetSqlMembershipProvider" 
      type="System.Web.Security.SqlMembershipProvider" 
      connectionStringName="LiveAndes" 
      enablePasswordRetrieval="false" enablePasswordReset="true" 
      requiresQuestionAndAnswer="false" requiresUniqueEmail="true"  
      maxInvalidPasswordAttempts="5" minRequiredPasswordLength="6" 
      minRequiredNonalphanumericCharacters="0" passwordAttemptWindow="10" 
      applicationName="/"/> 
     </providers> 
    </membership> 
    </system.web> 
    <appSettings> 
    <!--Local--> 
    <add key="mainPathDeployWCF" value="http://localhost:61642/"/> 
    <add key="mainMachPathDeployWCF" value="\LiveAndesWCF\bin"/> 

    </appSettings> 

    <system.serviceModel> 
    <protocolMapping> 
     <add scheme="http" binding="wsHttpBinding" /> 
    </protocolMapping> 
    <services> 

     <service name="LiveAndesWCF.SightingServiceRest" 
      behaviorConfiguration="MetadataBehavior"> 
     <endpoint address="" behaviorConfiguration="WebBehavior" 
       binding="webHttpBinding" 
       contract="LiveAndesWCF.ISightingServiceRest"/> 
     </service> 

    </services> 
    <bindings> 
     <webHttpBinding> 
     <binding maxReceivedMessageSize="99999999" closeTimeout="00:02:00" 
      openTimeout="00:02:00" receiveTimeout="00:10:00" sendTimeout="00:02:00"> 
      <readerQuotas maxArrayLength="76384000" maxStringContentLength="2147483647"/> 
     </binding> 
     </webHttpBinding> 
    </bindings> 
    <behaviors> 
     <endpointBehaviors> 
     <behavior name="jsonBehavior"> 
      <enableWebScript/> 
     </behavior> 
     <behavior name="WebBehavior"> 
      <webHttp /> 
     </behavior> 
     </endpointBehaviors> 
     <serviceBehaviors> 
     <behavior> 

      <serviceMetadata httpGetEnabled="true"/> 

      <serviceDebug includeExceptionDetailInFaults="true"/> 
     </behavior> 
     <behavior name="MetadataBehavior"> 
      <serviceMetadata httpGetEnabled="true" httpGetUrl="" /> 
     </behavior> 
     </serviceBehaviors> 
    </behaviors> 
    <serviceHostingEnvironment multipleSiteBindingsEnabled="true"/> 
    </system.serviceModel> 
    <system.webServer> 
    <modules runAllManagedModulesForAllRequests="true"/> 
    </system.webServer> 
</configuration> 

最後,我從仿真器使用方法:我收到 「responseString」 的

private void addNewSighting() throws Exception 
{ 
    String URL = "http://10.0.2.2:61642/SightingServiceRest.svc"; 

    AlertDialog popup; 

    SightingWrapper sighting = new SightingWrapper(); 

    String xml = createXML(sighting); 

    try{ 
     HttpPost request = new HttpPost(URL + "/SaveNewSightingRest/"); 

     StringEntity sen = new StringEntity(xml); 

     request.setHeader("Accept", "application/json"); 
     request.setHeader("Content-type", "application/json; charset=utf-8"); 

     request.setEntity(sen); 

     DefaultHttpClient httpclient = new DefaultHttpClient(); 
     HttpResponse response = httpclient.execute(request); 

     HttpEntity responseEntity = response.getEntity(); 

     char[] buffer = new char[(int)responseEntity.getContentLength()]; 

     InputStream stream = responseEntity.getContent(); 
     InputStreamReader reader = new InputStreamReader(stream); 
     reader.read(buffer); 
     stream.close(); 

     String responseString = new String(buffer); 

    } 
    catch(Exception e) 
    {   

    } 

} 

「端點未找到」 的消息。

我不知道我的錯誤在哪裏。

任何幫助表示讚賞。

謝謝

回答

0

嘗試在地址刪除最後的斜線

HttpPost request = new HttpPost(URL + "/SaveNewSightingRest/"); //before 
HttpPost request = new HttpPost(URL + "/SaveNewSightingRest"); //after 
相關問題