-1

我正在使用 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);   
}

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

4

1 回答 1

-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 未锁定。希望有帮助。

于 2012-12-01T10:43:54.897 回答