从iOS到Django URL的HTTP Post请求

时间:2013-07-18 06:02:38

标签: iphone ios django http

基本上这似乎在服务器端工作,但是Query Dict看起来像这样。

POST:QueryDict:{u'Contestant1 John Doe Contestant2 Jane Doe':[u'']}

将我的所有值存储为键,而在Dictionary中没有值。显然这是一个新手的错误,我猜这是在iOS方面的事情,所以任何帮助将不胜感激。代码如下:

    NSString *queryString = [NSString stringWithFormat:@"URL HERE"];
    NSMutableURLRequest *theRequest=[NSMutableURLRequest
                                     requestWithURL:[NSURL URLWithString:
                                                     queryString]
                                     cachePolicy:NSURLRequestUseProtocolCachePolicy
                                     timeoutInterval:60.0];
    [theRequest setHTTPMethod:@"POST"];
    NSMutableData *body = [NSMutableData data];
    [body appendData:[[NSString stringWithFormat:@"Contestant1 %@ ",contestant1] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[[NSString stringWithFormat:@"Contestant2 %@ ",contestant2] dataUsingEncoding:NSUTF8StringEncoding]];
    [theRequest setHTTPBody:body];
    NSURLConnection *con = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
    if (con) {
        NSLog(@"success!");
    } else { 
        //something bad happened
    }

编辑:我的问题的解决方案。

    NSDictionary *postDict = [NSDictionary dictionaryWithObjectsAndKeys:contestant1, @"contestant1",
                              contestant2, @"contestant2", nil];

    NSError *error=nil;

    NSData* jsonData = [NSJSONSerialization dataWithJSONObject:postDict
                                                       options:NSJSONWritingPrettyPrinted error:&error];


    [theRequest setHTTPBody:jsonData];

0 个答案:

没有答案