作者热门文章
- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
我正在使用 AFNetworking 在 VolunteerMatch API 上执行网络请求。当我执行请求时,我收到一个 200 代码,但我没有收到回复。典型的 VolunteerMatch 请求如下所示:
GET /api/call?action=helloWorld&query=... HTTP/1.1
Host: www.volunteermatch.org
Accept-Charset: UTF-8
Content-Type: application/json
Authorization: WSSE profile="UsernameToken"
X-WSSE: UsernameToken Username="acme", PasswordDigest="quR/EWLAV4xLf9Zqyw4pDmfV9OY=",
Nonce="d36e316282959a9ed4c89851497a717f", Created="2003-12-15T14:43:07-0700"
Here是扩展文档。
这是我用来发出请求的代码:
// Create parameters
NSDictionary* param = @{
@"action":@"helloWorld",
@"query":@"{\"name\":\"john\"}"
};
// Create Manager
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
manager.responseSerializer = [AFJSONResponseSerializer serializer];
manager.requestSerializer = [AFJSONRequestSerializer serializer];
// Add Headers
[manager.requestSerializer setValue:@"WWSE profile=\"UsernameToken\"" forHTTPHeaderField:@"Authorization"];
[manager.requestSerializer setValue:[CocoaWSSE headerWithUsername:@"XXXXX" password:@"XXXXXXXXXXX"] forHTTPHeaderField:@"X-WSSE"];
// Perform Request
[manager GET:@"http://www.volunteermatch.org/api/call" parameters:param success:^(AFHTTPRequestOperation * _Nonnull operation, id _Nonnull responseObject) {
NSLog(@"HTTP Request URL: %@", [operation.request URL]);
NSLog(@"HTTP Response Status Code: %ld", [operation.response statusCode]);
NSLog(@"HTTP Response Body: %@", responseObject);
} failure:^(AFHTTPRequestOperation * _Nullable operation, NSError * _Nonnull error) {
NSLog(@"HTTP Request failed: %@", error);
}];
这是我得到的回复:
2015-11-17 16:53:36.476 XXXXXX[85280:5969811] HTTP Response Status Code: 200
2015-11-17 16:53:36.476 XXXXXX[85280:5969811] HTTP Response Body: (null)
最佳答案
我正在使用一种常用方法进行 AFNetworking WS 调用。用途:
调用 WS:
NSDictionary* param = @{
@"action":@"helloWorld",
@"query":@"{\"name\":\"john\"}"
};
[self requestWithUrlString:@"URL" parmeters:paramDictionary success:^(NSDictionary *response) {
//code For Success
} failure:^(NSError *error) {
// code for WS Responce failure
}];
添加两个方法:这两个方法是通用的,你可以使用 NSObject 类在整个项目中使用这些通用方法。还添加//定义错误代码,例如...
- (void)requestWithUrlString:(NSString *)stUrl parmeters:(NSDictionary *)parameters success:(void (^)(NSDictionary *response))success failure:(void (^)(NSError *error))failure {
[self requestWithUrl:stUrl parmeters:parameters success:^(NSDictionary *response) {
if([[response objectForKey:@"success"] boolValue]) {
if(success) {
success(response);
}
}
else {
NSError *error = [NSError errorWithDomain:@"Error" code:kDefaultErrorCode userInfo:@{NSLocalizedDescriptionKey:[response objectForKey:@"message"]}];
if(failure) {
failure(error);
}
}
} failure:^(NSError *error) {
if(failure) {
failure(error);
}
}];}
和//在下面的方法中设置标题(如果需要,否则删除)
- (void)requestWithUrl:(NSString *)stUrl parmeters:(NSDictionary *)parameters success:(void (^)(NSDictionary *response))success failure:(void (^)(NSError *))failure {
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
[manager setResponseSerializer:[AFHTTPResponseSerializer serializer]];
[manager.requestSerializer setValue:@"WWSE profile=\"UsernameToken\"" forHTTPHeaderField:@"Authorization"];
[manager GET:stUrl parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
if([responseObject isKindOfClass:[NSDictionary class]]) {
if(success) {
success(responseObject);
}
}
else {
NSDictionary *response = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingAllowFragments error:nil];
if(success) {
success(response);
}
}
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Error: %@", error);
if(failure) {
failure(error);
}
}];}
对于任何问题和更多详细信息 please visit AFNetworking
更新
---------- 使用 AFNetworking 3.0 迁移 ----------请将您的 AFN-2.0 迁移到 3.0,并做一些小改动 chaeck here AFN 3.0
关于ios - 志愿者匹配 API Objective-C ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33767908/
我是一名优秀的程序员,十分优秀!