Json使用http post方法解析不起作用

时间:2011-10-20 05:53:59

标签: iphone json

我必须使用http post方法解析json,但我总是得到该方法未实现的响应。但它适用于post方法。这是我的代码

NSString *password = txtPassword.text;
NSArray *objects = [NSArray arrayWithObjects:@"sintu",@"sintugeorge",@"auth.gettoken", nil];
NSArray *keys = [NSArray arrayWithObjects:@"username",@"password",@"method",  nil];
NSDictionary *theRequestDictionary = [NSDictionary dictionaryWithObjects:objects forKeys:keys];
NSString *theBodyString = [theRequestDictionary JSONRepresentation];
NSData *theBodyData =  [theBodyString dataUsingEncoding:NSUTF8StringEncoding];
NSString *postLength = [NSString stringWithFormat:@"%d", [theBodyData length]];

NSURL *theURL = [NSURL URLWithString:@"http://192.168.1.11/elggapi/services/api/rest/json"];
NSMutableURLRequest *theRequest = [[[NSMutableURLRequest alloc] init] autorelease];
[theRequest setURL:theURL];
[theRequest setHTTPMethod:@"POST"];
[theRequest setValue:postLength forHTTPHeaderField:@"Content-Type"];
[theRequest setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[theRequest setHTTPBody:theBodyData];

NSLog(@"%@", theBodyString);
NSURLResponse *theResponse = NULL;
NSError *theError = NULL;
NSData *theResponseData = [NSURLConnection sendSynchronousRequest:theRequest returningResponse:&theResponse error:&theError];
NSString *theResponseString = [[[NSString alloc] initWithData:theResponseData encoding:NSUTF8StringEncoding] autorelease];
// NSLog(theResponseString);
NSDictionary *theResponseDictionary =(NSDictionary*)[theResponseString JSONValue] ;

NSLog(@"%@", theResponseDictionary);

1 个答案:

答案 0 :(得分:1)

我正在使用ASIHTTP从API获取数据。所以这就是我在为POST数据获取数据时所做的事情

ASIFormDataRequest *request = [[ASIFormDataRequest alloc] initWithURL:[NSURL URLWithString:@"http://mobile.sample.com/IPhone/SingleAPI/Preview"]];
[request setDelegate:self];
[request setRequestMethod:@"POST"];
[request setPostValue:messagetele forKey:@"message"];

[request setPostValue:D_Name forKey:@"deliveryName"];
[request setPostValue:D_Address forKey:@"deliveryAddress"];
[request setPostValue:D_City forKey:@"deliveryCity"];
[request setPostValue:D_ZipCode forKey:@"deliveryZipCode"];
[request setPostValue:D_State forKey:@"deliveryState"];
[request setPostValue:D_Country forKey:@"deliveryCountry"];

[request setPostValue:S_Name forKey:@"senderFromName"];
[request setPostValue:S_City forKey:@"senderYourCity"];
[request setPostValue:S_Email forKey:@"senderYourEmail"];
[request setPostValue:S_Country forKey:@"senderYourCountry"];

[request startAsynchronous];

这就是我正在为获取数据做的事情,如果它在GET方法中

NSString *url = [NSString stringWithFormat:@"http://www.sample.com/IPhoneAPI/Search?uuid=%@&appkey=%@&page=%d&type=%@&criteria=%@&country=%@",uuid,@"TNaNpiJwd6wjDM5fGKYZyPDyMU93", [Globals sharedGlobals].pageNumber,@"suburb",[[Globals sharedGlobals].searchText objectForKey:@"text"],countryName];

ASIFormDataRequest *request = [[ASIFormDataRequest alloc] initWithURL:[NSURL URLWithString:url]];

[request setTimeOutSeconds:30];

[request setRequestMethod:@"GET"];

[request setDelegate:self];

[request startAsynchronous];

这两个都是异步调用网络。

在JSON中收到数据后,我正在调用此方法

- (void)requestFinished:(ASIHTTPRequest *)request
{   
    NSString *responseString = [request responseString];

    SBJSON *parser = [[SBJSON alloc]init];
    NSDictionary *data = (NSDictionary*) [parser objectWithString:responseString error:nil];

    tempOffers = [[data objectForKey:@"offers"]retain];

我正在收集JSON响应并将其放入数组或字典并使用它。

这是我调用API和获取数据的方式。希望这会帮助你。谢谢:))