通过目标C调用Web服务方法未命中 [英] Web service method not hit when called via Objective C

查看:104
本文介绍了通过目标C调用Web服务方法未命中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的App_Code/IGetEmployees.vb文件

My App_Code/IGetEmployees.vb file

<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文件

My App_Code/GetEmployees.vb file

   <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 Server检索信息,并将其以JSON格式传递给我的iPad.我有一个参数传递问题.因此,就像您看到的那样,我有2个文件IGetEmployees.vb和GetEmployees.vb.我正在实现方法GetAllContactsMethod.发生的是GetEmployees.vb文件(Utilities.log)中的两行,但从未记录.该函数根本没有被调用.

NContactNames is a class with 3 properties. So i am using ASP.NET web services to retrieve information from SQL server and pass it to my iPad in JSON format. I have a problem with parameter passing. So like you see i have 2 files IGetEmployees.vb and GetEmployees.vb. I am implementing the method GetAllContactsMethod. What's happening is the two lines in GetEmployees.vb file (Utilities.log), they never get logged. The function is not getting called at all.

我要调用此函数的目标c代码

My objective c code to call this function

    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端根本没有调用该函数.如果您需要更多信息,请询问.谢谢...

At the time of posting this code the "if" statement is true and (null) is printed followed by "Done" The output of my absolute request is: request string is http://xyz-dev.com/GetEmployees.svc/json/contactoptions This is the first time i am writing json to accept parameters. So i might be missing something.What is it?Why is the function not getting called at all on the Visual Studio side. If you need more info please ask.Thanks...

推荐答案

为此,这是Objetive-C中的方法.

this is the moethod in Objetive-C, for that.

-(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测试您的URL,这是来自Google Chrome的应用程序. 问候.

Before to run the code, test your URL with POSTMAN, is an app from Google Chrome. regards.

这篇关于通过目标C调用Web服务方法未命中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆