发布请求json解析

时间:2013-03-14 11:10:35

标签: iphone ios objective-c json cocoa-touch

现在

question_id = 28,值='是', null 那么功能就会得到

{
 question_id = 22, 
 value = 'yes', 
 array = (9=>1, 28 => 0)
}

9表示是1表示是,0表示否。 该数组存储所有以前的问题和答案 如果没有问题,则显示结果。 我如何使用两个操作按钮yes和no ??

来实现/发布/

1 个答案:

答案 0 :(得分:2)

您可以使用以下代码段,如in this article所述:

在这里,我简单描述了如何使用POST方法。

  

1。使用实际用户名和密码设置帖子字符串。

NSString *post = [NSString stringWithFormat:@"&Username=%@&Password=%@",@"username",@"password"];
     

2。使用NSASCIIStringEncoding以及您需要以NSData格式发送的帖子字符串对帖子字符串进行编码。

NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
     

您需要发送数据的实际长度。计算长度   帖子字符串。

NSString *postLength = [NSString stringWithFormat:@"%d",[postData length]];
     

3。使用HTTP方法,http标题字段以及帖子字符串的长度等所有属性创建Urlrequest。创建URLRequest   反对并初始化它。

NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
     

设置要将数据发送到该请求的网址。

[request setURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://www.abcde.com/xyz/login.aspx"]]];
     

现在,设置 HTTP 方法( POST或GET )。写下这条线   你的代码。

[request setHTTPMethod:@"POST"];
     

设置HTTP标题字段,其中包含发布数据的长度。

[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
     

还设置HTTP标头字段的编码值。

[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Current-Type"];
     

使用postData设置urlrequest的HTTPBody

[request setHTTPBody:postData];
     

4. 现在,创建URLConnection对象。使用URLRequest初始化它。

NSURLConnection *conn = [[NSURLConnection alloc]initWithRequest:request delegate:self];
     

它返回初始化的url连接并开始加载数据   对于网址请求。您可以检查是否URL连接   正确完成或不使用 if / else 语句,如下所示。

if(conn)
{
NSLog(@”Connection Successful”)
}
else
{
NSLog(@”Connection could not be made”);
}
     

5. 要从HTTP请求接收数据,您可以使用URLConnection类参考提供的委托方法。   代表方法如下。

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData*)data
     

上面的方法用于接收我们使用post获得的数据   方法

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
     

此方法,可以用来接收错误报告   没有与服务器建立连接。

- (void)connectionDidFinishLoading:(NSURLConnection *)connection
     

上述方法用于在连接完成后处理数据   成功。

请参阅 This This 文档了解POST方法。

以下是源代码为HTTPPost Method.

的最佳示例

<强>编辑:

-(void) buttonPressed:(id) sender
{
    NSURL *url = [NSURL URLWithString:@"URL"];
    ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
    [request setShouldStreamPostDataFromDisk:YES];
    [request setDidFinishSelector:@selector(uploadFinished:)];
    [request setDidFailSelector:@selector(uploadFail:)];
    [request setTimeOutSeconds:300];
    [request setPostValue:@"Login" forKey:@"action"];
    [request setPostValue:Yourvalue1 forKey:@"key1"];
    [request setPostValue:Yourvalue2 forKey:@"key2"];
    .
    .
    .

    request.delegate = self;
    [self.sendLoader startAnimating];
    [request startAsynchronous];
}

#pragma Mark -
#pragma Mark - ASIHTTPRequest Methods

- (void) uploadFinished:(ASIHTTPRequest *)request
{
    NSString *str = [request responseString];// convert it as JSON also.

}
- (void) uploadFail:(ASIHTTPRequest *)request
{
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"Error",@"") message:NSLocalizedString(@"Connection failed !!",@"") delegate:nil cancelButtonTitle:NSLocalizedString(@"OK",@"") otherButtonTitles:nil];
    [alert show];

}