I tried lots but getting this error while calling this api for getting match schedule using this
- (IBAction)getButtonPressed:(id)sender
{
NSString *string = [NSString stringWithFormat:#"https://api.sportsdatallc.org/nba-Trial(t)1/ru/games/2014/reg/schedule.json?api_key=qgdmf6egtuf5guy9pdvk5ydf"];
NSURL *url = [NSURL URLWithString:string];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
NSLog(#"url is %#",url);
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
operation.responseSerializer = [AFJSONResponseSerializer serializer];
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
// 3
NSLog(#"%#",responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
// 4
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:#"Error Retrieving Weather"
message:[error localizedDescription]
delegate:nil
cancelButtonTitle:#"Ok"
otherButtonTitles:nil];
[alertView show];
}];
// 5
[operation start];
}
getting this error
Error Domain=com.alamofire.error.serialization.response Code=-1011 "Request failed: server error (596)" UserInfo=0x7994db10 {com.alamofire.serialization.response.error.response=<NSHTTPURLResponse: 0x799335b0> { URL: https://api.sportsdatallc.org/nba-Trial(t)1/ru/games/2014/reg/schedule.json?api_key=qgdmf6egtuf5guy9pdvk5ydf } { status code: 596, headers {
Connection = "keep-alive";
"Content-Length" = 30;
"Content-Type" = "text/xml";
Date = "Fri, 20 Mar 2015 08:55:01 GMT";
Server = "Mashery Proxy";
"X-Mashery-Error-Code" = "ERR_596_SERVICE_NOT_FOUND";
} }, NSErrorFailingURLKey=https://api.sportsdatallc.org/nba- Trial(t)1/ru/games/2014/reg/schedule.json?api_key=qgdmf6egtuf5guy9pdvk5ydf, NSLocalizedDescription=Request failed: server error (596), com.alamofire.serialization.response.error.data=<3c68313e 35393620 53657276 69636520 4e6f7420 466f756e 643c2f68 313e>, NSUnderlyingError=0x7994eb90 "Request failed: unacceptable content-type: text/xml"}
I am using this website api:
http://developer.sportsdatallc.com/docs/NBA_API
Details of using this api:
Daily Schedule
Schema
Syntax:
http(s)://api.sportsdatallc.org/nba-[access_level][version]/schema/schedule-v2.0.xsd?api_key=[your_api_key]
Schema Example:
Feed
Syntax:
http(s)://api.sportsdatallc.org/nba-[access_level][version]/[locale]/games/[year]/[month]/[day]/schedule.xml?api_key=[your_api_key]
Parameter Format Notes
[access_level] = Production (p), Trial (t)
[version] = whole number (sequential, starting with the number 1)
[year] = Year in 4 digit format (YYYY)
[month] = Month in 2 digit format (MM)
[day] = Day of the month in 2 digit format (DD)
[format] = xml, json
Optional Parameters
[locale] = ru (russian), zh (simplified chinese)
In -[AFHTTPRequestOperationManager initWithBaseURL:], you will find that the responseSerializer property is set to an instance of AFJSONResponseSerializer. .
AFHTTPRequestOperationManager *manager = [[AFHTTPRequestOperationManager alloc] initWithBaseURL:myURL];
To get the functionality you want(delivering a parsed XML model back to your callback as the responseObject), simply set the responseSerializer property to an instance of AFXMLResponseSerializer
manager.responseSerializer = [AFXMLResponseSerializer new];
Sample Code:
AFHTTPRequestOperationManager *manager = [[AFHTTPRequestOperationManager alloc] initWithBaseURL:myURL];
manager.responseSerializer = [AFXMLResponseSerializer new];
[manager GET:#"http://www.example.com/feed" parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(#"Data: %#", responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(#"Error: %#", error);
}];
Related
I am having an issue where I am not able to connect to the API. This happens only with iOS8. It works fine with iOS7.
- (void)performSearch:(NSString*)type : (NSString*)keyword{
url = [NSURL URLWithString:[NSString stringWithFormat:#"%#%#", API, keyword]];
request = [NSURLRequest requestWithURL:url];
operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
credential = [NSURLCredential credentialWithUser:USERNAME password:PASSWORD persistence:NSURLCredentialPersistenceForSession];
[operation setCredential:credential];
[[NSOperationQueue mainQueue] addOperation:operation];
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
NSError *error = nil;
NSDictionary *JSON = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:&error];
if (error) {
NSLog(#"Error serializing %#", error);
return;
}
[self parseJSON:JSON :keyword];
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(#"error : %#", error);
}];
[operation start];
}
The Error message I am getting.
Domain=com.alamofire.error.serialization.response Code=-1011 "Request failed: unauthorized (401)" UserInfo=0x7ff0650052f0 {com.alamofire.serialization.response.error.response=<NSHTTPURLResponse: 0x7ff062c5e4d0> { URL: https://distribution.virk.dk/cvr-permanent-prod-20150622/_search?q=anders } { status code: 401, headers {
Connection = "keep-alive";
"Content-Length" = 194;
"Content-Type" = "text/html";
Date = "Tue, 18 Aug 2015 09:36:09 GMT";
Server = "nginx/1.6.2";
"Www-Authenticate" = "Basic realm=\"Beskyttet adgang\"";
} }, NSErrorFailingURLKey=https://distribution.virk.dk/cvr-permanent-prod-20150622/_search?q=anders, com.alamofire.serialization.response.error.data=<3c68746d 6c3e0d0a 3c686561 643e3c74 69746c65 3e343031 20417574 686f7269 7a617469 6f6e2052 65717569 7265643c 2f746974 6c653e3c 2f686561 643e0d0a 3c626f64 79206267 636f6c6f 723d2277 68697465 223e0d0a 3c63656e 7465723e 3c68313e 34303120 41757468 6f72697a 6174696f 6e205265 71756972 65643c2f 68313e3c 2f63656e 7465723e 0d0a3c68 723e3c63 656e7465 723e6e67 696e782f 312e362e 323c2f63 656e7465 723e0d0a 3c2f626f 64793e0d 0a3c2f68 746d6c3e 0d0a>, NSLocalizedDescription=Request failed: unauthorized (401)}
Status code is 401, which means that your account or password is incorrect. I suggest that you should log out the params, and find out whether they are null or anything else
I am creating an iOS Application working in offline mode by using a AFNetworking library.
To manage a HTTP request I have defined a AFHTTPRequestOperationManager
- (AFHTTPRequestOperationManager *)requestOperationManager
{
if (!_requestOperationManager)
{
_requestOperationManager = [[AFHTTPRequestOperationManager alloc] initWithBaseURL:[NSURL URLWithString:SERVICE_BASE_URL]];
}
return _requestOperationManager;
}
To load a HTTP request I have defined a method:
- (void)loadRequest
{
if (!self.internetConnectionAvaliable)
{
self.requestOperationManager.requestSerializer.cachePolicy = NSURLRequestReturnCacheDataElseLoad;
}
NSString *url = SERVICE_METHOD_URL;
NSDictionary *parameters = #{#"password" : USER_PASSWORD, #"username" : USER_NAME};
[self.requestOperationManager POST:url parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject)
{
NSString *htmlString = [NSString stringWithFormat:#"%#", [responseObject objectForKey:#"view"]];
[self.webView loadHTMLString:htmlString baseURL:nil];
}
failure:^(AFHTTPRequestOperation *operation, NSError *error)
{
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:#"ERROR!!!" message:#"ERROR!!!" delegate:nil cancelButtonTitle:#"CANCEL" otherButtonTitles:nil];
[alertView show];
}];
}
When internet connection is enabled, AFNetworking loads desired data perfectly.
When internet connection is disabled, AFNetworking doesn't load any data. This makes me think, that data caching doesn't work as it should!
Does anyone knows why?
How to fix it?
I want to get address from latitude and longitude with GoogleGEO CODING (EX URL = http://maps.googleapis.com/maps/api/geocode/json?latlng=40.714224,-73.961452&sensor=true_or_false )
So I want to get JSON from that page by AFNetworking.
It's my code below :
IBAction)reLocation:(UIButton*)sender
{
if(sender.tag==1)
{
NSArray *gpsValue = [self getGPS];
float lat = [[gpsValue objectAtIndex:0] floatValue];
float lon = [[gpsValue objectAtIndex:1] floatValue];
NSString *string = [NSString stringWithFormat:#"%#%#,%#&sensor=true_or_false",GEOCODING_URL,[NSString stringWithFormat:#"%f", lat],[NSString stringWithFormat:#"%f",lon]]; // NSString *str = [NSString stringWithFormat:#"%f", myFloat];
NSLog(string);
NSURL *url = [NSURL URLWithString:string];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
operation.responseSerializer = [AFJSONResponseSerializer serializer];
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject)
{
NSLog(#"AFNetworking success");
NSDictionary *location = (NSDictionary *)responseObject;
// 3
self.title = #"JSON Retrieved";
//[self.tableView reloadData];
NSLog(responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(#"AFNetworking failure");
switch (operation.response.statusCode) {
case 400:
// Do stuff
NSLog(#"error 400");
break;
default:
NSLog([NSString stringWithFormat:#"%ld",(long)operation.response.statusCode]);
break;
}
// 4
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:#"Error Retrieving Weather"
message:[error localizedDescription]
delegate:nil
cancelButtonTitle:#"Ok"
otherButtonTitles:nil];
[alertView show];
}];
// 5
[operation start];
}}
But when I click my button, always afnetworking fails and log shows 0 for status code.
I also got url log that i put in
i already checked that url is not problem (it shows json in working order)
I debug with simulator!
Is there something I miss ?
i wanted to get data from server encoded in json to display it, so i use afnetworking library but i get error here is my code
NSURL * url =[NSURL URLWithString:#"http://software-sultan.com/alaa/image_test.php"];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
operation.responseSerializer = [AFJSONResponseSerializer serializer];
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
// 3
array = [NSJSONSerialization JSONObjectWithData:responseObject options:kNilOptions error:nil];
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
// 4
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:#"Error Retrieving Weather"
message:[error localizedDescription]
delegate:nil
cancelButtonTitle:#"Ok"
otherButtonTitles:nil];
[alertView show];
NSLog(#"%#",error);
}];
// 5
[operation start];
here is the error
Error Domain=com.alamofire.error.serialization.response Code=-1016 "Request failed: unacceptable content-type: text/html" UserInfo=0x7f920964bff0 {com.alamofire.serialization.response.error.response=<NSHTTPURLResponse: 0x7f920965cc20> { URL: http://software-sultan.com/alaa/image_test.php } { status code: 200, headers {
Connection = "keep-alive";
"Content-Length" = 126781;
"Content-Type" = "text/html";
Date = "Thu, 29 Jan 2015 19:00:55 GMT";
"Keep-Alive" = "timeout=30";
Server = "Apache/2";
"X-Powered-By" = "PHP/5.3.13";
} }, NSErrorFailingURLKey=http://software-sultan.
It looks like you are trying to parse a JSON response with AFJSONResponseSerializer, but the webserver sends text/html as content type. JSON responses are generally served with the Content-Type: application/json header.
Also, if the response was successfully parsed as JSON, the responseObject given as the second parameter of your completion block would already be a valid NSArray containing your data. So there wouldn't need to create it again using NSJSONSerialization.
I Wanna Get Data From PhP Page
My Code Is
NSURL *url = [NSURL URLWithString:#"http://avicenna-apps.com/harleychatget.php"];
AFHTTPClient * Client = [AFHTTPClient clientWithBaseURL:url];
[Client defaultValueForHeader:#"Accept"];
[Client getPath:#"" parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject)
{
NSLog(#"Data %#",responseObject);
}
failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(#"Error retrieving data: %#", error);
}];
The Output Is
Data <32352020 32303133 2d30312d 31302030 383a3234 3a353620 2041686d 65642020 68656c6c 6f206865 6c6c6f20 3c627220 2f3e3234 20203230 31332d30 312d3130 2030383a 32343a35 30202041 686d6564 20203c62 72202f3e 32332020 32303133 2d30312d 31302030 383a3234 3a343920 2041686d 65642020 3c627220 2f3e3230 20203230 31332d30 312d3130 2030383a 32343a34 38202041 686d6564 20203c62 72202f3e 32312020 32303133 2d30312d 31302030 383a3234 3a343820 2041686d 65642020 3c627220 2f3e200a 0a>
Any Help Please ?
That is binary data. You should convert it.
Try NSString *response = [[NSString alloc] initWithData:(NSData*)responseObject encoding:NSUTF8StringEncoding]; to get the response as string
or simple:
NSString *response = request.responseString;