I've got an iphone app where I'm trying to use HTTPRiot to make some API calls to a web app. Problem is I can't see that none of the HTTPRiot delegate methods are being called. I've got a log in all the delegate methods, and I'm also looking at the webserver log. I see that the URL is being hit.
//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:@"开发者_如何学C1234567" 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
2 Things:
1-- you're using class methods wrong. +initialize is a method that is guaranteed to be called once by the runtime. It's good for setting up static variables and the like (in a thread safe way)
you want to use -(id)init for setting that up. You are calling 'self' in a class method for both initialize and runTest, which either is garbage or nil. Make runTest an instance method instead, and I am sure you'll get results.
2 -- Check the stuff coming in and out of the app using Charles.
http://www.charlesproxy.com/
If you're getting the expected responses from the server, then yeah, its your app.
精彩评论