Iphone Http request response using json

I am trying to send data to a server and get a response. The data reaches the server, but I get no response. The value of the response data is nil bcd from which it throws an exception,

-JSONValue failed. Error trace is: ( "Error Domain=org.brautaset.JSON.ErrorDomain Code=11 \"Unexpected end of string\" UserInfo=0x4e2dd70 {NSLocalizedDescription=Unexpected end of string}" 

Can anyone help me ....

My code is:

 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]); 
+7
source share
3 answers

I do not know the details of your web service, but the code below may be the source of your problem (or at least one of them!)

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

You submit a request before setting the body, which I assume should contain your jsonString content. Also, you assign your jsonString in the header field, are you sure you want to? Here guess what might work:

 [request setValue:@"application/json" forHTTPHeaderField:@"Content-type"]; [request setHTTPBody:jsonString]; responseData = // rest of your code here.... 

I suggest you look at this code well, as this is a mess at the moment! You have two NSURLConnection , one asynchronous and one synchronous , it's hard to understand what / why you are doing all this, so check the Apple documentation for NSURLConnection and arrange the code ...

[EDIT]

Here is my suggestion for you:

 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]; 
+12
source
  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
source

Try the following:

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

All Articles