收到令牌后立即获取请求

时间:2015-08-29 04:38:54

标签: ios mashape

我正在使用camfind api,我发布请求,我发送api图像并获取令牌,然后我可以使用令牌获取图像,但这需要两个步骤,一个帖子和获取请求。我想组合它们,因此当服务器响应post请求时,get请求会自动触发。这是代码:

我在顶部

指定了令牌的值
 NSString *tokenValue;

请求方法,这里选择一个图像并发送到服务器,服务器在此请求中发回一个令牌响应

- (void *) requestMethod: (UIImage *)imageToConvert{

    NSString *temp = @"saved";
    NSLog(@"%@", temp);
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentDirectoryPath = [paths objectAtIndex:0];
    NSString *imagePath = [documentDirectoryPath stringByAppendingPathComponent:@"tmp_image.jpg"];
    NSURL *imageURL = [NSURL fileURLWithPath:imagePath];

    NSData *imageData = UIImageJPEGRepresentation(imageToConvert , 1.0);
    [imageData writeToURL:imageURL atomically:YES];
    //    NSString *base64image = [NSString stringWithFormat:@"%@",[imageData base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength]];
    NSString *base64image = [imageData base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength];

    // These code snippets use an open-source library.
    //  NSString *baseboy =  [base64image stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    // NSURL *urlimage_request = [NSURL URLWithString:[base64image stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
    NSDictionary *headers = @{@"X-Mashape-Key": @"********"};
    NSDictionary *parameters = @{@"focus[x]": @"480", @"focus[y]": @"640", @"image_request[altitude]": @"27.912109375", @"image_request[image]": imageURL, @"image_request[language]": @"en", @"image_request[latitude]": @"35.8714220766008", @"image_request[locale]": @"en_US", @"image_request[longitude]": @"14.3583203002251"};

    UNIUrlConnection *asyncConnection = [[UNIRest post:^(UNISimpleRequest *request) {
        [request setUrl:@"https://camfind.p.mashape.com/image_requests"];
        [request setHeaders:headers];
        [request setParameters:parameters];
    }] asJsonAsync:^(UNIHTTPJsonResponse *response, NSError *error) {
        NSInteger code = response.code;
        NSDictionary *responseHeaders = response.headers;
        UNIJsonNode *body = response.body;
        NSData *rawBody = response.rawBody;
        NSString *token = response.description;
        NSDictionary *json = [NSJSONSerialization JSONObjectWithData:response.rawBody
                                                             options:kNilOptions
                                                               error:nil];
        NSLog(@"Response status: %ld\n%@", (long) response.code, json);
        for(NSString *key in [json allValues])
        {


            tokenValue = [json valueForKey: @"token" ];
            NSString *two = [json valueForKey: @"token" ]; // assuming the value is indeed a string
            NSLog(@"Token :%@", two);
            NSString *one = @"https://camfind.p.mashape.com/image_responses" ;
            NSDictionary *headers = @{@"X-Mashape-Key": @"9hcyYCUJEsmsh4lNTgpgVX1xRq0Ip1uogovjsn5Mte0ONVBtes", @"Accept": @"application/json"};
            NSString *responseString = [NSString stringWithFormat:@"%@/%@", one, two];


            // NSString *responseURL = [one stringByAppendingString:two];
            NSLog(@"response URL %@", responseString);
            //            UNIUrlConnection *asyncConnection = [[UNIRest get:^(UNISimpleRequest *request) {
            //                [request setUrl:responseString];
            //                [request setHeaders:headers];
            //            }] asJsonAsync:^(UNIHTTPJsonResponse *response, NSError *error) {
            //                NSInteger code = response.code;
            //                NSDictionary *responseHeaders = response.headers;
            //                UNIJsonNode *body = response.body;
            //                NSData *rawBody = response.rawBody;
            //                NSDictionary *json = [NSJSONSerialization JSONObjectWithData:response.rawBody
            //                                                                     options:kNilOptions
            //                                                                       error:nil];
            //                NSLog(@"Response status: %ld\n%@", (long) response.code, json);
            //
            //
            //                //              NSLog(@"didfinishLoadingbody%@",rawBody);
            //                //        NSLog(@"didfinishLoadingbody%@",body);
            //                //
            //               // NSLog(@"didfinishLoading responseheader%@",responseHeaders);
            //                //           NSLog(@"didfinishLoading tok%@",token);
            //            }];







        }

        //              NSLog(@"didfinishLoadingbody%@",rawBody);
        //        NSLog(@"didfinishLoadingbody%@",body);
        //
        // NSLog(@"didfinishLoading responseheader%@",responseHeaders);
        //           NSLog(@"didfinishLoading tok%@",token);

    }];
    return (__bridge void *)(self);
}

下面的响应方法使用上面请求中的令牌来打印图像

- (void *) responseMethod {
    // These code snippets use an open-source library.
    NSString *one = @"https://camfind.p.mashape.com/image_responses" ;

    NSString *responseString = [NSString stringWithFormat:@"%@/%@", one, tokenValue];

    NSDictionary *headers = @{@"X-Mashape-Key": @"9hcyYCUJEsmsh4lNTgpgVX1xRq0Ip1uogovjsn5Mte0ONVBtes", @"Accept": @"application/json"};
    UNIUrlConnection *asyncConnection = [[UNIRest get:^(UNISimpleRequest *request) {
        [request setUrl:responseString];
        [request setHeaders:headers];
    }] asJsonAsync:^(UNIHTTPJsonResponse *response, NSError *error) {
        NSInteger code = response.code;
        NSDictionary *responseHeaders = response.headers;
        UNIJsonNode *body = response.body;
        NSData *rawBody = response.rawBody;
        NSDictionary *json = [NSJSONSerialization JSONObjectWithData:response.rawBody
                                                             options:kNilOptions
                                                               error:nil];
        NSLog(@"Response status: %ld\n%@", (long) response.code, json);


        //              NSLog(@"didfinishLoadingbody%@",rawBody);
        //        NSLog(@"didfinishLoadingbody%@",body);
        //
        NSLog(@"didfinishLoading responseheader%@",responseHeaders);
        //           NSLog(@"didfinishLoading tok%@",token);
    }];
    return (__bridge void *)(self);
}

其他vc其他东西

 -(void)loadView {
        [super loadView];


    }

    - (void)viewDidLoad{
        [super viewDidLoad];
    }

    -(void)viewDidAppear:(BOOL)animated{
        [super viewDidAppear:animated];
    }

    -(void)didReceiveMemoryWarning {
        [super didReceiveMemoryWarning];

    }
    -(void)viewWillDisappear:(BOOL)animated{
        [super viewWillDisappear:animated];

    }

我不知道如何在服务器回复时立即启动响应方法,我不知道objc,我在快速项目中使用它

@end

0 个答案:

没有答案