Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/119.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone 从iOS到Django URL的HTTP Post请求_Iphone_Ios_Django_Http - Fatal编程技术网

Iphone 从iOS到Django URL的HTTP Post请求

Iphone 从iOS到Django URL的HTTP Post请求,iphone,ios,django,http,Iphone,Ios,Django,Http,从本质上看,这似乎是可行的,但是在服务器端,查询Dict看起来是这样的 发帖:QueryDict:{u'参赛者1约翰·多伊参赛者2简·多伊:[u']} 它将我的所有值存储为键,而字典中没有值。很明显,这是一个新手犯的错误,我猜这是iOS方面的错误,所以任何帮助都将不胜感激。代码如下: NSString *queryString = [NSString stringWithFormat:@"URL HERE"]; NSMutableURLRequest *theRequest=[N

从本质上看,这似乎是可行的,但是在服务器端,查询Dict看起来是这样的

发帖:QueryDict:{u'参赛者1约翰·多伊参赛者2简·多伊:[u']}

它将我的所有值存储为键,而字典中没有值。很明显,这是一个新手犯的错误,我猜这是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];

您在这里所做的只是通过附加字符串来创建POST数据。这是服务器接收的内容。您尚未定义“查询参数”。API定义了如何传递参数?在URL中?作为多部分/表单数据?作为“application/x-www-form-urlencoded”,作为JSON?是的,我想知道这一点,但我不确定如何定义查询参数。我认为JSON在这种情况下是理想的。我认为我找到了解决这个问题的方法。在我的NSData中,我使用了NSJsonSerialization,现在参数以字典的形式整齐地接收,并具有正确的键值对。太好了!我衷心希望您不需要那个古老的、模棱两可的、令人困惑的、容易出错的、不断变化的URL百分比编码参数quatsch我真的不知道:p谢谢!