简体   繁体   English

如何在Android上使用WCF Rest Service

[英]How to Consume WCF Rest Service with Android

Here is code snippet, where I connect to my WCF Service. 这是我连接到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"));    

It behaves different ways in different situation. 它在不同情况下的行为方式不同。 Here is code from my WCF Service 这是我的WCF服务中的代码

 public Controller GetEmployees(int personalNUmber)
        {                

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

SO when I call this method everything works fine, I get json format well, but when I edited my method and connected into Entity DB like this 因此,当我调用此方法时,一切正常,我可以很好地获取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 object didn't returned. Json对象没有返回。 Returned only html content with error like this 仅返回带有错误的 html内容,像这样

server encountered an error processing the request.Please see the service help page for constructing valid requests to the service. 服务器在处理请求时遇到错误。请参阅服务帮助页面,以构建对服务的有效请求。

But both type of method in browser works fine and returns relevant data. 但是浏览器中的两种方法都可以正常工作并返回相关数据。

and here is my web.config 这是我的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>

So can anyone tell me what is the problem when using Entity to retrieve data ? 那么有人可以告诉我使用实体检索数据时出什么问题吗?

There is not enough information in your post to determine the cause. 您的帖子中没有足够的信息来确定原因。 You must post the RAW HTTP response for anyone to help you. 您必须发布RAW HTTP响应,任何人都可以帮助您。 The server is probably returning a HTTP 415 since you are not constructing the request correctly. 由于您未正确构建请求,服务器可能正在返回HTTP 415。

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

You are setting the content type to json for a GET request. 您正在将GET请求的内容类型设置为json。 That is not possible. 这是不可能的。 The server might be try to match that and get confused. 服务器可能会尝试匹配它并感到困惑。 It could also be that the server has a genuine HTTP 500 error from the request. 服务器也可能是来自请求的真正的HTTP 500错误。 From the way the response error is constructed, the service seems to be complaining that the request itself is not valid. 从构造响应错误的方式来看,服务似乎在抱怨该请求本身无效。

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