【发布时间】:2013-01-02 22:44:39
【问题描述】:
我的 App_Code/IGetEmployees.vb 文件
<ServiceContract()> _
Public Interface IGetEmployees
<OperationContract()> _
<WebInvoke(Method:="POST", ResponseFormat:=WebMessageFormat.Json, BodyStyle:=WebMessageBodyStyle.Wrapped, UriTemplate:="json/contactoptions")> _
Function GetAllContactsMethod(strCustomerID As String) As List(Of NContactNames)
End Interface
我的 App_Code/GetEmployees.vb 文件
<WebMethod()> _
<ScriptMethod(ResponseFormat:=ResponseFormat.Json)> _
Public Function GetAllContactsMethod(strCustomerID As String) As List(Of NContactNames) Implements IGetEmployees.GetAllContactsMethod
Utilities.log("Hit get all contacts at 56")
Dim intCustomerID As Integer = Convert.ToInt32(strCustomerID)
Dim lstContactNames As New List(Of NContactNames)
'I add some contacts to the list.
Utilities.log("returning the lst count of " & lstContactNames.Count)
Return lstContactNames
End Function
NContactNames 是一个具有 3 个属性的类。 所以我正在使用 ASP.NET Web 服务从 SQL 服务器检索信息并以 JSON 格式将其传递给我的 iPad。我在传递参数时遇到问题。所以就像你看到的,我有 2 个文件 IGetEmployees.vb 和 GetEmployees.vb。我正在实现方法 GetAllContactsMethod。发生的事情是 GetEmployees.vb 文件 (Utilities.log) 中的两行,它们永远不会被记录。该函数根本没有被调用。
我调用这个函数的目标 c 代码
NSString *param = [NSString stringWithFormat:@"strCustomerID=%@",strCustomerID];
jUrlString = [NSString stringWithFormat:@"%@",@"http://xyz-dev.com/GetEmployees.svc/json/contactoptions"];
jurl = [NSURL URLWithString:jUrlString];
NSMutableURLRequest * request = [NSMutableURLRequest requestWithURL:jurl];
[request setHTTPMethod:@"POST"];
[request setHTTPBody:[param dataUsingEncoding:NSUTF8StringEncoding]];
NSLog(@" request string is %@",[[request URL] absoluteString]);
NSLog(@"Done");
NSURLConnection *theConnection=[[NSURLConnection alloc] initWithRequest:request delegate:self];
if(theConnection)
{
jData = [NSMutableData data];
NSError *jError;
NSMutableDictionary *json =[NSJSONSerialization JSONObjectWithData:jData options:kNilOptions error:&jError];
NSLog(@"%@",json); //Gets Here and prints (null)
NSLog(@"Done"); //prints this as well.
}
else
{
NSLog(@"No");
}
在发布此代码时,“if”语句为真,并且 (null) 被打印,后跟“Done” 我的绝对请求的输出是: 请求字符串是http://xyz-dev.com/GetEmployees.svc/json/contactoptions 这是我第一次编写 json 来接受参数。所以我可能会遗漏一些东西。它是什么?为什么在 Visual Studio 端根本没有调用该函数。如果您需要更多信息,请询问。谢谢...
【问题讨论】:
-
您期望的输出究竟是什么?您甚至都不会启动连接,更不用说从中获取任何数据了。
-
@warrenm。我在我的 lstContactNames 列表中添加了一些值。我没有把代码放在这里,但实际上在该方法调用上触发了一个 sql 存储过程,它返回 3/4 行,我制作 3/4 个对象并将其添加到列表中。我想以 JSON 格式获取这些值.还有你所说的“开始连接”是什么意思?我在网上看到了一个例子并遵循了它。这里有什么问题?
-
您还应该尝试使用 REST 客户端以确保服务器端正常工作,然后在 iOS 端工作并修复您的 NSURLConnection 代码。
标签: ios sql json web-services post