我正在我們的客戶公司做一項服務。我試圖通過AFNetWorking從他們的服務器獲得一些信息(我們的客戶鼓勵使用AFNetWorking) 我做了一些使用AFNetWorking的示例,它是可行的。 但是當我使用我們的客戶的一個URL來獲取JSON數據,它失敗了,這是錯誤的描述:錯誤代碼-1011當我使用AFNetWorking
Error Domain=com.alamofire.networking.error Code=-1011
"Expected status code <NSIndexSet: 0x7e274f0>[number of indexes: 100 (in 1 ranges),
indexes: (200-299)], got 403" UserInfo=0x7b64040 {NSErrorFailingURLKey=<url_hidden_for_stackoverflow>,
NSLocalizedDescription=Expected status code <NSIndexSet: 0x7e274f0>[number of indexes: 100 (in 1 ranges), indexes: (200-299)], got 403}
我試圖找出一些解決方案,但我不能修復呢。有我的代碼:
NSURL *url = [NSURL URLWithString:@"http://example.com/"];
AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:url];
//[httpClient setDefaultHeader:@"Accept" value:@"text/json"];
//NSDictionary *params = [NSDictionary dictionaryWithObjectsAndKeys:CONST_KEY_REGISTER_UNIQUE_KEY, CONST_API_KEY_TEXT,nil];
NSMutableURLRequest *request = [httpClient requestWithMethod:@"POST" path:@"path/to/page.json" parameters:nil];
[httpClient release];
AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {
NSString *status = [JSON valueForKey:@"status"];
if ([status isEqualToString:@"OK"]) {
NSString *uniqueId = [JSON valueForKey:@"uniqueId"];
[UserSettings setWithKey:CONST_PROGRAM_UNIQUE_KEY value:uniqueId];
}
//NSString *message = [json valueForKey:@"message"];
//NSString *error = [json valueForKey:@"error"];
[[LoadingView instance] performSelectorOnMainThread:@selector(removeLoadingView) withObject:nil waitUntilDone:YES];
}
failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error) {
NSString *errorString = [error description];
[[LoadingView instance] performSelectorOnMainThread:@selector(removeLoadingView) withObject:nil waitUntilDone:YES];
}];
NSOperationQueue *queue = [[[NSOperationQueue alloc] init] autorelease];
[queue addOperation:operation];
感謝您的閱讀,任何幫助或答覆將不勝感激。
編輯:正如DarkDust所說:服務器拒絕我的訪問。但我可以通過基本的連接從服務器獲取數據: 這裏是代碼獲取:
NSURL *url = [NSURL URLWithString:@"http://example.com/path/to/page.json"];
NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData timeoutInterval:CONST_CONNECTION_TIMEOUT];
rssConnection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
[self performSelectorOnMainThread:@selector(downloadStarted) withObject:nil waitUntilDone:NO];
if (rssConnection != nil) {
do {
[[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]];
} while (!done);
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
// I can get text here, but it is not JSON format
NSString *content = [NSString stringWithUTF8String:[data bytes]];
}
我不知道爲什麼rssConnection可以得到JSON文本和AFHTTPClient不能?
您是否真的認爲在不混淆的情況下發布客戶的內部/隱私細節是一個好主意? – DarkDust
這只是一個測試頁面,不是私人頁面。在我完成之前會有更多的變化。未來,需要API密鑰才能訪問,當然,我也會隱藏它 – magichero
不過,現在每個人都可以看到你的客戶是誰。您的客戶可能不會很高興。 – DarkDust