POST
当然相关的知识资料请参考:点击打开链接
+ (void)fetchMusicData:(NSString *)key page:(NSInteger)page callback:(fetchTrackDataAndError)callback;
其中fetchTrackDataAndError是回调方法,至于什么是回调方法,请参考:点击打开链接。
typedef void(^fetchTrackDataAndError)(NSArray *array, NSInteger page, NSError *error);
参数的含义:key,这个是要搜索的歌曲名;page,是第几页;array,是搜到的歌曲放在array中。
FetchDataFromNet.m文件中:
[request setHTTPBody:[bodyString dataUsingEncoding:NSUTF8StringEncoding]];
@end
当然这需要你在ViewController.m调用一下该类的类方法。
- (void)testData:(NSString *)name page:(NSInteger)pageIndex{
[FetchDataFromNet fetchMusicData:name page:pageIndex callback:^(NSArray *array, NSInteger page, NSError *error){
if (error) {
NSLog(@"error = %@",error);
} else{
}
}];
}
- (void)viewDidLoad {
[super viewDidLoad];
[self testData:@"lo" page:1];
}
这是一个歌曲的信息,有很多的属性,从而可以确定MusicData中需要哪些属性变量了。
2015-05-20 18:13:02.509 GetData[1609:172269] {
code = 200;
result = {
songCount = 300;
songs = (
{
album = {
artist = {
albumSize = 0;
alias = (
);
id = 0;
img1v1 = 0;
img1v1Url = "http://p3.music.126.net/6y-UleORITEDbvrOLV0Q8A==/5639395138885805.jpg";
name = "";
picId = 0;
picUrl = "
trans = "
};
copyrightId = 5003;
id = 1633376;
name = "Garden Window";
picId = 562949953429998;
publishTime = 1321286400000;
size = 11;
status = 1;
};
alias = (
);
artists = (
{
albumSize = 0;
alias = (
);
id = 98435;
img1v1 = 0;
img1v1Url = "http://p4.music.126.net/6y-UleORITEDbvrOLV0Q8A==/5639395138885805.jpg";
name = "O'Brother";
picId = 0;
picUrl = "
trans = "
}
);
copyrightId = 5003;
duration = 222000;
fee = 0;
ftype = 0;
id = 17755320;
mvid = 0;
name = Lo;
rUrl = "
rtype = 0;
status = 1;
}
);
};
}
可以看出{songs({({album};{artists});id;duration})}
json的相关知识资料请参考:点击打开链接
MusicData.h文件中:
//歌曲的属性
@property(nonatomic,strong)NSString *trackIdentifier;
@property(nonatomic,strong)NSString *albumIdentifier;
@property(nonatomic,strong)NSString *artistIdentifier;
@property(nonatomic,strong)NSString *trackname;
@property(nonatomic,strong)NSString *albumname;
@property(nonatomic,strong)NSString *artistname;
@property(nonatomic,strong)NSString *logoname;
@property(nonatomic,strong)NSString *duration;
//api
+ (id)musicDataWithDic:(NSDictionary *)dic;
MusicData.m文件中:
+ (id)musicDataWithDic:(NSDictionary *)dic{
return [[self alloc] initWithDic:dic];
}
- (id)initWithDic:(NSDictionary *)dic{
if (self = [super init]) {
self.trackIdentifier = [dic objectForKey:@"id"];
self.trackname = [dic objectForKey:@"name"];
self.duration = [dic objectForKey:@"duration"];
//进入字典中的字典去标识,用key去标识内层中的属性
NSDictionary *albumDic = [dic objectForKey:@"album"];
self.albumIdentifier = [albumDic objectForKey:@"id"];
self.albumname = [albumDic objectForKey:@"name"];
NSDictionary *artistDic = [[dic objectForKey:@"artists" ] firstObject];
self.artistIdentifier = [artistDic objectForKey:@"id"];
self.artistname = [artistDic objectForKey:@"name"];
self.logoname = [artistDic objectForKey:@"img1v1Url"];
}
return self;
}
这样就可以在一个UITableVIewController视图控制器中访问这些数据了,可以把数据加载进cell中了。当然这些工作留在下一个的博文中。