我正在尝试将数据发送到服务器并获得响应.数据到达服务器但我没有得到任何响应.响应数据的值是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}"
Run Code Online (Sandbox Code Playgroud)
任何人都可以帮助我....
我的代码:
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]);
Run Code Online (Sandbox Code Playgroud)
Rog*_*Rog 12
我不知道您的网络服务的细节,但下面的代码可能是您的问题的来源(或至少其中一个!)
[request setValue:jsonString forHTTPHeaderField:@"json"];
responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&theResponse error:&theError];
[request setHTTPBody:responseData];
Run Code Online (Sandbox Code Playgroud)
您在设置正文之前发送请求,我认为应该包含您的jsonString
内容.另外,您将自己分配jsonString
到标题字段,您确定这是您想要的吗?这是对可能有效的猜测:
[request setValue:@"application/json" forHTTPHeaderField:@"Content-type"];
[request setHTTPBody:jsonString];
responseData = // rest of your code here....
Run Code Online (Sandbox Code Playgroud)
我建议你仔细研究一下代码,因为它现在很乱!你有两个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];
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
12214 次 |
最近记录: |