【问题标题】:iOS : How to correctly send put request to serveriOS:如何正确发送 put 请求到服务器
【发布时间】:2014-10-21 12:40:15
【问题描述】:

我有一个我想要实现的 put 请求。我遇到的问题是它没有将正确的原始正文发送到服务器表单/发布参数。我期待的是{"questions":[{"type":"control_head"}]} 的原始正文,而不是我得到questions[][type]=control_head 任何提示或建议表示赞赏。

  NSString *jsonString = @"{\"questions\":[{\"type\":\"control_head\"}]}";
 [self createForms:jsonString];

 - (void) createForms : (NSString *) form
 {
 [self executePutRequest:@"user/forms" params:form];
  }

- (void) executePutRequest : (NSString *) url params : (NSString *) params
{
NSString *urlStr = [NSString stringWithFormat:@"http://requestb.in/13oujhot"];

AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];

NSData *data = [params dataUsingEncoding:NSUTF8StringEncoding];
NSDictionary *json = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];

NSMutableDictionary *userinfo = [[NSMutableDictionary alloc] init];

[manager PUT:urlStr parameters:json success:^(AFHTTPRequestOperation *operation, id responseObject) {
    [operation setUserInfo:userinfo];
    SBJsonParser *jsonparser = [SBJsonParser new];
    id result = [jsonparser objectWithString:[operation responseString]];
    if ( self.delegate != nil && [self.delegate respondsToSelector:finishSelector] ) {
        [self.delegate performSelector:finishSelector withObject:result];
    }

} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    [operation setUserInfo:userinfo];
    if ( self.delegate != nil && [self.delegate respondsToSelector:failSelector] ) {
        [self.delegate performSelector:failSelector withObject:[operation error]];
    }
}];
}

【问题讨论】:

  • 你是 NSJSONSerialization 数据已经是 JSON

标签: ios iphone http afnetworking put


【解决方案1】:

复制自here

再次为 AFNetworking 2.0 更新 - 见底部

对于 AFNetworking 1.0:

NSURL *url = [NSURL URLWithString:@"https://mysite.com/"];
AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:url];

NSDictionary *params = [NSDictionary dictionaryWithObjectsAndKeys:
                        height, @"user[height]",
                        weight, @"user[weight]",
                        nil];
[httpClient postPath:@"/myobject" parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {
    NSString *responseStr = [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
    NSLog(@"Request Successful, response '%@'", responseStr);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    NSLog(@"[HTTPClient Error]: %@", error.localizedDescription);
}];

对于 AFNetworking 2.0(也使用新的 NSDictionary 语法):

AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
NSDictionary *params = @{@"user[height]": height,
                         @"user[weight]": weight};
[manager POST:@"https://mysite.com/myobject" parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {
    NSLog(@"JSON: %@", responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    NSLog(@"Error: %@", error);
}];

【讨论】:

    【解决方案2】:

    你可以使用afnetworking框架,它会对你有所帮助。

    http://www.raywenderlich.com/59255/afnetworking-2-0-tutorial

    【讨论】:

      【解决方案3】:
       NSString *str_URL = [NSString stringWithFormat:@"YOUR URL"]];
      
      
       NSDictionary *parameters = [NSDictionary dictionaryWithObjectsAndKeys:
                                                          Your Value,KEY_Name,
                                                          nil];
      
       AFHTTPRequestOperationManager *Manager = [AFHTTPRequestOperationManager manager];
      
       [Manager PUT:str_URL parameters:parameters
            success: ^(AFHTTPRequestOperation *operation, id responseObject)
                       {
                                   NSLog(@"%@", responseObject);
                       }
            failure:^(AFHTTPRequestOperation *operation, NSError *error)
                      {
                                   NSLog(@"%@", error.description); 
                      }];
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-10-11
        • 1970-01-01
        • 2015-02-27
        • 1970-01-01
        • 2020-12-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多