iOS AFNetWorking 請求json數據

jopen 9年前發布 | 2K 次閱讀 Objective-C AFNetworking

1.請求json數據,使用AFHTTPRequestOperation完成

    NSString *str=[NSString stringWithFormat:@"http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=json&ip=218.4.255.255"];

NSURL *url = [NSURL URLWithString:[str stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];                        // UTF-8
NSURLRequest *request = [NSURLRequest requestWithURL:url];
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc]initWithRequest:request];
// 請求數據,設置成功與失敗的回調函數
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
    NSString *html = operation.responseString;
    NSData* data=[html dataUsingEncoding:NSUTF8StringEncoding];
    id dict=[NSJSONSerialization  JSONObjectWithData:data options:0 error:nil];
    NSLog(@"獲取到的數據為:%@",dict);
}failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    NSLog(@"發生錯誤!%@",error);
}];
// 加入隊列
NSOperationQueue *queue = [[NSOperationQueue alloc] init];
[queue addOperation:operation];</pre> 


 本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!