我正在尝试从Android客户端调用WCF服务,我得到了End of input at character 0 of
错误。当我用WCF客户端测试测试它时服务工作正常,但是当我在Android客户端中尝试它时,它没有生成输入错误结束...我不知道我错过了什么..
所以这是我的代码
EmployeeInfo.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>
Android方法
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服务,我在这里掖...请帮助
答案 0 :(得分:0)
只需将此值的大小增加为图像上显示的6553600。
WebGet(UriTemplate = "GetEmployee/{employeeId}",
BodyStyle = WebMessageBodyStyle.WrappedRequest,
ResponseFormat = WebMessageFormat.Json,
Method="GET",
RequestFormat = WebMessageFormat.Json)]