2011-10-13 57 views
0

people, 我正在使用JSON touch inmy iphone应用程序。 现在我必须发送一个字符串然后一个数组到服务器,我该怎么做? 我从json请求中成功获取数据,但我必须发送一些数据。 这是到目前为止,我已经得到了代码:JSON touch iphone-sdk,通过请求发送数据

-(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? 


} 

回答

1

您可以使用ASIHTTRequest串/ JSON POST数据到服务器:

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也可以以异步模式使用。 P.S.我没有测试代码,但它应该可以工作。

要发布表单数据,您可以使用ASIFormDataRequest,文档是here