簡體   English   中英

iOS:如何正確將放置請求發送到服務器

[英]iOS : How to correctly send put request to server

我有一個要達到的看跌要求。 我遇到的問題是沒有將正確的原始內容發送到服務器的form / post參數。 我期望的是{"questions":[{"type":"control_head"}]} ,而不是我收到的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]];
    }
}];
}

這里復制

再次更新了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);
}];

您可以使用afnetworking框架,它將為您提供幫助。

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

 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); 
                }];

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM