我正在尝试将数据发送到服务器并获得响应。数据到达服务器但我没有得到任何响应。响应数据的值是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]);
答案 0 :(得分:12)
我不了解您的网络服务的详细信息,但下面的代码可能是您问题的根源(或至少其中一个!)
[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的{{3}文档并整理你的代码...
<强> [编辑] 强>
以下是我的建议:
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];
答案 1 :(得分: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);
}
答案 2 :(得分:0)
试试这个:
[request setValue:jsonString forHTTPHeaderField:@"json"];
responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&theResponse error:&theError];
[request setHTTPBody:responseData];