Ich verwende diesen Code, um eine Anfrage an den Server zu stellen. Ich habe die Antwort in completionHandler Block dann möchte ich in succes Block passieren, aber ich habe nullkeine Antwortdaten im Abschlussblock erhalten
-(void)getJsonResponse : (NSString *)urlStr success : (void (^)(NSDictionary *responseDict))success failure:(void(^)(NSError* error))failure
{
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:url]];
[request setHTTPMethod:@"POST"];
NSURLSession *session = [NSURLSession sharedSession];
[[session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
NSLog(@"error====%@",error);
NSString *str = [[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
NSLog(@"str is %@",str);
// Convert to JSON object:
NSError *jsonerror = nil;
NSData *data1 = [str dataUsingEncoding:NSUTF8StringEncoding];
NSDictionary *jsondistionary = [NSJSONSerialization JSONObjectWithData:data1 options:0 error:&jsonerror];
NSLog(@"str is JSON ERROR %@",jsonerror);
NSLog(@"%@", jsondistionary); here i am getting data
success(jsondistionary) ;
}]resume];
}
dieses i mein Call-Methode hier i Antwort erhalten möchten, aber null
[self getJsonResponse:SERVER_API_URL success:^(NSDictionary *responseDict) {
NSLog(@"%@ responseDict ====",responseDict);
/here i am getting null
} failure:^(NSError *error) {
// error handling here ...
}];
bekommen, was ist falsch in Methode
Verwendung Geleistete REST-Client und sehen, was du da Problem sein könnte mit webservice Seite –
stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding auf Ihre URL-Zeichenfolge –
als Schnelltest Ihre SERVER_API_URL einzuloggen und kopieren Sie diese URL in den Browser einfügen, sehen, wenn Sie habe gültige Antworten. – StefanS