0

私の 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 に渡しています。パラメータの受け渡しに問題があります。ご覧のとおり、IGetEmployees.vb と GetEmployees.vb の 2 つのファイルがあります。メソッド GetAllContactsMethod を実装しています。何が起こっているかというと、GetEmployees.vb ファイル (Utilities.log) の 2 行で、ログに記録されません。関数はまったく呼び出されていません。

この関数を呼び出す私の目的の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 側で関数がまったく呼び出されないのはなぜですか。さらに情報が必要な場合は、お問い合わせください。ありがとうございます...

4

1 に答える 1

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];
    }
}

コードを実行する前に、Google Chrome のアプリである POSTMAN で URL をテストしてください。よろしく。

于 2015-04-29T16:20:14.827 に答える