通过Objective C调用Web服务方法时没有命中

时间:2013-01-02 22:44:39

标签: ios sql json web-services post

我的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”语句为true且打印(null)后跟“Done” 我的绝对请求的输出是:     请求字符串为http://xyz-dev.com/GetEmployees.svc/json/contactoptions 这是我第一次写json来接受参数。所以我可能会遗漏一些东西。它是什么?为什么在Visual Studio端根本没有调用该函数。如果您需要更多信息,请询问。谢谢......

1 个答案:

答案 0 :(得分:0)

这就是Objetive-C中的一个方法。

-(void) insertEmployeeMethod

{

    if(firstname.text.length && lastname.text.length && salary.text.length)

    {

        NSString *str = [BaseWcfUrl stringByAppendingFormat:@"InsertEmployee/%@/%@/%@",firstname.text,lastname.text,salary.text];

        NSURL *WcfSeviceURL = [NSURL URLWithString:str];
        NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];

        [request setURL:WcfSeviceURL];

        [request setHTTPMethod:@"POST"];

        // connect to the web

        NSData *respData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];

        // NSString *respStr = [[NSString alloc] initWithData:respData encoding:NSUTF8StringEncoding];


        NSError *error;

        NSDictionary* json = [NSJSONSerialization 

                              JSONObjectWithData:respData 

                              options:NSJSONReadingMutableContainers 

                              error:&error];  



        NSNumber *isSuccessNumber = (NSNumber*)[json objectForKey:@"InsertEmployeeMethodResult"];


        //create some label field to display status
        status.text = (isSuccessNumber && [isSuccessNumber boolValue] == YES) ? [NSString stringWithFormat:@"Inserted %@, %@",firstname.text,lastname.text]:[NSString stringWithFormat:@"Failed to insert %@, %@",firstname.text,lastname.text];
    }
}

在运行代码之前,使用POSTMAN测试您的网址,是来自Google Chrome的应用。 问候。