人, 我在iphone应用程序中使用JSON touch。 现在我必须将一个字符串然后一个数组发送到服务器,我该怎么做? 我成功地从json请求获取数据,但我必须发送一些数据。 这是我到目前为止的代码:
-(void)subimtSelection:(int)aNumber
{
NSString *choiceData=[NSString stringWithFormat:@"%d", aNumber];
NSError *theError=nil;
[[CJSONSerializer serializer] serializeString:choiceData error:&theError];
NSDictionary *jsDic=[NSDictionary dictionaryWithObject:choiceData
forKey:@"selection"];
//WHAT SHOULD I DO NEXT?
}
答案 0 :(得分:1)
您可以使用ASIHTTRequest
将字符串/ json数据发送到服务器:
ASIHTTPRequest *request = [[ASIHTTPRequest alloc] initWithURL:@"http://server.url"];
[request addRequestHeader:@"Accept" value:@"application/json"];
[request addRequestHeader:@"Content-Type" value:@"application/json"];
[request setRequestMethod:@"POST"];
[request appendPostData:[yourJSONString dataUsingEncoding:NSUTF8StringEncoding]];
[request startSynchronous];
如果您想发布字符串值,请尝试:
[request appendPostData:@"key=value"];
ASIHTTPRequest
也可用于异步模式。
附:我没有测试代码,但它应该工作。
要发布表单数据,您可以使用ASIFormDataRequest
,文档为here