繁体   English   中英

从服务Objective-C获取数据

[英]Getting data from a service Objective-C

是iOS的新手,无法显示以下服务数据中的数据

[{
    "Name": Rahul,
    "FatherName": Ravinder,
    "Designation": Engineering,
    "Profession": Software Eng,
    "Height": "5 ft 3 in",
    "Weight": "134.5 lbs"
}]

下面是我尝试过的代码。 请帮助我找到问题。 提前致谢。

 NameDetails.m
---------------

- (void)viewDidLoad {
    [super viewDidLoad];
    [self callService:[appDelegate.signUpdata objectForKey:@"id"]];
}

-(void)callService:(NSString *)userid
{
    [Utility showIndicator:nil view1:self.view];
    JsonServicePostData = [[JsonServiceCls alloc] init];
    JsonServicePostData.delegate = self;
    [JsonServicePostData Getdata:userid];
}

-(void)DidFinishWebServicesPostData
{
    [Utility hideIndicator];

        NSMutableDictionary *dict = [[NSMutableDictionary alloc]init];
        _txtName.text=[dict objectForKey:@"Name"];
        _txtFName.text=[dict objectForKey:@"FatherName"];
        _txtDesg.text=[dict objectForKey:@"Designation"];
        _txtprof.text=[dict objectForKey:@"Profession"];
        _txtHeight.text=[dict objectForKey:@"Height"];
        _txtWeight.text=[dict objectForKey:@"Weight"];

    }

}
+(void)makeHttpGETresponceParsingwithSerVer:(NSString *)strServer withCallBack:(void(^)(NSDictionary *dicArr,NSError *error))handler
{

NSURL *urlServer = [NSURL URLWithString:strServer];

    NSURLRequest *request = [NSURLRequest requestWithURL:urlServer];

    NSURLSession *session = [NSURLSession sharedSession];

    NSURLSessionDataTask *postDataTask = [session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {

            NSDictionary *res = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:&error];


            handler(res,error);


    }];

    [postDataTask resume];
}
then call your method In viewDidLoad...

[RestClient makeHttpGETresponceParsingwithSerVer:@"YOUR_URL" withCallBack:^(NSDictionary *responceDic, NSError *error) {

    _txtName.text   =[responceDic objectForKey:@"Name"];
    _txtFName.text  =[responceDic objectForKey:@"FatherName"];
    _txtDesg.text   =[responceDic objectForKey:@"Designation"];
    _txtprof.text   =[responceDic objectForKey:@"Profession"];
    _txtHeight.text =[responceDic objectForKey:@"Height"];
    _txtWeight.text =[responceDic objectForKey:@"Weight"];

}];

// RestClient是类名,因为它是一个类方法,您可以使用实例方法。

您好,对于这种API调用活动,更好的方法是AFNetworking- https://github.com/AFNetworking/AFNetworking

它相当简单,功能更强大。 一旦获得json响应,就必须进行模型方法。

#import <UIKit/UIKit.h>

@interface NameDetails : NSObject

@property (nonatomic, strong) NSString * designation;
@property (nonatomic, strong) NSString * fatherName;
@property (nonatomic, strong) NSString * height;
@property (nonatomic, strong) NSString * name;
@property (nonatomic, strong) NSString * profession;
@property (nonatomic, strong) NSString * weight;

-(instancetype)initWithDictionary:(NSDictionary *)dictionary;

-(NSDictionary *)toDictionary;
@end

#import "RootClass.h"

NSString *const kRootClassDesignation = @"Designation";
NSString *const kRootClassFatherName = @"FatherName";
NSString *const kRootClassHeight = @"Height";
NSString *const kRootClassName = @"Name";
NSString *const kRootClassProfession = @"Profession";
NSString *const kRootClassWeight = @"Weight";

@interface RootClass ()
@end
@implementation RootClass




/**
 * Instantiate the instance using the passed dictionary values to set the properties values
 */

-(instancetype)initWithDictionary:(NSDictionary *)dictionary
{
    self = [super init];
    if(![dictionary[kRootClassDesignation] isKindOfClass:[NSNull class]]){
        self.designation = dictionary[kRootClassDesignation];
    }   
    if(![dictionary[kRootClassFatherName] isKindOfClass:[NSNull class]]){
        self.fatherName = dictionary[kRootClassFatherName];
    }   
    if(![dictionary[kRootClassHeight] isKindOfClass:[NSNull class]]){
        self.height = dictionary[kRootClassHeight];
    }   
    if(![dictionary[kRootClassName] isKindOfClass:[NSNull class]]){
        self.name = dictionary[kRootClassName];
    }   
    if(![dictionary[kRootClassProfession] isKindOfClass:[NSNull class]]){
        self.profession = dictionary[kRootClassProfession];
    }   
    if(![dictionary[kRootClassWeight] isKindOfClass:[NSNull class]]){
        self.weight = dictionary[kRootClassWeight];
    }   
    return self;
}


/**
 * Returns all the available property values in the form of NSDictionary object where the key is the approperiate json key and the value is the value of the corresponding property
 */
-(NSDictionary *)toDictionary
{
    NSMutableDictionary * dictionary = [NSMutableDictionary dictionary];
    if(self.designation != nil){
        dictionary[kRootClassDesignation] = self.designation;
    }
    if(self.fatherName != nil){
        dictionary[kRootClassFatherName] = self.fatherName;
    }
    if(self.height != nil){
        dictionary[kRootClassHeight] = self.height;
    }
    if(self.name != nil){
        dictionary[kRootClassName] = self.name;
    }
    if(self.profession != nil){
        dictionary[kRootClassProfession] = self.profession;
    }
    if(self.weight != nil){
        dictionary[kRootClassWeight] = self.weight;
    }
    return dictionary;

}

上面的是模型类。

您的JSON看起来像一个数组。 因此,您需要对其上的Dictionary值进行迭代。 除此之外,您可以直接通过。

现在在您的ViewController类中初始化可变数组

并像这样传递响应

NSArray *arrayData = ResponseFromAFNETWORKING

    for (NSDictionary *data in arrayData) {
        NameDetails *modelFeed = [[NameDetails alloc] initFromDictinary:data]
       [self.YourMutableDictionary addObject:modelFeed]

    }
self.updateDisplay:self.YourMutableDictionary[0] // If not array No iteration, you can prepare the model and pass it directly
----------------------------------------
- (void)updateDisplay:(NameDetails *)feed {
    _txtName.text   =feed.Name;
    _txtFName.text  =feed.FatherName;
    _txtDesg.text   =feed.Designation;
    _txtprof.text   =feed.Profession;
    _txtHeight.text =feed.Height;
    _txtWeight.text =feed.Weight;

}

希望这会有所帮助。 这是一种强大而灵活的方法,也是线程安全机制

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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