我找到了一个接一个地在一个方法中运行2个服务的方法,一个接一个地发布图像。在第一次服务之后我需要得到回应然后我需要将该响应传递给第二服务。
我用来发布单个服务以发布单张图片的代码是
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];
stackoverflow中可能存在一些类似的问题,但我的需求完全不同。
方法的流程必须是这样的 *运行第一个网址 - >生成响应({userid:“20”,message:“success”}) - >运行第二个网址*
帮助我,先谢谢大家。
答案 0 :(得分:1)
你可以调用关于第一种方法的响应的第二种方法
-(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];
}
来自Web服务的响应:
-(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];
}
}
第二个WebService:
-(void)CallToSecondWebService
{
}
答案 1 :(得分:0)
http://codewithchris.com/tutorial-how-to-use-ios-nsurlconnection-by-example/
NSURLConnection synchronous request on https
how to send Asynchronous URL Request?
您可以根据自己的选择阅读所有这些内容..
答案 2 :(得分:0)
也许这会对你有帮助。
-(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"]];
}
}
答案 3 :(得分:0)
我强烈反对你使用同步连接,他们阻止线程直到完成,如果这个线程是主线程,它们将阻止用户交互。此外,您无法控制发送的块或auth挑战
在您的情况下,最好的方法是使用AFNetworking 2之类的网络管理器并创建不同的网络操作,之后您可以在它们之间添加依赖关系(如果您愿意,可以将它们链接起来)。
另一种方式是dispatch_group。您可以将操作(或会话)添加到组中,并等待它们结束。
[更新]
在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];