我的带有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];
答案 0 :(得分:3)
你可以:
创建NSOperationQueue
和
调用sendAsynchronousRequest
,将所有NSData
处理代码放在完成块中。
因此,而不是:
NSURLResponse *response = nil;
NSError *error = nil;
NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
// now process resulting `data`
使用:
NSOperationQueue *queue = [[NSOperationQueue alloc] init];
[NSURLConnection sendAsynchronousRequest:request queue:queue completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
// now process resulting `data`
}];
或者,您可以实现NSURLConnectionDataDelegate
方法。有关详细信息,请参阅 URL加载系统编程指南的Using NSURLConnection部分。
你说“如果请求是空的”:我认为你的意思是“如果返回的数据是空的”。你说它是[[]]
。如果这真的是你得到的,它听起来像一个带有一个项目的数组(它本身就是一个空数组)。或者它是[]
(这是一个空数组)?或者是nil
?
我将假设返回的数据是[]
,一个空数组。
我还建议您考虑使用内置JSON解析器的NSJSONSerialization
,但显然如果您真的需要,可以使用JSONValue
。
最后,您的实现正在跳过第一个条目(NSArray
使用从零开始的索引)。我认为这是无意的。
NSOperationQueue *queue = [[NSOperationQueue alloc] init];
[NSURLConnection sendAsynchronousRequest:request queue:queue completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
if (error) {
NSLog(@"%s: sendAsynchronousRequest error: %@", __FUNCTION__, error);
return;
}
NSError *parseError;
NSArray *meinErgebnis = [NSJSONSerialization JSONObjectWithData:data options:0 error:&parseError];
if (parseError) {
NSLog(@"%s: JSONObjectWithData error: %@", __FUNCTION__, parseError);
return;
}
if ([meinErgebnis count] == 0) {
NSLog(@"%s: meinErgebnis empty", __FUNCTION__);
return;
}
for (NSDictionary *dictionary in meinErgebnis)
{
// now process each dictionary entry in meinErgebnis
}
// etc.
}];