2015-12-18 103 views
0

我有這個RESTFUL API調用。我嘗試連接到Web服務。響應成功接收,但是當我註銷數據接收時,它沒有。請幫幫我。我的休息信息是否錯誤?IOS如何使用RESTFUL API從Web服務檢索數據

這裏是我的編碼:

- (void)viewDidLoad { 
    [super viewDidLoad]; 


    // Do any additional setup after loading the view. 

    feeds = [[NSMutableArray alloc] init]; 

    NSString *restMessage = [NSString stringWithFormat:@"https://deepsightapi.symantec.com/v1/domains/www.google.com/"]; 



    NSURL *url = [NSURL URLWithString:restMessage]; 
    NSURLRequest *theRequest = [NSURLRequest requestWithURL:url]; 







    NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self]; 
    //[connection start]; 

    if(connection) 
    { 
     _webResponseData = [NSMutableData data] ; 


    } 
    else 
    { 
     NSLog(@"Connection is NULL"); 
    } 

} 
- (void)connection:(NSURLConnection *) connection didReceiveResponse:(NSURLResponse *)response 
{ 
    NSLog(@"Response recieved"); 
    [self.webResponseData setLength:0]; 
} 

- (void)connection:(NSURLConnection*) connection didReceiveData:(NSData *)data 
{ 
    NSLog(@"Data recieved"); 
    [self.webResponseData appendData:data]; 

} 

-(void)connectionDidFinishLoading:(NSURLConnection *)connection { 

    NSLog(@"Received %lu Bytes", (unsigned long)[_webResponseData length]); 
    NSString *theXML = [[NSString alloc] initWithBytes: 
         [_webResponseData mutableBytes] length:[_webResponseData length] encoding:NSUTF8StringEncoding]; 

    NSLog(@"hello %@",theXML); 

    //now parsing the xml 

    NSData *myData = [theXML dataUsingEncoding:NSUTF8StringEncoding]; 

    NSXMLParser *xmlParser = [[NSXMLParser alloc] initWithData:myData]; 

    //setting delegate of XML parser to self 
    xmlParser.delegate = self; 

    // Run the parser 
    @try{ 
     BOOL parsingResult = [xmlParser parse]; 
     NSLog(@"parsing result = %hhd",parsingResult); 
    } 
    @catch (NSException* exception) 
    { 
     UIAlertView* alert = [[UIAlertView alloc]initWithTitle:@"Server Error" message:[exception reason] delegate:self cancelButtonTitle:@"OK" otherButtonTitles: nil]; 
     [alert show]; 
     return; 
    } 
} 
+1

你可以發佈你的NSLogs嗎? – Duc

回答

0

你爲什麼不使用AFNetworking呢?如果您使用AFNetworking,您可以輕鬆操作。

AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager]; 
[manager.responseSerializer.acceptableContentTypes setByAddingObject:@"text/html"]; 
manager.responseSerializer = [AFHTTPResponseSerializer serializer]; 

NSString *urlStr=[NSString stringWithFormat:@"%@companyinfo",BASE_URL]; 
[manager GET:urlStr parameters:nil 
    success:^(AFHTTPRequestOperation *operation, id responseObject) 
{ 
    NSString *jsonString = [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding]; 
    NSData *data = [jsonString dataUsingEncoding:NSUTF8StringEncoding]; 
    NSDictionary *jsonResponse = [NSJSONSerialization JSONObjectWithData:data 
                    options:kNilOptions 
                    error:nil]; 

    NSLog(@"jsonResponse %@", jsonResponse); 

} 

    failure: 
^(AFHTTPRequestOperation *operation, NSError *error) { 
    NSLog(@"Error: %@", error); 

}]; 
+0

他特別要求基於NSURLConnection的解決方案,而不是在第三方庫上。此外,數據沒有被記錄,這與使用第三方庫不同。 – jbehrens94

1
NSURLSessionConfiguration *configurationSession = [NSURLSessionConfiguration defaultSessionConfiguration]; 
NSURLSession *defaultSession = [NSURLSession sessionWithConfiguration:configurationSession delegate:self delegateQueue:[NSOperationQueue mainQueue]]; 
NSMutableString *urlParams = [[NSMutableString alloc] init]; 
NSMutableString *mainUrl = [[NSMutableString alloc] initWithString:baseUrl]; 
if (dictParameter) { 
     for (NSString *key in dictParameter) { 
      if(urlParams.length){ 
       [urlParams appendString:@":"]; 
      } 
      NSString *value = [dictParameter objectForKey:key]; 
      [urlParams appendFormat:@"/%@",value]; 
     } 
    [mainUrl appendString:urlParams]; 
} 
NSLog(@"Main url %@",mainUrl); 
NSURLRequest *requestUrl = [[NSURLRequest alloc] initWithURL:[NSURL URLWithString:mainUrl]]; 

    NSURLSessionDataTask *dataTask = [defaultSession dataTaskWithRequest:requestUrl completionHandler:^(NSData * data, NSURLResponse * response, NSError * error) {  }]; 
    [dataTask resume]; 
2

我用這種方式,你可以把你的反應成爲字典或數組。 請用下面的代碼替換你的代碼。

- (void)connectionDidFinishLoading:(NSURLConnection *)connection { 
     NSLog(@"Succeeded! Received %d bytes of data",[_webResponseData length]); 

     NSString *responseText = [[NSString alloc] initWithData:_webResponseData encoding: NSASCIIStringEncoding]; 
     NSLog(@"Response by server is : %@", responseText); 
     NSError *e; 

     // Convert into Array 
     NSMutableArray *object = [NSJSONSerialization JSONObjectWithData:_webResponseData options:NSJSONReadingMutableContainers error:&e]; 
NSLog(@"%@",object[0]); 
    // Print your field 

    // Convert into Dictionary 

NSMutableDictionary *objectDic = [NSJSONSerialization JSONObjectWithData:_webResponseData options:NSJSONReadingMutableContainers error:&e]; 
NSLog(@"%@",objectDic[@"your Key name"]); 

請讓我知道如果它的工作。 謝謝

相關問題