gpt4 book ai didi

Iphone Http请求响应使用json

转载 作者:可可西里 更新时间:2023-11-01 03:27:54 26 4
gpt4 key购买 nike

我正在尝试将数据发送到服务器并获得响应。数据正在到达服务器,但我没有收到任何响应。响应数据的值为 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]);

最佳答案

我不知道你的网络服务的细节,但下面的代码可能是你问题的根源(或者至少是其中之一!)

[request setValue:jsonString forHTTPHeaderField:@"json"];
responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&theResponse error:&theError];
[request setHTTPBody:responseData];

您在设置正文之前发送请求,我认为它应该包括您的 jsonString 内容。另外,您将 jsonString 分配给 header 字段,您确定这是您想要的吗?以下是对可能起作用的猜测:

[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];

关于Iphone Http请求响应使用json,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5388642/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com