2016-01-21 79 views
0

如何從iOS中的REST API url獲取響應數據。如何在ios中使用REST API url獲取響應數據

enter image description here

NSURL *url = [NSURL URLWithString:@"http://sssssssssssssssss"]; 
//NSData * JSONdata = [NSJSONSerialization dataWithJSONObject:dictionary options:0 error:&error]; 
     NSString *headerValue = [NSString stringWithFormat:@"%@" , testToken]; 
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url]; 
     [request setHTTPMethod:@"POST"]; 
     [request setValue:@"application/json; charset=utf-8" forHTTPHeaderField:@"Content-Type"]; 
     [request setHTTPBody:JSONBody]; 
     [request addValue:headerValue forHTTPHeaderField:@"oauth_token"]; 
     // examine the response 

由於提前

+0

要打印在從靜止API文本框或標籤數據? –

+0

@Abhi我想在控制檯中打印數據 –

回答

-1

使用https://github.com/AFNetworking/AFNetworking

AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager]; 
    [manager GET:@"yourURL.com"] parameters:@"Your pars to set it to nil" 
     success:^(AFHTTPRequestOperation *operation, id responseObject) { 
      //here you can responseObject content that return from API 
      NSLog(@"responseObject: %@", responseObject); 
     } failure:^(AFHTTPRequestOperation *operation, NSError *error) { 
      NSLog(@"Error: %@", error); 
     }]; 
1

您可以使用下面的代碼從REST API數據

{ 
NSString *post = [NSString stringWithFormat:@"parameter1=%@&parameter2=%@&parameter3=%@",parameter1txt]; // <--here put the request parameters you used to get the response 


    NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES]; 

    NSString *postLength = [NSString stringWithFormat:@"%lu", (unsigned long)[postData length]]; 

    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init]; 
    [request setURL:[NSURL URLWithString:@"http://your api"]]; 

    [request setHTTPMethod:@"POST"]; 
    [request setValue:postLength forHTTPHeaderField:@"Content-Length"]; 
    [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"]; 
    [request setHTTPBody:postData]; 

    NSURLResponse *response; 
    NSError *err; 
    NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&err]; 

    NSString *str=[[NSString alloc]initWithData:responseData encoding:NSUTF8StringEncoding]; 
    //NSLog(@"str : %@",str); 

    dict6 = [self cleanJsonToObject:responseData]; 
    NSLog(@"str : %@",dict6); 
} 

這裏dict6是NSMutableDictionary和最後NSLOG將打印在控制檯窗口中的響應

- (id)cleanJsonToObject:(id)data 
{ 
    NSError* error; 
    if (data == (id)[NSNull null]) 
    { 
     return [[NSObject alloc] init]; 
    } 
    id jsonObject; 
    if ([data isKindOfClass:[NSData class]]) 
    { 
     jsonObject = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error]; 
    } else 
    { 
     jsonObject = data; 
    } 
    if ([jsonObject isKindOfClass:[NSArray class]]) 
    { 
     NSMutableArray *array = [jsonObject mutableCopy]; 
     for (int i = (int)array.count-1; i >= 0; i--) 
     { 
      id a = array[i]; 
      if (a == (id)[NSNull null]) 
      { 
       [array removeObjectAtIndex:i]; 
      } else 
      { 
       array[i] = [self cleanJsonToObject:a]; 
      } 
     } 
     return array; 
    } else if ([jsonObject isKindOfClass:[NSDictionary class]]) 
    { 
     NSMutableDictionary *dictionary = [jsonObject mutableCopy]; 
     for(NSString *key in [dictionary allKeys]) 
     { 
      id d = dictionary[key]; 
      if (d == (id)[NSNull null]) 
      { 
       dictionary[key] = @""; 
      } else 
      { 
       dictionary[key] = [self cleanJsonToObject:d]; 
      } 
     } 
     return dictionary; 
    } else 
    { 
     return jsonObject; 
    } 
} 
0

這裏是一個雨燕3.0的答案: 雨燕3.0

let request = NSMutableURLRequest(url: NSURL(string: "http://httpstat.us/200")! as URL) 
let session = URLSession.shared 
request.httpMethod = "POST" 
let params = ["username":"username", "password":"password"] as Dictionary<String, String> 
request.httpBody = try? JSONSerialization.data(withJSONObject: params, options: []) 
request.addValue("application/json", forHTTPHeaderField: "Content-Type") 
request.addValue("application/json", forHTTPHeaderField: "Accept") 

let task = session.dataTask(with: request as URLRequest, completionHandler: {data, response, error -> Void in 
     if error != nil { 
      print("Error: \(String(describing: error))") 
     } else { 
      print("Response: \(String(describing: response))") 
     } 
}) 

task.resume() 
相關問題