objective c - NSMutableUrlRequest keeps failing -
i'm finish newbie concept of server backend connected ios app, thought should know more it. created function below help of tutorials on internet. however, when executed maintain getting error message (500, eventhough when test same url webapp works). know what's causing this?
- (void)loadninjas { nsurlrequest *request = [nsurlrequest requestwithurl: [nsurl urlwithstring:@"http://www.load.com/ninjas"]]; nsmutableurlrequest *mutablerequest = [request mutablecopy]; [mutablerequest addvalue:_xauthtoken forhttpheaderfield:@"x-auth-token"]; request = [mutablerequest copy]; [[nsurlconnection alloc] initwithrequest:request delegate:self]; afhttprequestoperation *operation = [[afhttprequestoperation alloc] initwithrequest:request]; operation.responseserializer = [afjsonresponseserializer serializer]; [operation setcompletionblockwithsuccess:^(afhttprequestoperation *operation, id responseobject) { nsarray *jsonarray = (nsarray *)responseobject; nsmutablearray *tempninjas = [[nsmutablearray alloc] init]; (nsdictionary *dic in jsonarray) { ninja *ninja = [[ninja alloc] initwithdictionary:dic]; [tempninjas addobject:ninja]; } self.ninjas = [[nsarray alloc] initwitharray:tempninjas]; tempninjas = nil; [self.tableview reloaddata]; } failure:^(afhttprequestoperation *operation, nserror *error) { uialertview *alertview = [[uialertview alloc] initwithtitle:@"error retrieving services" message:[error localizeddescription] delegate:nil cancelbuttontitle:@"ok" otherbuttontitles:nil]; [alertview show]; }]; [operation start]; }
and illustration of json fetches:
{ "data" : [ { "name" : "test1", "text" : "test1test1" }, { "name" : "test2", "text" : "test2test2", }, { "name" : "test3", "text" : "test3test3", } ] }
the request goes laravel route:
route::group(['before' => 'auth.token'], function () { route::resource('ninjas', 'apicontroller'); });
and function calls:
public function index() { $payload = request::header('x-auth-token'); $check = authtoken::validate($payload); $userid = $check['id']; $ninjas = db::table('ninjas') ->where('userid','=', $userid) ->get(); homecoming $this->respond([ 'data' => $this->ninjastransformer->transformcollection($ninjas) ]); }
you have lines say:
nsurlrequest *request = [nsurlrequest requestwithurl:[nsurl urlwithstring:@"http://www.load.com/ninjas"]]; nsmutableurlrequest *mutablerequest = [request mutablecopy]; [mutablerequest addvalue:_xauthtoken forhttpheaderfield:@"x-auth-token"]; request = [mutablerequest copy];
you can simplify just:
nsurlrequest *request = [nsmutableurlrequest requestwithurl:[nsurl urlwithstring:@"http://www.load.com/ninjas"]]; [request addvalue:_xauthtoken forhttpheaderfield:@"x-auth-token"];
you initiating both nsurlconnection
afnetworking request. i'd pick 1 or other (afnetworking going easier). eliminate line says:
[[nsurlconnection alloc] initwithrequest:request delegate:self];
regarding why you're receiving 500 status code, don't have plenty info diagnose that. can seek monitoring successful connection via charles , comparing see when charles monitors app trying same connection.
but don't have plenty info diagnose exactly web service needs/expects.
objective-c xcode laravel
No comments:
Post a Comment