我从 Web 服务(我的流程中的 Web 服务 2)获得四个参数 - slno、订单、标志、名称。我不知道这些参数会被接收多少次。在这四个参数中,我将“名称”发送到标签,因为它包含要问的问题。
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"some url"]];
NSLog(@"Web service 2 url is = %@", url);
NSString *json = [NSString stringWithContentsOfURL:url encoding:NSASCIIStringEncoding error:&error];
NSLog(@"Json data = %@ \n error = %@", json, error);
if(!error)
{
NSData *jsonData = [json dataUsingEncoding:NSASCIIStringEncoding];
NSArray *myJsonArray = [NSJSONSerialization JSONObjectWithData:jsonData options:0 error:Nil];
//NSArray *arrayLabel = [[NSArray alloc] initWithObjects:label1, label2, label3, label4, label5, label6, nil];
//NSMutableArray *tempArray = [NSMutableArray arrayWithCapacity:myJsonArray.count];
i = 0;
for(NSDictionary *myJsonDictionary in myJsonArray)
{
//UILabel *label = (UILabel *)[arrayLabel objectAtIndex:i++];
//[label setText:myJsonDictionary[@"Name"]];
NSString *name = myJsonDictionary[@"Name"];
NSLog(@"Question from ws2 is %@", name);
projectIdGobal = myJsonDictionary[@"ProjectID"];
NSLog(@"Project id from ws2 is %@", projectIdGobal);
slno = myJsonDictionary[@"SLNO"];
NSLog(@"slno from ws2 is %@", slno);
NSString *idWS2 = myJsonDictionary[@"ID"];
NSLog(@"id from ws2 is %@", idWS2);
order = myJsonDictionary[@"Order"];
NSLog(@"order from ws2 is %@", order);
flag = myJsonDictionary[@"Flag"];
NSLog(@"flag from ws2 is %@", flag);
[self putLabelsInScrollView:name];
i++;
}
NSLog(@"Number of cycles in for-each = %d", i);
[activity stopAnimating];
}
- (void) putLabelsInScrollView:(NSString *)labelText
{
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10, yPosition_label, 261, 30)];
[label setFont:[UIFont fontWithName:@"Helvetica Neue" size:12.0f]];
label.numberOfLines = 2;
[label setText:labelText];
[self.scroll addSubview:label];
yPosition_label += 90;
UITextField *text = [[UITextField alloc] initWithFrame:CGRectMake(10, yPosition_text, 261, 30)];
text.borderStyle = UITextBorderStyleRoundedRect;
text.textColor = [UIColor blackColor];
text.font = [UIFont systemFontOfSize:12.0];
text.backgroundColor = [UIColor clearColor];
text.keyboardType = UIKeyboardTypeDefault;
text.delegate = self;
[self.scroll addSubview:text];
yPosition_text += 90;
yPosition_result = yPosition_label + yPosition_text;
[self.scroll setContentSize:CGSizeMake(self.scroll.frame.size.width, yPosition_result)];
[self.view addSubview:self.scroll];
}
现在我创建了一个动态创建的文本字段,并将用户输入的答案存储在数组中,如下所示。
- (IBAction)save:(id)sender {
NSMutableArray *mutableTextArray = [[NSMutableArray alloc] init];
for(UITextField *field in self.scroll.subviews)
{
if([field isKindOfClass:[UITextField class]])
{
if([[field text] length] > 0)
{
[mutableTextArray addObject:field.text];
//NSLog(@"Save button 1 : %@", mutableTextArray);
//NSString *str = [str stringByAppendingString:[mutableTextArray objectAtIndex:0]];
//[self fetchStrings:mutableTextArray];
}
}
}
NSLog(@"Save button 2 : %@", mutableTextArray);
[self fetchStrings:mutableTextArray];
}
现在,在将答案发布到另一个 Web 服务(我的流程中的 Web 服务 3)时,我必须将我从 Web 服务 2 获得的 slno、订单、标志以及用户在动态创建的字段中输入的“答案”传递给“答案” ' 钥匙。我该如何获取这 4 个参数 [slno、order、flag(来自 web 服务 2)和 answer(来自动态文本字段)] 以发布到 web 服务 3?
- (void) fetchStrings:(NSArray *)textArray
{
NSLog(@"Array string = %@", textArray); //I get the array that the user enters in the dynamically created text field here
NSOperationQueue *queue = [[NSOperationQueue alloc] init];
NSUserDefaults *getDefaults = [NSUserDefaults standardUserDefaults];
NSString *uidObject = [getDefaults objectForKey:@"UIDKEY"];
NSString *str = [NSString stringWithFormat:@"{\"ProjID\": \"%@\",\"Uid\": \"%@\",\"EmailID\": \"%@\",", projectIdGobal, uidObject, emailFromLogin];
str = [str stringByAppendingString:@"\"ProjectInviterFQAnswers\": ["];
**for (SaveAsking *saveAsk in textArray) {
str = [str stringByAppendingString:[NSString stringWithFormat:@"{\"slno\":\"%@\",\"Answer\": \"%@\",\"order\": \"%@\", \"flag\": \"%@\"},", saveAsk.slno, saveAsk.answer, saveAsk.order, saveAsk.flag]]; // I want to save the parameters here
}**
// SaveAsking is a nsobject class where I have used a self created delegate for slno answer order and flag
str = [str stringByAppendingString:@"]}"];
NSLog(@"String is === %@", str);
NSURL *url = [NSURL URLWithString:@"some url"];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url];
NSData *requestData = [str dataUsingEncoding:NSUTF8StringEncoding];
[request setHTTPMethod:@"POST"];
[request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[request setValue:[NSString stringWithFormat:@"%d", [requestData length]] forHTTPHeaderField:@"Content-Length"];
[request setHTTPBody: requestData];
NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
[connection start];
[NSURLConnection sendAsynchronousRequest:request queue:queue completionHandler:^(NSURLResponse *response, NSData *data, NSError *error){
if(error || !data)
{
NSLog(@"JSON Data not posted!");
[activity stopAnimating];
UIAlertView *alertMessage = [[UIAlertView alloc] initWithTitle:@"Error" message:@"Data not saved" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alertMessage show];
}
else
{
[activity startAnimating];
NSLog(@"JSON data posted! :)");
NSError *error = Nil;
NSJSONSerialization *jsonObject = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:&error];
NSLog(@"Response is %@", jsonObject);
}
}];
}
如果您了解我要达到的目标,请纠正我的流程。左框中的迭代次数 == 右框中的迭代次数,结果在中间框中,需要发布到 Web 服务。