我正在尝试从 android 客户端调用 WCF 服务,但出现End of input at character 0 of
错误。当我使用 WCF 客户端测试对其进行测试时,该服务工作正常,但是当我在 android 客户端中尝试它时,它没有,并且生成了输入错误结束...我不知道我错过了什么..
所以这是我的代码
员工信息.svc
namespace EmployeeServices
{
// NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "EmployeeInfo" in code, svc and config file together.
public class EmployeeInfo : IEmployeeInfo
{
public Employee GetEmployee(int employeeId)
{
Employee employeeInfo = GetEmployees(employeeId).Where(employee => employee.EmployeeId == employeeId).FirstOrDefault();
return employeeInfo;
}
private List<Employee> GetEmployees(int employeeId)
{
return new List<Employee> {
new Employee { EmployeeId = 11, FirstName = "Waqas", LastName = "Yousuf", Address="A-175 Block 1" , BloodGroup = "B+" },
new Employee { EmployeeId = 22, FirstName = "Moiz", LastName = "Ahmed", Address="B-176 Block 2" , BloodGroup = "O-" },
new Employee { EmployeeId = 33, FirstName = "Waqas", LastName = "Raza", Address="C-177 Block 3" , BloodGroup = "A+" },
new Employee { EmployeeId = 44, FirstName = "Yasir", LastName = "Amin", Address="D-178 Block 4" , BloodGroup = "AB+" },
new Employee { EmployeeId = 55, FirstName = "Adeel", LastName = "Ali", Address="E-179 Block 5" , BloodGroup = "B+" } };
}
}
}
IEmployeeInfo.cs
namespace EmployeeServices
{
// NOTE: You can use the "Rename" command on the "Refactor" menu to change the interface name "IEmployeeInfo" in both code and config file together.
[ServiceContract(Namespace = "http://services.example.com")]
public interface IEmployeeInfo
{
[OperationContract]
[WebGet(UriTemplate = "GetEmployee/{employeeId}",
BodyStyle = WebMessageBodyStyle.WrappedRequest,
ResponseFormat = WebMessageFormat.Json,
RequestFormat = WebMessageFormat.Json)]
Employee GetEmployee(int employeeId);
}
[DataContract]
public class Employee
{
[DataMember]
public int EmployeeId { get; set; }
[DataMember]
public string FirstName { get; set; }
[DataMember]
public string LastName { get; set; }
[DataMember]
public string Address { get; set; }
[DataMember]
public string BloodGroup { get; set; }
}
}
这是web.config
<?xml version="1.0"?>
<configuration>
<system.web>
<compilation debug="true" targetFramework="4.0" />
</system.web>
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior>
<!-- To avoid disclosing metadata information, set the value below to false and remove the metadata endpoint above before deployment -->
<serviceMetadata httpGetEnabled="true"/>
<!-- To receive exception details in faults for debugging purposes, set the value below to true. Set to false before deployment to avoid disclosing exception information -->
<serviceDebug includeExceptionDetailInFaults="false"/>
</behavior>
</serviceBehaviors>
</behaviors>
<serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
<services>
<service name="EmployeeService.EmployeeInfo">
<endpoint kind="webHttpEndpoint"
contract="EmployeeService.IEmployeeInfo" />
</service>
</services>
</system.serviceModel>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true"/>
</system.webServer>
</configuration>
安卓方法
try {
String SERVICE_URI = "http://192.168.1.4:1234/Employeeinfo.svc";
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpGet request = new HttpGet(SERVICE_URI + "/GetEmployee/11");
request.setHeader("Accept", "application/json");
request.setHeader("Content-type", "application/json");
HttpResponse response = httpClient.execute(request);
HttpEntity responseEntity = response.getEntity();
// Read response data into buffer
char[] buffer = new char[(int)responseEntity.getContentLength()];
InputStream stream = responseEntity.getContent();
InputStreamReader reader = new InputStreamReader(stream);
reader.read(buffer);
stream.close();
JSONObject Emp = new JSONObject(new String(buffer));
// Populate text fields
ed1.setText(Emp.getString("Address"));
ed2.setText(Emp.getString("BloodGroup"));
ed3.setText(Emp.getString("EmployeeId"));
ed4.setText(Emp.getString("FirstName"));
ed5.setText(Emp.getString("LastName"));
}
catch (Exception e) {
e.printStackTrace();
lblStatus.setText(e.getMessage());
}
我工作了好几天来学习如何从 android 应用程序调用 WCF 服务,我正在这里...请帮助