NSMutableUrlRequest一直失败

时间:2014-10-13 20:05:44

标签: objective-c xcode laravel

我是连接到iOS应用程序的服务器后端概念的完全新手,但我想我应该对它有更多了解。我在互联网上的一些教程的帮助下创建了下面的功能。但是,当执行此操作时,我不断收到错误消息(500,尽管当我使用我的webapp测试相同的URL时,它仍然有效)。有谁知道是什么导致了这个?

- (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];

        for (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];
}

这是它提取的JSON的一个例子:

{
    "data" : [
        {
            "name" : "test1",
            "text" : "test1test1"
        },
        {
            "name" : "test2",
            "text" : "test2test2",
        },
        {
            "name" : "test3",
            "text" : "test3test3",
        }
    ]
}

请求进入Laravel路线:

Route::group(['before' => 'auth.token'], function ()
{

    Route::resource('ninjas', 'ApiController'); 

});

这就是它所调用的功能:

public function index()
{

    $payload = Request::header('X-Auth-Token');

    $check = AuthToken::validate($payload);

    $userid = $check['id'];

    $ninjas = DB::table('ninjas')
                ->where('userid','=', $userid)
                ->get();

    return $this->respond([

        'data' => $this->NinjasTransformer->transformCollection($ninjas)

    ]);

}

1 个答案:

答案 0 :(得分:0)

你有句话说:

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];

您可以将其简化为:

NSURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"http://www.load.com/ninjas"]];
[request addValue:_xAuthToken forHTTPHeaderField:@"X-Auth-Token"];

您还要发起NSURLConnection以及AFNetworking请求。我会选择其中一个(AFNetworking可能会更容易)。所以消除说:

的界限
[[NSURLConnection alloc] initWithRequest:request delegate:self];

关于您收到500状态代码的原因,我们没有足够的信息来诊断它。您可以尝试通过Charles监控成功连接,然后将其与Charles在您尝试使用相同连接时监控应用时看到的内容进行比较。

但是我们没有足够的信息来诊断您的Web服务究竟需要/期望的内容。