F515249B-CC3F-4F22-B5EB-20D2B4F68EB1.png
錯誤示例:
//1.創建一個網絡路徑
NSURL *url = [NSURL URLWithString:urlString];
// 2.創建一個網絡請求
NSURLRequest *request =[NSURLRequest requestWithURL:url];
// 3.獲得會話對象
NSURLSession *session = [NSURLSession sharedSession];
// 4.根據會話對象,創建一個Task任務
NSURLSessionDataTask *sessionDataTask = [session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse*)response;
NSLog(@"從服務器獲取到數據 %ld",(long)httpResponse.statusCode);
/*對從服務器獲取到的數據data進行相應的處理:*/
NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:(NSJSONReadingMutableLeaves) error:nil];
NSLog(@"%@",dict);
}];
// 5.最后一步,執行任務(resume也是繼續執行):
[sessionDataTask resume];```
>正確示例
``` Nsstring*urlString=@"https://graph.facebook.com/debug_token?input_token=/*facebook return token*/&access_token=/*apptoken*/";```
###urlString=[urlString stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];###
``` NSURL *url = [NSURL URLWithString:urlString];;```