Romance
Romance

Reputation: 1414

Json response is giving wrong data

I am calling the webservice string using the NSURLConnection , I am getting wrong data in response in success method of NSURLConnection , But if i load same URL in browser i am getting correct response. I am using below code .

    NSData *mydata=[srtRes dataUsingEncoding:NSUTF8StringEncoding];
    NSError *e;
    NSMutableArray *returnArry =[[NSMutableArray alloc]init];
    returnArry = [NSJSONSerialization JSONObjectWithData:mydata options:NSJSONReadingMutableContainers error:&e];

How to resolve this issue. Kindly give suggestion and answers.

Upvotes: 0

Views: 181

Answers (1)

Smart_stud
Smart_stud

Reputation: 29

Try this one :

  // In .h class
  @property (nonatomic,retain) NSMutableData *responseData;
  @property (nonatomic, retain) NSMutableArray *temp_arr;

  // In .m class
  @synthesize responseData;
  @synthesize temp_arr;

  - (void)viewDidLoad
  {
     NSString *urlString=@"http://your urls";
     self.responseData=[NSMutableData data];
     NSURLConnection *connection=[[NSURLConnection alloc]initWithRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:urlString]] delegate:self];
     NSLog(@"connection====%@",connection);
  }

Delegate Method of JSON Parsing :

 -(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
 {
    [self.responseData setLength:0];
 }
 -(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
 {
    [self.responseData appendData:data];
 }  
 -(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
 {
    self.responseData=nil;
 }
 -(void)connectionDidFinishLoading:(NSURLConnection *)connection
 {
     NSArray * returnArry = [NSJSONSerialization JSONObjectWithData:self.responseData options:kNilOptions error:nil];
      NSDictionary *nameDic = nil;
     for (int i = 0 ; i < [returnArry count]; i++)
     {
        nameDic = [returnArry objectAtIndex:i];
        [self.temp_arr addObject:[nameDic objectForKey:@"name"]];  // According your key you have to save data in temp_arr.
     }
 }

Please follow this , if any doubt let me know :)

Upvotes: 1

Related Questions