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];
You could:
create an
NSOperationQueue
, andcall
sendAsynchronousRequest
, placing all of yourNSData
processing code inside the completion block.Thus, instead of:
Use:
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 itnil
?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 useJSONValue
if you really want.Finally, your implementation is skipping the first entry (
NSArray
uses a zero-based index). I'm assuming that was unintentional.