Подтвердить что ты не робот

Как отправить запрос Get в iOS?

Я создаю библиотеку, чтобы получить ответ от определенного URL с указанными данными и типом метода. Для этого я делаю запрос с URL-адресом. Но когда я устанавливаю его тип метода, он показывает исключение нераспознанного селектора отправки в [NSURLRequest setHTTPMethod:] Я устанавливаю его как

[requestObject setHTTPMethod:@"GET"];

Скажите, что может быть проблемой. Также дайте мне код, если у вас есть.

4b9b3361

Ответ 1

NSMutableURLRequest *request = 
[NSMutableURLRequest requestWithURL:[NSURL 
            URLWithString:serverAddress] 
            cachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData
                                                   timeoutInterval:10
 ];

[request setHTTPMethod: @"GET"];

NSError *requestError = nil;
NSURLResponse *urlResponse = nil;


NSData *response1 =
        [NSURLConnection sendSynchronousRequest:request
                         returningResponse:&urlResponse error:&requestError];

Ответ 2

NSString *getString = [NSString stringWithFormat:@"parameter=%@",yourvalue];
NSData *getData = [getString dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSString *getLength = [NSString stringWithFormat:@"%d", [getData length]];
NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
[request setURL:[NSURL URLWithString:@"https:yoururl"]];
[request setHTTPMethod:@"GET"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:getData];
self.urlConnection = [[[NSURLConnection alloc] initWithRequest:request delegate:self] autorelease];
NSAssert(self.urlConnection != nil, @"Failure to create URL connection.");
// show in the status bar that network activity is starting [UIApplication sharedApplication].networkActivityIndicatorVisible = YES;

Ответ 3

Убедитесь, что ваш объект requestObject имеет тип NSMutableURLRequest.

Ответ 4

Просто позвоните и используйте:

(void)jsonFetch{

    NSURL *url = [NSURL URLWithString:@"http://itunes.apple.com/us/rss/topaudiobooks/limit=10/json"];

    NSURLSession *session = [NSURLSession sharedSession];

    NSURLSessionDataTask *data = [session dataTaskWithURL:url completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {

        NSError *erro = nil;

        if (data!=nil) {

            NSDictionary *json = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&erro ];

            if (json.count > 0) {

                for(int i = 0; i<10 ; i++){

                    [arr addObject:[[[json[@"feed"][@"entry"] objectAtIndex:i]valueForKeyPath:@"im:image"] objectAtIndex:0][@"label"]];

                }

            }
        }
        dispatch_sync(dispatch_get_main_queue(),^{

            [table reloadData];
        });
    }];

    [data resume];
}