使用 json 的 Iphone Http 请求响应
Posted
技术标签:
【中文标题】使用 json 的 Iphone Http 请求响应【英文标题】:Iphone Http request response using json 【发布时间】:2011-03-22 08:40:47 【问题描述】:我正在尝试将数据发送到服务器并获得响应。数据正在到达服务器,但我没有得到任何响应。响应数据的值为 nil bcd ,它正在抛出异常,
-JSONValue failed. Error trace is: (
"Error Domain=org.brautaset.JSON.ErrorDomain Code=11 \"Unexpected end of string\" UserInfo=0x4e2dd70 NSLocalizedDescription=Unexpected end of string"
谁能帮帮我....
我的代码:
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"http://192.168.0.83:8082/WebServiceProject/AcessWebservice?operation=login"]];
[[NSURLConnection alloc] initWithRequest:request delegate:self];
NSURLResponse *theResponse =[[NSURLResponse alloc]init];
NSError *theError = NULL;
NSArray *keys = [NSArray arrayWithObjects:@"UserId", @"Password", nil];
NSArray *objects = [NSArray arrayWithObjects:@"rajin.sasi", @"abhi1551", nil];
NSDictionary *jsonDictionary = [NSDictionary dictionaryWithObjects:objects forKeys:keys];
NSString* jsonString = [jsonDictionary JSONRepresentation];
SBJSON *jsonParser = [SBJSON new];
[jsonParser objectWithString:jsonString];
NSLog(@"Val of json parse obj is %@",jsonString);
[request setHTTPMethod:@"POST"];
[request setValue:jsonString forHTTPHeaderField:@"json"];
responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&theResponse error:&theError];
[request setHTTPBody:responseData];
NSMutableString* stringData= [[NSString alloc]initWithData:responseData encoding:NSUTF8StringEncoding];
NSDictionary *jsonDictionaryResponse = [stringData JSONValue];
NSString *json_message=[jsonDictionaryResponse objectForKey:@"message"];
printf("Json string is %s **********",[json_message UTF8String]);
【问题讨论】:
【参考方案1】:我不知道您的网络服务的详细信息,但下面的代码可能是您的问题的根源(或至少其中一个!)
[request setValue:jsonString forHTTPHeaderField:@"json"];
responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&theResponse error:&theError];
[request setHTTPBody:responseData];
您在设置正文之前发送请求,我认为应该包括您的jsonString
内容。另外,您将jsonString
分配给标题字段,您确定这是您想要的吗?以下是对可能可行的猜测:
[request setValue:@"application/json" forHTTPHeaderField:@"Content-type"];
[request setHTTPBody:jsonString];
responseData = // rest of your code here....
我建议您仔细查看该代码,因为它目前一团糟!你有两个NSURLConnection
请求,一个asynchronous
和一个synchronous
,很难理解你在做什么/为什么要做这一切,所以查看Apple 的文档以获取NSURLConnection 并整理你的代码。 .
[编辑]
这是我给你的建议:
NSError *theError = nil;
NSArray *keys = [NSArray arrayWithObjects:@"UserId", @"Password", nil];
NSArray *objects = [NSArray arrayWithObjects:@"rajin.sasi", @"abhi1551", nil];
NSDictionary *jsonDictionary = [NSDictionary dictionaryWithObjects:objects forKeys:keys];
NSString *jsonString = [jsonDictionary JSONRepresentation];
NSData *jsonData = [jsonString dataUsingEncoding:NSUTF8StringEncoding];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"http://192.168.0.83:8082/WebServiceProject/AcessWebservice?operation=login"]];
[request setValue:jsonString forHTTPHeaderField:@"json"];
[request setHTTPMethod:@"POST"];
[request setHTTPBody:jsonData];
NSURLResponse *theResponse =[[NSURLResponse alloc]init];
NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&theResponse error:&theError];
NSMutableString *string = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSDictionary *jsonDictionaryResponse = [string JSONValue];
[string release];
[theResponse release];
【讨论】:
感谢 Rog 的响应,但请求的代码工作正常,问题出在响应中。响应数据在响应中没有任何价值... 好吧,如果你觉得没问题,那我想没问题! 你认为响应部分是否有任何缺陷或错误.... responseData = [NSURLConnection sendSynchronousRequest:request returnedResponse:&theResponse error:&theError]; NSMutableString* stringData= [[NSString alloc]initWithData:responseData encoding:NSUTF8StringEncoding]; NSDictionary *jsonDictionaryResponse = [stringData JSONValue]; NSString *json_message=[jsonDictionaryResponse objectForKey:@"message"]; printf("Json 字符串是 %s **********",[json_message UTF8String]); 非常感谢您的快速响应,但我仍然无法获取值。我尝试在方法中设置断点,但变量数据的大小为 0 字节..我卡住了,尝试从2 天,但无法弄清楚发生了什么...请帮助我... @Götze 嘿,你得到这个答案了吗?我坚持同样的。如果你有解决方案,请帮忙。【参考方案2】: NSData* responseData = nil;
NSURL *url=[NSURL URLWithString:[URLString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
responseData = [NSMutableData data] ;
NSMutableURLRequest *request=[NSMutableURLRequest requestWithURL:url];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
NSString *bodydata=[NSString stringWithFormat:@"%@",jsonString];
NSData *req=[NSData dataWithBytes:[bodydata UTF8String] length:[bodydata length]];
[request setHTTPMethod:@"POST"];
[request setHTTPBody:req];
[request setTimeoutInterval:15.0];
NSURLResponse* response;
NSError* error = nil;
responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
NSError *dataError;
NSMutableDictionary * jsonDict = [[NSMutableDictionary alloc]init];
if (responseData != nil)
jsonDict = [NSJSONSerialization JSONObjectWithData:responseData
options:kNilOptions
error:&dataError];
NSLog(@"jsonDict:%@",jsonDict);
【讨论】:
【参考方案3】:试试这个:
[request setValue:jsonString forHTTPHeaderField:@"json"];
responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&theResponse error:&theError];
[request setHTTPBody:responseData];
【讨论】:
以上是关于使用 json 的 Iphone Http 请求响应的主要内容,如果未能解决你的问题,请参考以下文章