JSON touch iphone-sdk,根据请求发送数据

时间:2022-10-09 10:42:21

people, I'm using JSON touch inmy iphone app. Now I have to send a string and then an array to server, how can I do this? I get data from json requests succefully, but I have to send some data. Here is the code I've got so far:

人们,我正在使用JSON touch inmy iphone app。现在我必须将一个字符串然后一个数组发送到服务器,我该怎么做?我成功地从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?


}

1 个解决方案

#1


1  

You can use ASIHTTRequest to POST string/json data to server:

您可以使用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];

If you want to post a string value then try:

如果您想发布字符串值,请尝试:

[request appendPostData:@"key=value"];

ASIHTTPRequest can be used in asynchronious mode as well. P.S. I did not tested the code, but it should work.

ASIHTTPRequest也可以用于异步模式。附:我没有测试代码,但它应该工作。

To post form data you can use ASIFormDataRequest, documentation is here

要发布表单数据,您可以使用ASIFormDataRequest,文档在这里

#1


1  

You can use ASIHTTRequest to POST string/json data to server:

您可以使用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];

If you want to post a string value then try:

如果您想发布字符串值,请尝试:

[request appendPostData:@"key=value"];

ASIHTTPRequest can be used in asynchronious mode as well. P.S. I did not tested the code, but it should work.

ASIHTTPRequest也可以用于异步模式。附:我没有测试代码,但它应该工作。

To post form data you can use ASIFormDataRequest, documentation is here

要发布表单数据,您可以使用ASIFormDataRequest,文档在这里