简体   繁体   中英

why Json Dictionary is Giving the response in nsdata(Bytes) always.How can i Get the Dictionary from web service

In this i am sending the header and Add perameters to hitting the web service. the problem is after hitting the web service controller is not going in to success part sometimes. if it is hitting properly then it will return the result in bytes. This is My code::

 AFHTTPRequestOperationManager *manager =[AFHTTPRequestOperationManager manager];
manager.responseSerializer = [AFHTTPResponseSerializer serializer];
[manager.requestSerializer setTimeoutInterval:300];
manager.requestSerializer.cachePolicy = NSURLRequestReloadIgnoringLocalAndRemoteCacheData;

//giving the correct format to json
manager.responseSerializer.acceptableContentTypes = [manager.responseSerializer.acceptableContentTypes setByAddingObject:@"text/html"];
token=[parameters valueForKey:@"token"];
if(token == nil  || [token isEqual:[NSNull null]]){
    token=[[[NSUserDefaults standardUserDefaults]dictionaryForKey:@"LoginWebData"]valueForKey:@"token"];
}else{
    token=[parameters valueForKey:@"token"];
}
//set the header do http request
[manager.requestSerializer setValue:[NSString stringWithFormat:@"%@",token]forHTTPHeaderField:@"token"];
[manager POST:[BaseURLString stringByAppendingString:methodName] parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject){

    NSLog(@"JSON: %@", responseObject);
    UserDict=responseObject;
    [self.TableviwSideMenu reloadData];
}
      failure:^(AFHTTPRequestOperation *operation, NSError *error){
          NSLog(@"Error: %@", [error localizedDescription]);
          [self showAlert: [error localizedDescription]];
      }];

the AFNetworking will do the serialization for you and the responseObject will most likely be either a NSArray or NSDictionary object do like

   [manager POST:[BaseURLString stringByAppendingString:methodName] parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject){

    NSLog(@"JSON: %@", responseObject);
       if ([responseObject isKindOfClass:[NSArray class]]) {
    NSArray *responseArray = responseObject;
       UserDict=responseArray;
    /* do something with responseArray */
} else if ([responseObject isKindOfClass:[NSDictionary class]]) {
    NSDictionary *responseDict = responseObject;
     UserDict=responseDict;
    /* do something with responseDict */
}
    if (UserDict.count>0)
     [self.TableviwSideMenu reloadData];

}
      failure:^(AFHTTPRequestOperation *operation, NSError *error){
          NSLog(@"Error: %@", [error localizedDescription]);
          [self showAlert: [error localizedDescription]];
      }];

The AFNetworking framework and its delegate will handle everything. You will get responseObject in NSDictionary.

  -(void)Request{
    [self.Request_coursecomplete setRequestMethod:@"POST"];
    self.Request_coursecomplete = [ASIFormDataRequest requestWithURL:sendSMSURL];
    self.Request_coursecomplete.delegate = self;


    //will try second time
    [self.Request_coursecomplete setNumberOfTimesToRetryOnTimeout:2];

    [self.Request_coursecomplete setShouldContinueWhenAppEntersBackground:YES];

    NSTimeInterval timeOutSecs = 60000;

    [self.Request_coursecomplete setTimeOutSeconds:timeOutSecs];
    [self.Request_coursecomplete startAsynchronous];
}

- (void)requestFinished:(ASIHTTPRequest *)request
{

    NSString *receivedString = [request responseString];

    NSLog(@"json string: %@",receivedString);

    NSDictionary *d = [receivedString JSONValue];
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM