简体   繁体   中英

How to Consume WCF Rest Service with Android

Here is code snippet, where I connect to my WCF Service.

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

 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

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. Returned only html content with error like this

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

<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. The server is probably returning a HTTP 415 since you are not constructing the request correctly.

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

You are setting the content type to json for a GET request. 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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM