没有通过使用http post方法获得服务器响应& nsurlsession

时间:2016-09-23 06:58:14

标签: ios objective-c nsurlsession nsurlrequest

NSError *error;
NSDictionary *dicData = [[NSDictionary alloc]initWithObjectsAndKeys:username,@"username",password,@"password",cpassword,@"cpassword",mobile,@"mobile",firstname,@"firstname",lastname,@"lastname",@"register",@"action",nil];
NSLog(@"parameter=%@",dicData);

NSURLComponents *components = [NSURLComponents componentsWithString:@"http://followerlikes.com/app_appoint/json/?action=register"];

NSMutableArray *queryItems = [NSMutableArray array];
for (NSString *key in dicData) {
    [queryItems addObject:[NSURLQueryItem queryItemWithName:key value:dicData[key]]];
}
components.queryItems = queryItems;

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:components.URL];

[request setHTTPMethod:@"POST"];
[request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
NSData *postData = [NSJSONSerialization dataWithJSONObject:dicData options:0 error:&error];
[request setHTTPBody:postData];

NSURLSession *session = [NSURLSession sharedSession];
NSURLSessionDataTask *task = [session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
    NSLog(@"%@",data);
    NSLog(@"%@",response);
    NSLog(@"%@",error);

    NSString *strRes = [[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
    NSLog(@"%@",strRes);
    NSError *resultError;

    NSDictionary *dicResult = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&resultError];
    dispatch_async(dispatch_get_main_queue(), ^
        {
            if (error !=nil) {
                NSLog(@"%@",error.description);
                NSLog(@"%@",error.localizedDescription);
            }
            else {
                completion(dicResult);
            }
        });
    }];

    [task resume];
}

1 个答案:

答案 0 :(得分:0)

为了发送获取数据的请求,您必须在info.plist中添加密钥,如下所示:

enter image description here

只需更新您的代码,如下所示:

NSError *error;
NSDictionary *dicData = [[NSDictionary alloc] initWithObjectsAndKeys:@"user123", @"username", @"pass1234", @"password", @"pass1234", @"cpassword", @"0123456789", @"mobile", @"User", @"firstname", @"Name", @"lastname", @"register", @"action", nil];
NSLog(@"parameter=%@",dicData);

//    NSURLComponents *components = [NSURLComponents componentsWithString:@"http://followerlikes.com/app_appoint/json/?action=register"];
//    
//    NSMutableArray *queryItems = [NSMutableArray array];
//    for (NSString *key in dicData) {
//        [queryItems addObject:[NSURLQueryItem queryItemWithName:key value:dicData[key]]];
//    }
//    components.queryItems = queryItems;




NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"http://followerlikes.com/app_appoint/json/?action=register"]];
[request setHTTPMethod:@"POST"];
[request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
NSData *postData = [NSJSONSerialization dataWithJSONObject:dicData options:0 error:&error];
[request setHTTPBody:postData];


NSURLSession *session = [NSURLSession sharedSession];
NSURLSessionDataTask *task = [session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {

    NSLog(@"Data : %@",data);
    NSLog(@"RESPONSE : %@",response);
    NSLog(@"ERROR : %@",error);

    NSString *strRes = [[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
    NSLog(@"%@",strRes);
    NSError *resultError;

    NSDictionary *dicResult = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&resultError];
    NSLog(@"RESPONSE DICT : %@", dicResult);

    dispatch_async(dispatch_get_main_queue(), ^ {

       if (error !=nil) {
           NSLog(@"ERROR : %@",error.description);
           NSLog(@"ERROR : %@",error.localizedDescription);
       }
       else {
//                               completion(dicResult);
       }
   });
}];

[task resume];