通过POST发送JSON不起作用 - 目标C

时间:2012-05-30 01:31:54

标签: objective-c ios json post nsmutableurlrequest

我使用POST方法向服务器发送JSON字符串。应该发生的是它应该返回一个显示“Array(JSON String)Array(JSON String)”的响应。响应包含两个数组:如果我使用POST方法,则填充第一个数组,而如果我使用GET方法则填充第二个数组。我尝试通过GET方法发送JSON,实际上,第二个数组已经填充。但是,当我尝试通过POST方法发送JSON时,两个数组都返回空。

以下是我使用的代码:

  

NSData * requestData = [NSJSONSerialization dataWithJSONObject:sqlArray options:NSJSONWritingPrettyPrinted error:& error];

NSURL *url = [NSURL URLWithString:@"http://myurl/xmlrpc/imwebsrvcjson.php"];

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60];

[request setHTTPMethod:@"POST"];
[request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[request setValue:[NSString stringWithFormat:@"%d", [requestData length]] forHTTPHeaderField:@"Content-Length"];
[request setHTTPBody:requestData];

NSData *result =[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];

NSString *returnString = [[NSString alloc] initWithData:result encoding:NSUTF8StringEncoding];

if (error == nil){
    NSLog(@"The Result string: %@", returnString);   
}

你能告诉我我的代码有什么问题吗?

1 个答案:

答案 0 :(得分:-2)

这就是我的工作(请注意,进入我服务器的JSON需要是一个带有一个值的字典(另一个字典),用于key = question..ie {:question => {dictionary}}):

NSArray *objects = [NSArray arrayWithObjects:[[NSUserDefaults standardUserDefaults]valueForKey:@"StoreNickName"],
  [[UIDevice currentDevice] uniqueIdentifier], [dict objectForKey:@"user_question"],     nil];
NSArray *keys = [NSArray arrayWithObjects:@"nick_name", @"UDID", @"user_question", nil];
NSDictionary *questionDict = [NSDictionary dictionaryWithObjects:objects forKeys:keys];

NSDictionary *jsonDict = [NSDictionary dictionaryWithObject:questionDict forKey:@"question"];

NSString *jsonRequest = [jsonDict JSONRepresentation];

NSLog(@"jsonRequest is %@", jsonRequest);

NSURL *url = [NSURL URLWithString:@"https://xxxxxxx.com/questions"];

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url
             cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0];


NSData *requestData = [NSData dataWithBytes:[jsonRequest UTF8String] length:[jsonRequest length]];

[request setHTTPMethod:@"POST"];
[request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[request setValue:[NSString stringWithFormat:@"%d", [requestData length]] forHTTPHeaderField:@"Content-Length"];
[request setHTTPBody: requestData];

NSURLConnection *connection = [[NSURLConnection alloc]initWithRequest:request delegate:self];
if (connection) {
 receivedData = [[NSMutableData data] retain];
}
The receivedData is then handled by:

NSString *jsonString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSDictionary *jsonDict = [jsonString JSONValue];
NSDictionary *question = [jsonDict objectForKey:@"question"];

这不是100%明确,需要重新阅读,但一切都应该在这里让你开始。从我所知道的,这是异步的。在进行这些调用时,我的UI未被锁定。希望有所帮助。