我有一个iphone应用程序,我正在尝试使用HTTPRiot对Web应用程序进行一些API调用。问题是我看不到没有调用HTTPRiot委托方法。我已经登录了所有委托方法,我也在查看网络服务器日志。我看到网址被点击了。
//API.h
#import <Foundation/Foundation.h>
#include <HTTPRiot/HTTPRiot.h>
@interface API : HRRestModel {
}
+(void)runTest;
@end
//API.m
#import "API.h"
@implementation API
+ (void)initialize {
NSLog(@"api initialize");
[self setDelegate:self];
[self setBaseURL:[NSURL URLWithString:@"http://localhost:3000/api"]];
[self setBasicAuthWithUsername:@"demo" password:@"123456"];
NSDictionary *params = [NSDictionary dictionaryWithObject:@"1234567" forKey:@"api_key"];
[self setDefaultParams:params];
}//end initialize
+(void)runTest {
NSLog(@"api run test");
// Would send a request to http://localhost:1234/api/people/1?api_key=1234567
[self getPath:@"/save_diet" withOptions:nil object:nil];
}
+(void)restConnection:(NSURLConnection *)connection didReturnResource:(id)resource object:(id)object {
NSLog(@"didReturnResource");
}
+(void)restConnection:(NSURLConnection *)connection didReceiveResponse:(NSHTTPURLResponse *)response object:(id)object {
NSLog(@"didReceiveResponse");
}
+(void)restConnection:(NSURLConnection *)connection didReceiveParseError:(NSError *)error responseBody:(NSString *)body object:(id)object {
NSLog(@"didReceiveParseError");
}
+(void)restConnection:(NSURLConnection *)connection didReceiveError:(NSError *)error response:(NSHTTPURLResponse *)response object:(id)object {
NSLog(@"didReceiveError");
}
+(void)restConnection:(NSURLConnection *)connection didFailWithError:(NSError *)error object:(id)object {
NSLog(@"didFailWithError");
}
@end
//test code
[API runTest];
//log output
答案 0 :(得分:0)
2件事:
1-- 你正在使用类方法错误。 + initialize是一个保证由运行时调用一次的方法。它适用于设置静态变量等(以线程安全的方式)
你想用 - (id)init来设置它。你在一个类方法中为initialize和runTest调用'self',它们是垃圾或零。让runTest成为一个实例方法,我相信你会得到结果。
2 - 使用Charles检查进出应用程序的内容。
如果您从服务器获得预期的响应,那么,它是您的应用程序。