2015-05-19 106 views
0

我目前正在使用Google日历API并允许用户向与会者发布活动,但现在我应该发送一个POST方法,其中包含“请求正文” 。发送带有“正文请求”的NSMutableURLRequest请求在iOS中

下面是我应该发送请求:

https://www.googleapis.com/calendar/v3/calendars/<MyCalendorID>/events/<MyEventID> 

Request Body : 

{ 
"end": { 
    "date": "2015-05-19" 
}, 
"start": { 
    "date": "2015-05-20" 
}, 
"attendees": [ 
    { 
    "email": "[email protected]" 
    } 
] 
} 

我敢肯定,我做错了什么在[request setHTTPBody]但是这是我能想到的唯一的事情。

请帮

+0

不知道这会有所帮助,但有一个样子:http://stackoverflow.com/questions/29669296/post-json-data-into-existing-object –

+0

同样的问题,有你发现它的修复? http://stackoverflow.com/questions/31585403/how-to-escape-body-characters-in-nsmutableurlrequest –

+0

@János我发布我的答案检查它:) –

回答

0
  NSMutableDictionary *RequestDict = [[NSMutableDictionary alloc] init]; 

      BOOL *remind_bool = false; 
      NSMutableDictionary *remindDict = [[[NSMutableDictionary alloc]init] autorelease]; 
      [remindDict setObject:[NSNumber numberWithBool:remind_bool] forKey:@"useDefault"]; 

      [RequestDict setObject:[eventInfoDict objectForKey:@"end"] forKey:@"end"]; 
      [RequestDict setObject:[eventInfoDict objectForKey:@"start"] forKey:@"start"]; 
      [RequestDict setObject:AttendeesArr forKey:@"attendees"]; 
      [RequestDict setObject:_txt_eventname.text forKey:@"summary"]; 
      [RequestDict setObject:_txt_desc.text forKey:@"description"]; 
      [RequestDict setObject:_txt_location.text forKey:@"location"]; 
      [RequestDict setObject:remindDict forKey:@"reminders"]; 
      [RequestDict setObject:@"tentative" forKey:@"status"]; 

      [RequestDict retain]; 

      NSLog(@"%@",RequestDict); 

-(void)callAPIfortesting:(NSString *)apiURL withHttpMethod:(HTTP_Method)httpMethod 
     postParameterNames:(NSArray *)params 
    postParameterValues:(NSArray *)values postParameterDict:(NSDictionary *)RequestDict 
{ 
    // Check if the httpMethod value is valid. 
    // If not then notify for error. 
    if (httpMethod != httpMethod_GET && httpMethod != httpMethod_POST && httpMethod != httpMethod_DELETE && httpMethod != httpMethod_PUT) { 
     [self.gOAuthDelegate errorOccuredWithShortDescription:@"Invalid HTTP Method in API call" andErrorDetails:@""]; 
    } 
    else 
    { 
     // Create a string containing the API URL along with the access token. 
     NSString *urlString = [NSString stringWithFormat:@"%@?access_token=%@", apiURL,[_accessTokenInfoDictionary objectForKey:@"access_token"]]; 
     // Create a mutable request. 


     NSUserDefaults *UserDefaults = [NSUserDefaults standardUserDefaults]; 

     [UserDefaults setObject:[_accessTokenInfoDictionary objectForKey:@"access_token"] forKey:@"AccessToken"]; 
     [UserDefaults synchronize]; 



     NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:urlString]]; 

     // Depending on the httpMethod value set the respective property of the request object. 
     switch (httpMethod) 
     { 
      case httpMethod_GET: 
       [request setHTTPMethod:@"GET"]; 
       break; 
      case httpMethod_POST: 
       [request setHTTPMethod:@"POST"]; 
       break; 
      case httpMethod_DELETE: 
       [request setHTTPMethod:@"DELETE"]; 
       break; 
      case httpMethod_PUT: 
       [request setHTTPMethod:@"PUT"]; 
       break; 

      default: 
       break; 
     } 

     // In case of POST httpMethod value, set the parameters and any other necessary properties. 
     if (httpMethod == httpMethod_POST) 
     { 
      // A string with the POST parameters should be built. 
      // Create an empty string. 
      NSString *postParams = @""; 
      // Iterrate through all parameters and append every POST parameter to the postParams string. 
      for (int i=0; i<[params count]; i++) { 
       postParams = [postParams stringByAppendingString:[NSString stringWithFormat:@"%@=%@", 
                    [params objectAtIndex:i], [values objectAtIndex:i]]]; 

       // If the current parameter is not the last one then add the "&" symbol to separate post parameters. 
       if (i < [params count] - 1) { 
        postParams = [postParams stringByAppendingString:@"&"]; 
       } 
      } 


      NSLog(@"%@",postParams); 

     } 


     if([NSJSONSerialization isValidJSONObject:RequestDict]) 
     { 
      // Convert the JSON object to NSData 
      NSData * httpBodyData = [NSJSONSerialization dataWithJSONObject:RequestDict options:0 error:nil]; 
      // set the http body 
      [request setHTTPBody:httpBodyData]; 
     } 

     [request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"]; 

     NSLog(@"%@",request); 

     // Make the request. 
     [self makeRequest:request]; 
    } 
}