Frage

My Code for NSURLConnection with sendSynchronousRequest works fine but how can i change it to an async request? i tried a lot but nothing would work.

if the Request is empty i´ll get an empty Array with [[]] . How can i catch it for an Alert Message?

Please help ...

     [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];
War es hilfreich?

Lösung

You could:

  • create an NSOperationQueue, and

  • call sendAsynchronousRequest, placing all of your NSData processing code inside the completion block.

Thus, instead of:

NSURLResponse *response = nil;
NSError *error = nil;
NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];

// now process resulting `data`

Use:

NSOperationQueue *queue = [[NSOperationQueue alloc] init];
[NSURLConnection sendAsynchronousRequest:request queue:queue completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {

    // now process resulting `data`
}];

Alternatively, you could implement the NSURLConnectionDataDelegate methods. For more information on that, see the Using NSURLConnection section of the URL Loading System Programming Guide.


You say "if the request is empty": I assume you mean "if the data returned is empty". And you say it is [[]]. If that's really what you're getting, it sounds like an array with one item (which itself, is an empty array). Or is it [] (which is an empty array)? Or is it nil?

I'm going to assume that the data returned was [], an empty array.

I'd also suggest you consider using NSJSONSerialization, the built in JSON parser, but obviously you can use JSONValue if you really want.

Finally, your implementation is skipping the first entry (NSArray uses a zero-based index). I'm assuming that was unintentional.

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.
}];
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top