概述
#import <Foundation/Foundation.h>
typedef void(^CompletionHandle)(id result);
@class ASIHTTPRequest;
/*
类描述:
提供数据的工具类
*/
@interface WXDataService : NSObject
//请求数据工具方法
+ (id)requestData:(NSString *)jsonName;
//请求豆瓣的API接口
+ (ASIHTTPRequest *)requestDoubanAPI:(NSString *)subURL
parsms:(NSDictionary *)params
method:(NSString *)method
completion:(CompletionHandle)block;
//请求网易的API接口
+ (ASIHTTPRequest *)request163API:(NSString *)subURL
parsms:(NSDictionary *)params
method:(NSString *)method
completion:(CompletionHandle)block;
@end
#import "WXDataService.h"
#import "JSONKit.h"
#import "ASIFormDataRequest.h"
#define DOUBAN_URL @"https://api.douban.com"
#define WY163_URL @"http://piao.163.com"
/*
json解析的框架:
JSONKit 性能好
SBJSON
TouchJSON
NSJSONSerialization ios5.0新增的类,性能最好
*/
@implementation WXDataService
+ (id)requestData:(NSString *)jsonName {
// NSString *filePath = [[NSBundle mainBundle] pathForResource:jsonName ofType:nil];
NSString *resourcePath = [[NSBundle mainBundle] resourcePath];
NSString *filePath = [resourcePath stringByAppendingPathComponent:jsonName];
NSString *systemVersion = [[UIDevice currentDevice] systemVersion];
float version = [systemVersion floatValue];
id jsonObj = nil;
if (version >= 5.0) {
//NSJSONSerialization 5.0之后ios新添加解析json的工具类
NSData *data = [NSData dataWithContentsOfFile:filePath];
jsonObj = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
} else {
NSString *jsonData = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:nil];
//jsonkit解析json字符串
jsonObj = [jsonData objectFromJSONString];
}
return jsonObj;
}
+ (ASIHTTPRequest *)requestDoubanAPI:(NSString *)subURL
parsms:(NSDictionary *)params
method:(NSString *)method
completion:(CompletionHandle)block {
//拼接此接口的完整URL
NSString *urlstring = [NSString stringWithFormat:@"%@/%@",DOUBAN_URL,subURL];
return [self requestAPI:urlstring parsms:params
method:method completion:block];
}
+ (ASIHTTPRequest *)request163API:(NSString *)subURL
parsms:(NSDictionary *)params
method:(NSString *)method
completion:(CompletionHandle)block {
NSString *paramsString = @"?app_id=1&mobileType=iPhone&ver=2.6&channel=appstore&deviceId=9E89CB6D-A62F-438C-8010-19278D46A8A6&apiVer=6&city=110000";
NSString *urlstring = [NSString stringWithFormat:@"%@/%@%@",WY163_URL,subURL,paramsString];
return [self requestAPI:urlstring parsms:params
method:method completion:block];
}
+ (ASIHTTPRequest *)requestAPI:(NSString *)urlstring
parsms:(NSDictionary *)params
method:(NSString *)method
completion:(CompletionHandle)block {
//忽略大小写比较
NSComparisonResult result = [method caseInsensitiveCompare:@"GET"];
if (result == NSOrderedSame) { //GET请求
//start=0&count=30
NSMutableString *paramsstring = [NSMutableString string];
NSArray *allkeys = [params allKeys];
for (int i=0; i<params.count; i++) {
NSString *key = [allkeys objectAtIndex:i];
NSString *value = [params objectForKey:key];
[paramsstring appendFormat:@"%@=%@",key,value];
if (i < params.count-1) {
[paramsstring appendFormat:@"&"];
}
}
if (paramsstring.length > 0) {
//api.douban.com?start=0&count=30
urlstring = [NSString stringWithFormat:@"%@?%@",urlstring,paramsstring];
}
}
NSURL *url = [NSURL URLWithString:urlstring];
__block ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
[request setTimeOutSeconds:60];
[request setRequestMethod:method];
result = [method caseInsensitiveCompare:@"POST"];
if (result == NSOrderedSame) { //POST请求
NSArray *allkeys = [params allKeys];
for (int i=0; i<params.count; i++) {
NSString *key = [allkeys objectAtIndex:i];
NSString *value = [params objectForKey:key];
//将请求参数添加到请求体中
[request addPostValue:value forKey:key];
}
}
[request setCompletionBlock:^{
NSString *responseString = request.responseString;
NSLog(@"url:%@",request.url);
NSLog(@"result=%@",responseString);
NSData *data = request.responseData;
id result = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
//回调block
block(result);
}];
[request setFailedBlock:^{
NSLog(@"%@",request.error);
}];
[request startAsynchronous];
return request;
}
@end
最后
以上就是迅速诺言为你收集整理的DataService请求网络的全部内容,希望文章能够帮你解决DataService请求网络所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复