簡體   English   中英

如何在Android上使用WCF Rest Service

[英]How to Consume WCF Rest Service with Android

這是我連接到WCF服務的代碼段。

DefaultHttpClient client = new DefaultHttpClient();         
                            HttpGet request = new HttpGet(EMPLOYEE_LOG_IN);
                            request.setHeader("Accept", "application/json");
                            request.setHeader("Content-type", "application/json");          
                            HttpResponse response = client.execute(request);
                            HttpEntity entity = response.getEntity();
                            if(entity.getContentLength() != 0) {
                                Reader employeeReader = new InputStreamReader(response.getEntity().getContent());       
                                                //create a buffer to fill if from reader            
                                                char[] buffer = new char[(int) response.getEntity().getContentLength()];        
                                                //fill the buffer by the help of reader         
                                                employeeReader.read(buffer);                
                                                //close the reader streams              
                                                employeeReader.close(); 
                                                //for the employee json object              
                                                JSONObject employee =  new JSONObject(new String(buffer));
                                                Log.d("Response",employee.getString("FirstName"));    

它在不同情況下的行為方式不同。 這是我的WCF服務中的代碼

 public Controller GetEmployees(int personalNUmber)
        {                

                return new Controller
                            {
                                EmployeeId = 1,
                                FirstName = "FirstName",
                                LastName = "LastName"
                            };    
            }
        }

因此,當我調用此方法時,一切正常,我可以很好地獲取json格式,但是當我編輯方法並像這樣連接到Entity DB時

FineReceiptsTestEntities _entity = new FineReceiptsTestEntities();

        var t = _entity.Employees.FirstOrDefault(x => x.EmployeeID == personalNUmber);
        return new Controller
                   {

                       EmployeeId = 1,
                       FirstName = t != null ? t.Name : " ",
                       LastName = "LastName"
                   }; 

Json對象沒有返回。 僅返回帶有錯誤的 html內容,像這樣

服務器在處理請求時遇到錯誤。請參閱服務幫助頁面,以構建對服務的有效請求。

但是瀏覽器中的兩種方法都可以正常工作並返回相關數據。

這是我的web.config

<configuration>
  <system.web>
    <compilation debug="true" targetFramework="4.0">
      <assemblies>
        <add assembly="System.Data.Entity, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
      </assemblies>
    </compilation>
  </system.web>
  <system.serviceModel>
    <standardEndpoints>
      <webHttpEndpoint>
        <standardEndpoint helpEnabled="true" automaticFormatSelectionEnabled="true" />
      </webHttpEndpoint>
    </standardEndpoints>
    <services>
      <service name="dev_FineReceiptsService.ControllersInfo">
        <endpoint kind="webHttpEndpoint" contract="dev_FineReceiptsService.IControllersInfo" />
        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
      </service>
    </services>
  </system.serviceModel>
  <connectionStrings>
    <add name="FineReceiptsTestEntities" connectionString="metadata=res://*/FineTest.csdl|res://*/FineTest.ssdl|res://*/FineTest.msl;provider=System.Data.SqlClient;provider connection string=&quot;Data Source=msdev01;Initial Catalog=FineReceiptsTest;Integrated Security=True;MultipleActiveResultSets=True&quot;" providerName="System.Data.EntityClient" />
  </connectionStrings>
</configuration>

那么有人可以告訴我使用實體檢索數據時出什么問題嗎?

您的帖子中沒有足夠的信息來確定原因。 您必須發布RAW HTTP響應,任何人都可以幫助您。 由於您未正確構建請求,服務器可能正在返回HTTP 415。

request.setHeader("Content-type", "application/json");          

您正在將GET請求的內容類型設置為json。 這是不可能的。 服務器可能會嘗試匹配它並感到困惑。 服務器也可能是來自請求的真正的HTTP 500錯誤。 從構造響應錯誤的方式來看,服務似乎在抱怨該請求本身無效。

server encountered an error processing the request.Please see the service help page for constructing valid requests to the service.

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM