0
我需要從iOS6中的URL中獲取JSON數據。我如何獲得這些數據到iOS?如果數據中存在圖像的URL,我該如何獲取這些圖像呢?如何從iOS中包含圖像的URL獲取數據?
我需要從iOS6中的URL中獲取JSON數據。我如何獲得這些數據到iOS?如果數據中存在圖像的URL,我該如何獲取這些圖像呢?如何從iOS中包含圖像的URL獲取數據?
解析JSON和將超鏈接放入Array或的NSString
UIImageView *image1;
NSString *responseString;//it will contain the url
image1.image=[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:responseString]]];
/**
HTTP CONSTANTS
**/
#define TIMEOUT_INTERVAL 60
#define CONTENT_TYPE @"Content-Type"
#define URL_ENCODED @"application/x-www-form-urlencoded"
#define GET @"GET"
#define POST @"POST"
-(NSMutableURLRequest*)getNSMutableURLRequestUsingPOSTMethodWithUrl:(NSString *)url postData:(NSString*)_postData
{
NSMutableURLRequest *req = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:url] cachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData timeoutInterval:TIMEOUT_INTERVAL];
[req setHTTPMethod:POST];
[req addValue:URL_ENCODED forHTTPHeaderField:CONTENT_TYPE];
[req setHTTPBody: [_postData dataUsingEncoding:NSUTF8StringEncoding]];
return req;
}
-(NSMutableURLRequest*)getNSMutableURLRequestUsingGetMethodWithUrl:(NSString*)url
{
NSMutableURLRequest *req = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:url] cachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData timeoutInterval:TIMEOUT_INTERVAL];
[req setHTTPMethod:GET];
return req;
}
NSString *_postData = {<Your postData>}
NSMutableURLRequest *req = [self getNSMutableURLRequestUsingPOSTMethodWithUrl:LOGIN_URL postData:_postData];
[NSURLConnection sendAsynchronousRequest:req queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *error)
{
if (error)// it means server error
{
//error while connecting server
}
else
{
NSError *errorInJsonParsing;
NSDictionary *json = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&errorInJsonParsing];
if(errorInJsonParsing) //error parsing in json
{
//error in json parsing
}
else
{
@try
{
NSLog(@"json==%@",json);
// json is basically in key value format which is NSDictionary
// if you get the url of image inside this json, then again hit the url to get the image (u can get the image in NSData which is declared above)
}
@catch (NSException *exception)
{
//exception
}
}
}
}];
https://github.com/AFNetworking/AFNetworking – amar
查覈這些鏈路,[JSON解析在IOS](HTTP :http://stackoverflow.com/questions/5547311/how-do-i-parse-json-with-objective-c)和[圖片下載在iOS](http://stackoverflow.com/questions/6238139/ios-download -and-save-image-inside-app) – Amar
[從URL /服務器獲取圖像]的可能重複(http://stackoverflow.com/questions/1286096/getting-image-from-url-server) –