iphone - Unable to get responses from local server when connecting via iOS device -
im using afnetworking retrieve items basic rails server in simple ios project.
when make request in simulator, works smoothly. however, when make same request while running project on device, i'm getting frustrating error.
i understand cannot connect localhost directly device , therefore need use ip address, doing. weird part: when make request server, can see in terminal server hit , returning 200 response. however, request failing (on client end) error message: 'the request timed out.'
information , code:
my rails server extremely basic. i've generated new project, set simple model called 'items' has single column -- string -- item's content. have routes set respond json requests , index method on items_controller returns results of item.all in json form.
here routes:
testingserver::application.routes.draw scope :format => true, :constraints => { :format => 'json' } resources :items, :only => [:index] end end
and here items_controller.rb
class itemscontroller < applicationcontroller def index @items = item.all render :status => 200, :json => @items end end
as ios project, here afhttpclient subclass header:
#import <foundation/foundation.h> #import "afhttpclient.h" @interface pnapiclient : afhttpclient + (pnapiclient *)sharedclient; @end
and here implementation:
#import "pnapiclient.h" #import "afjsonrequestoperation.h" static nsstring * const kpnapiclientbaseurlstring = @"http://<ip address>:9292/"; @implementation pnapiclient + (pnapiclient *)sharedclient { static pnapiclient *_sharedclient = nil; static dispatch_once_t oncetoken; dispatch_once(&oncetoken, ^{ _sharedclient = [[pnapiclient alloc] initwithbaseurl:[nsurl urlwithstring:kpnapiclientbaseurlstring]]; }); return _sharedclient; } - (id)initwithbaseurl:(nsurl *)url { self = [super initwithbaseurl:url]; if (!self) { return nil; } [self registerhttpoperationclass:[afjsonrequestoperation class]]; [self setdefaultheader:@"accept" value:@"application/json"]; return self; } @end
and finally, here request failing:
- (ibaction)testrequest:(id)sender { [[pnapiclient sharedclient] getpath:@"/items.json" parameters:nil success:^(afhttprequestoperation *operation, id json) { nslog(@"success: %@", json); } failure:^(afhttprequestoperation *operation, nserror *error) { nslog(@"failure %@", error.localizeddescription); }]; }
one last comment: tried using different url (one online example) , worked fine. makes me suspect either issue rails server, or there issue device connecting locally. said, able simulator fine, , can see hitting server device.
update 1
it appears failure block on -getpath:parameters:success:failure: being called no matter server response is. is, if server throws 422 response json representation of error, able error message on device. however, if server returns 200 response other json object, failure block still thrown... no errors of course.
afjsonrequestoperation call failure block:
- if invalid json returned
- if http status code or content-type incorrect
- if connection cancelled or fails
in of these cases, error
variable set. in fact, it's presence of error
causes failure block called (see [afjsonrequestoperation -setcompletionblockwithsuccess:failure:]
).
if log isn't outputting anything, try logging error
instead of error.localizeddescription
.
anyway, sounds server returning http 200 invalid json object. can set breakpoint in failure block , type po operation.responsestring
in debugger inspect.
Comments
Post a Comment