2

我得到 JSON 结果为-JSONValue failed. Error is: Unexpected end of input. 请以正确的方式指导我。

我是解析新手。我必须通过 POST 方法从服务器获取数据。我有以下细节。我必须通过 url 传递 zip

{"zip":"52435","methodIdentifier":"search_dealer"}

url : http://usaautoleads.com/api.php

method: post

web service name: search_dealer

response : {"success":"0","dealer":[info...]}

我的代码在这里。

NSURL *myURL=[NSURL URLWithString:@"http://usaautoleads.com/api.php"];

NSString *post =[[NSString alloc]initWithString:@"52435"];

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

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

[request setURL:myURL]; 

[request setHTTPMethod:@"POST"];

[request setHTTPBody:postData]; 

[request setValue:@"application/json" forHTTPHeaderField:@"content-type"];

[[NSURLConnection alloc] initWithRequest:request delegate:self];
4

1 回答 1

1

这里的问题在于您的帖子字符串。用这个

NSString *zip = @"52435";
NSString *methodID = @"search_dealer";
NSString *post =[NSString stringWithFormat:@"{\"zip\":\"%@\",\"methodIdentifier\":\"%@\"}", zip, methodID];
于 2012-07-11T12:46:36.003 回答