如何从api获取数据?

时间:2018-07-04 06:28:26

标签: ios objective-c authentication url

嗨,我给了链接

"https://joysalescript.com/api/service?ws=1"

,他们需要从该链接获取数据

我不知道该如何实现这一新技能,任何人都可以帮助我。谢谢

3 个答案:

答案 0 :(得分:1)

**Simply Call and get yourData.**

-(void)getData
{

NSURL *url = [NSURL URLWithString:@"https://joysalescript.com/api/service?ws=1"];

NSURLSession *session = [NSURLSession sharedSession];

NSURLSessionDataTask *data = [session dataTaskWithURL:url completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {

    NSError *erro = nil;

    if (data!=nil) {

        NSDictionary *json = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&erro ];
//if data return type is json supposed
        if (json.count > 0) {

            for(int i = 0; i<10 ; i++){

                [arr addObject:[[[json[@"feed"][@"entry"] objectAtIndex:i]valueForKeyPath:@"im:image"] objectAtIndex:0][@"label"]];
           }

        }
    }
    dispatch_sync(dispatch_get_main_queue(),^{

        [table reloadData];
    });
}];

[data resume];
}

答案 1 :(得分:0)

在邮递员中检查此链接。它没有显示响应。 我的建议是使用Alamofire进行API调用。

答案 2 :(得分:0)

您还可以使用SBJson Parser调用Api

-(void)resetCode
{

    NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];

    // getting an NSString
    NSString *id = [prefs stringForKey:@"ID"];
    NSString *post =[[NSString alloc] initWithFormat:@"newpassword=%@&confirmpassword=%@&id=%@",[_passwordTextField text],[_confirmPasswordTextfield text],id];
    NSLog(@"PostData: %@",post);


    NSURL * url = [NSURL URLWithString:@"https://joysalescript.com/api/service?ws=1"];

    NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];

    NSString *postLength = [NSString stringWithFormat:@"%lu", (unsigned long)[postData length]];

    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
    [request setURL:url];
    [request setHTTPMethod:@"POST"];
    [request setValue:postLength forHTTPHeaderField:@"Content-Length"];
    [request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
    [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
    [request setHTTPBody:postData];

    //[NSURLRequest setAllowsAnyHTTPSCertificate:YES forHost:[url host]];

    // NSError *error = [[NSError alloc] init];
    NSError *error = nil;

    NSHTTPURLResponse *response = nil;
    NSData *urlData=[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];

    NSLog(@"Response code: %d", [response statusCode]);
    if ([response statusCode] >=200 && [response statusCode] <300)
    {
        NSString *responseData = [[NSString alloc]initWithData:urlData encoding:NSUTF8StringEncoding];
        NSLog(@"Response ==> %@", responseData);
        SBJsonParser *jsonParser = [SBJsonParser new];
        NSDictionary *jsonData = (NSDictionary *) [jsonParser objectWithString:responseData error:nil];
        NSLog(@"%@",jsonData);
        NSInteger status = [(NSNumber *) [jsonData objectForKey:@"status"] integerValue];
        NSLog(@"%ld",(long)status);
        if(status == 1)
        {

            forgotPasswordViewController *secondVC = [self.storyboard instantiateViewControllerWithIdentifier:@"loginVC"];
            [self presentViewController:secondVC animated:YES completion:nil];

        }
        else
        {
            NSLog(@"yash");

        }


    }
    else {
        if (error) NSLog(@"Error: %@", error);
        [self alertFailed:@"Login Failed!" :@"The network connection appears to be offline."];

        [hud hideAnimated:YES];



    }
}