如何在AFNETWORKING IOS中将NSDictionary作为参数传递

时间:2015-02-27 06:28:28

标签: ios json networking afnetworking afnetworking-2

 NSMutableDictionary *mainLoginDict=[[NSMutableDictionary alloc]init];
    NSMutableDictionary *logindict = [[NSMutableDictionary alloc] init];
    [logindict setObject:_emailtext.text forKey:@"email"];
    [logindict setObject:_passwordtext.text forKey:@"password"];
    [logindict setObject:@"" forKey:@"fbid"];
    // [logindict setObject:@"" forKey:@"twitter_id"];
    [mainLoginDict setObject:logindict forKey:@"login"];

    NSLog(@"The dictionary %@",mainLoginDict);

    NSError *writeError = nil;
    NSData *requestData = [NSJSONSerialization dataWithJSONObject:mainLoginDict options:NSJSONWritingPrettyPrinted error:&writeError];

    NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"%@login",QSURL]];

    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url
                                                           cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:1660.0];


    //  NSData *requestData = [NSData dataWithBytes:[jsonRequest UTF8String] length:[jsonRequest length]];

    [request setHTTPMethod:@"POST"];
    [request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
    [request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
    [request setValue:[NSString stringWithFormat:@"%lu", (unsigned long)[requestData length]] forHTTPHeaderField:@"Content-Length"];
    [request setHTTPBody: requestData];

    NSError *error;
    NSURLResponse *response;
    NSData *urlData=[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
    //   NSString *XMLString=[[NSString alloc]initWithData:urlData encoding:NSUTF8StringEncoding];
    NSArray  *jsonDic = (NSArray *)[NSJSONSerialization JSONObjectWithData:urlData options:kNilOptions error:&error];


    NSLog(@"the  is %@",jsonDic);

这是用作同步请求的代码。我是AFNetworking的新手,有人可以帮助我,我可以在AFNetworking的帮助下传递这样的字典。

0 个答案:

没有答案