Akshay
Akshay

Reputation: 2983

Iphone Http request response using json

I am trying to send the data to server and get the response. Data is reaching server but I am not getting any response. The value of response data is nil bcd of which it's throwing 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 pls help me....

My code:

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

Upvotes: 7

Views: 12254

Answers (3)

user4395662
user4395662

Reputation:

    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);

    }

Upvotes: 2

Rog
Rog

Reputation: 18670

I'm not privy of the particulars of your webservice, but the code below might 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 are sending the request before setting the body, which I assume should include your jsonString contents. Plus you're assigning your jsonString to a header field, are you sure that is what you want? Here's a guess at what might work:

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

I suggest you have a good look through that code as it is a mess at the moment! You have two NSURLConnection requests going there, one asynchronous and one synchronous, it's kind of hard to understand what/why you are doing all of this so check Apple's documentation for NSURLConnection and tidy up your code...

[EDIT]

Here's 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];

Upvotes: 11

Akshay
Akshay

Reputation: 2983

Try this :

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

Upvotes: 0

Related Questions