1

I am finding for the way to run 2 services in a method one after another to post images one by one . after the 1st service i need to get response then i need pass that response to 2nd service.

The code i've used to post run a single service to post single image is

NSString *url=[NSString stringWithFormat:@"http://37.187.152.236/UserImage.svc/InsertObjectImage?%@",requestString];
NSLog(@"url1%@",url);
 NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init] ;
[request setURL:[NSURL URLWithString:url]];
[request setHTTPMethod:@"POST"];

// Create 'POST' MutableRequest with Data and Other Image Attachment.

NSString *boundary = @"---------------------------14737809831466499882746641449";
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@", boundary];
[request setValue:contentType forHTTPHeaderField:@"Content-Type"];
NSData *data = UIImageJPEGRepresentation(chosenImage1, 0.2f);
[request addValue:@"image/JPEG" forHTTPHeaderField:@"Content-Type"];
NSMutableData *body = [NSMutableData data];
[body appendData:[NSData dataWithData:data]];
[request setHTTPBody:body];

some similar questions may be there in stackoverflow but my need is completely different.

Flow of method must be like this *Run 1st url --> generate response ({userid:"20",message:"success"} ) --> run 2nd url *

help me, thanks in advance for everyone.

Code cracker
  • 2,782
  • 6
  • 31
  • 57
  • u can use two way one is synchronous & another is asynchronous.. In both whenever u get your desired output from first url you can send request to second url.. – Alfa Jun 27 '14 at 04:34
  • thanks for your response @Alfa . can u improve ur answer please. i am new to ios – Code cracker Jun 27 '14 at 04:38

4 Answers4

1

you can call the second method with respect to the response of first method

-(void)webservicecall {
    WebApiController *obj=[[WebApiController alloc]init];

    NSMutableDictionary *imageparameter = [NSMutableDictionary dictionary];

    NSData *imagedata = UIImagePNGRepresentation(self.productImageView.image);

    [imageparameter setValue:imagedata forKey:@"image"];

    [obj callAPIWithImage:@"upload.php" WithImageParameter:imageparameter WithoutImageParameter:nil SuccessCallback:@selector(upload_response:Response:) andDelegate:self];

}

Response From Web Service:

-(void)upload_response:(NSString *)apiAlias Response:(NSData *)response {

    NSMutableDictionary *jsonDictionary=[NSJSONSerialization JSONObjectWithData:response options:NSJSONReadingMutableContainers error:nil];

    NSString *responseMsg=[[NSString alloc] initWithString:[jsonDictionary objectForKey:@"message"]];

    if ([responseCode isEqualToString:@"success"]) {
        [self CallToSecondWebService];

    }

}

Second WebService:

-(void)CallToSecondWebService
{

}
jherran
  • 3,113
  • 6
  • 32
  • 47
Vinaykrishnan
  • 702
  • 3
  • 16
  • 30
0

Maybe this will help you.

 -(void)getData:(NSString *)userid
{
    /*--- Your Current code here ---*/
    /*--- this is synchronous request you can use asynchronous as well ---*/
    NSURLResponse *response = nil;
    NSData *dataResponse = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:nil];

    /*--- Save your response if you want ---*/
    id Obj = [NSJSONSerialization JSONObjectWithData:dataResponse options:NSJSONReadingMutableLeaves error:nil];
    if ([Obj isKindOfClass:[NSArray class]])
    {
    /*--- here you get response ---*/
    /*--- I've created sample method that you can pass userid in same method ---*/
    /*--- Call same method or create new method and do same thing ---*/

    [self getData:[[Obj objectAtIndex:0] valueForKey:@"userid"]];
    }
}
ChintaN -Maddy- Ramani
  • 5,006
  • 1
  • 24
  • 46
0

I would strongly discourage you in using sync connections, they block the thread until they have finished and if this thread is the main thread, they will block the user interactions. Also you will not have control on the chunk sent or auth challenges.
In your case most probably the best is to use a network manger such as AFNetworking 2 and creating different network operations, after that you can add dependencies between them (thus chaining them if you like). The other way is dispatch_group. You can add the operations (or sessions) to a group and wait until they end.
[UPDATE]
In AFnetworking 2.0

NSDictionary * parameter = @{ParameterImage  : mainImage ? @"1" : @"0",};
NSError * __autoreleasing * constructingError = nil;   
AFHTTPRequestOperation * op1 = [self POST:ApiImageUploadURL parameters:parameter constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
        [formData appendPartWithFileURL:[NSURL fileURLWithPath:imagePath] name: ParameterImageData error:constructingError];
    } success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSError * error = nil;
        id objects = [NSJSONSerialization JSONObjectWithData:responseObject options:0 error:&error];
        DLog(@"Response %@", objects);
        NSString * imageID = [objects[@"id"] stringValue];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        DLog(@"Error%@", error);
    }];
// create operation op2
[op2 addDependecy:op1];
Andrea
  • 24,774
  • 10
  • 79
  • 121