JSON 授权使用 NSURL 解析
JSON parsing using NSURL with authorization
在下面的代码中,JSONparsing 方法工作正常,但是当我在 odataJsonParsing 方法中更改 url 时:http://192.168.1.37:8000/sap/opu/odata/sap/ZGW_BANK_DATA_SRV/BankSet? $filter=(BankCtry%20eq%20%27IN%27)&$format=json 无法加载数据,它显示为 Nil,但对于剩余的 urls,同样的方法工作正常,可以有人告诉我哪里出错了。
**Condition**: In odataJsonParsing method have authorization credentials like.
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL: [NSURL URLWithString:@"http://192.168.1.37:8000/sap/opu/odata/sap/ZGW_BANK_DATA_ SRV/BankSet?$filter=(BankCtry%20eq%20%27US%27)&$format=json"]];
[request setHTTPMethod: @"GET" ];
// [request setHTTPBody:self.requestData];
[request setValue:@"XXXXXXX" forHTTPHeaderField:@"username"];
[request setValue:@"XXXXXXX" forHTTPHeaderField:@"password"];
[request setValue:[[NSUserDefaults standardUserDefaults]valueForKey:@"Auth"] forHTTPHeaderField:@"Authorization"];
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
-----------------------------------------------------------------
-(void )JSONParsing
{
NSError *error;
//--------------------------URLS----------------------------//
//http://api.geonames.org/citiesJSON? north=44.1&south=-9.9&east=-22.4&west=55.2&lang=de&username=demo
//http://api.kivaws.org/v1/loans/search.json?status=fundraising
//http://192.168.1.37:8000/sap/opu/odata/sap/ZGW_BANK_DATA_SRV/BankSet? $filter=(BankCtry%20eq%20%27IN%27)&$format=json
//http://jsonplaceholder.typicode.com/posts
NSURL *url=[NSURL URLWithString:@"http://jsonplaceholder.typicode.com/posts"];
NSData *jsonData=[[NSData alloc] initWithContentsOfURL:url];
if(jsonData!=nil){
NSDictionary *jsonArray=[NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:&error];
NSLog(@"%@",jsonArray);
}
}
-(void)oDataJsonParsing
{
NSError *error;
NSData *jsonData=[[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:@"http://192.168.1.37:8000/sap/opu/odata/sap/ZGW_BANK_DATA_ SRV/BankSet?$filter=(BankCtry%20eq%20%27IN%27)&$format=json"]];
NSDictionary *dic=[NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:&error];
NSLog(@"%@",dic);
}
NSURL *url = [NSURL URLWithString:@"http://192.168.1.37:8000/sap/opu/odata/sap/ZGW_BANK_DATA_SRV/BankSet?$filter=(BankCtry%20eq%20%27CH%27)&$format=json"];
NSMutableURLRequest *request= [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:120.0];
NSString *authStr = [NSString stringWithFormat:@"%@:%@", @"abaper", @"initial@xyz"];
NSData *authData = [authStr dataUsingEncoding:NSUTF8StringEncoding];
NSString *authValue = [NSString stringWithFormat:@"Basic %@", [authData base64EncodedStringWithOptions:0]];
[request setValue:authValue forHTTPHeaderField:@"Authorization"];
NSError *error ;
NSHTTPURLResponse *responseCode = nil;
NSData *oResponseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&responseCode error:&error];
if([responseCode statusCode] != 200)
{
NSLog(@"Error getting %@, HTTP status code %li", url, (long)[responseCode statusCode]);
}
else{
NSDictionary *responsedata = [NSJSONSerialization JSONObjectWithData:oResponseData options:NSJSONReadingAllowFragments error:&error];
NSLog(@"%@",response data);
}
在下面的代码中,JSONparsing 方法工作正常,但是当我在 odataJsonParsing 方法中更改 url 时:http://192.168.1.37:8000/sap/opu/odata/sap/ZGW_BANK_DATA_SRV/BankSet? $filter=(BankCtry%20eq%20%27IN%27)&$format=json 无法加载数据,它显示为 Nil,但对于剩余的 urls,同样的方法工作正常,可以有人告诉我哪里出错了。
**Condition**: In odataJsonParsing method have authorization credentials like.
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL: [NSURL URLWithString:@"http://192.168.1.37:8000/sap/opu/odata/sap/ZGW_BANK_DATA_ SRV/BankSet?$filter=(BankCtry%20eq%20%27US%27)&$format=json"]];
[request setHTTPMethod: @"GET" ];
// [request setHTTPBody:self.requestData];
[request setValue:@"XXXXXXX" forHTTPHeaderField:@"username"];
[request setValue:@"XXXXXXX" forHTTPHeaderField:@"password"];
[request setValue:[[NSUserDefaults standardUserDefaults]valueForKey:@"Auth"] forHTTPHeaderField:@"Authorization"];
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
-----------------------------------------------------------------
-(void )JSONParsing
{
NSError *error;
//--------------------------URLS----------------------------//
//http://api.geonames.org/citiesJSON? north=44.1&south=-9.9&east=-22.4&west=55.2&lang=de&username=demo
//http://api.kivaws.org/v1/loans/search.json?status=fundraising
//http://192.168.1.37:8000/sap/opu/odata/sap/ZGW_BANK_DATA_SRV/BankSet? $filter=(BankCtry%20eq%20%27IN%27)&$format=json
//http://jsonplaceholder.typicode.com/posts
NSURL *url=[NSURL URLWithString:@"http://jsonplaceholder.typicode.com/posts"];
NSData *jsonData=[[NSData alloc] initWithContentsOfURL:url];
if(jsonData!=nil){
NSDictionary *jsonArray=[NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:&error];
NSLog(@"%@",jsonArray);
}
}
-(void)oDataJsonParsing
{
NSError *error;
NSData *jsonData=[[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:@"http://192.168.1.37:8000/sap/opu/odata/sap/ZGW_BANK_DATA_ SRV/BankSet?$filter=(BankCtry%20eq%20%27IN%27)&$format=json"]];
NSDictionary *dic=[NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:&error];
NSLog(@"%@",dic);
}
NSURL *url = [NSURL URLWithString:@"http://192.168.1.37:8000/sap/opu/odata/sap/ZGW_BANK_DATA_SRV/BankSet?$filter=(BankCtry%20eq%20%27CH%27)&$format=json"];
NSMutableURLRequest *request= [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:120.0];
NSString *authStr = [NSString stringWithFormat:@"%@:%@", @"abaper", @"initial@xyz"];
NSData *authData = [authStr dataUsingEncoding:NSUTF8StringEncoding];
NSString *authValue = [NSString stringWithFormat:@"Basic %@", [authData base64EncodedStringWithOptions:0]];
[request setValue:authValue forHTTPHeaderField:@"Authorization"];
NSError *error ;
NSHTTPURLResponse *responseCode = nil;
NSData *oResponseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&responseCode error:&error];
if([responseCode statusCode] != 200)
{
NSLog(@"Error getting %@, HTTP status code %li", url, (long)[responseCode statusCode]);
}
else{
NSDictionary *responsedata = [NSJSONSerialization JSONObjectWithData:oResponseData options:NSJSONReadingAllowFragments error:&error];
NSLog(@"%@",response data);
}