我的带有 sendSynchronousRequest 的 NSURLConnection 代码工作正常,但我怎样才能将其更改为异步请求?我尝试了很多,但没有任何效果。
如果请求为空,我将得到一个带有 [[]] 的空数组。我怎样才能捕捉到它的警报消息?
请帮忙 ...
[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
NSString *urlString = @"http://www.xyz.at/sample.php";
NSURL *url = [NSURL URLWithString:urlString];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:url];
[request setHTTPMethod:@"POST"];
NSMutableData *body = [NSMutableData data];
NSString *postWerte = [NSString stringWithFormat:@"id=%@", self.textfeld.text];
[body appendData:[postWerte dataUsingEncoding:NSUTF8StringEncoding]];
[request setHTTPBody:body];
NSError *error = nil;
NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:&error];
NSLog(@"Error: %@", error.description);
NSString *returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];
const char *convert = [returnString UTF8String];
NSString *responseString = [NSString stringWithUTF8String:convert];
NSMutableArray *meinErgebnis = [responseString JSONValue];
NSString *cycle = @"";
NSString *kopfdaten = [NSString stringWithFormat:@"Sendungsart: %@\r\nGewicht: %@ kg\r\n\r\n", [[meinErgebnis objectAtIndex:0] objectForKey:@"ParcelTypeDescription"], [[meinErgebnis objectAtIndex:0] objectForKey:@"Weight"]];
cycle = [cycle stringByAppendingString:kopfdaten];
for(int i = 1; i < meinErgebnis.count; i++)
{
NSString *myValue = [NSString stringWithFormat:@"%@ PLZ: %@\r\nStatus: %@\r\n\r\n",
[[meinErgebnis objectAtIndex:i] objectForKey:@"EventTimestamp"],
[[meinErgebnis objectAtIndex:i] objectForKey:@"EventPostalCode"],
[[meinErgebnis objectAtIndex:i] objectForKey:@"ParcelEventReasonDescription"]];
cycle = [cycle stringByAppendingString:myValue];
}
self.ergebnis.text = [NSString stringWithFormat:@"%@", cycle];
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
[self.textfeld resignFirstResponder];