这是代码片段,我连接到我的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格式,但是当我 编辑了我的方法,并像这样连接到实体数据库
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="Data Source=msdev01;Initial Catalog=FineReceiptsTest;Integrated Security=True;MultipleActiveResultSets=True"" providerName="System.Data.EntityClient" />
</connectionStrings>
</configuration>
所以有人能告诉我使用Entity检索数据时会出现什么问题吗?
答案 0 :(得分:0)
您的帖子中没有足够的信息来确定原因。您必须为任何人发布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.