2011-03-21 88 views
0

我已經編寫了這樣的代碼,但我無法理解如何查找數據是否存在於URL中。任何人都可以幫助我解決這個問題嗎?如何知道數據是否存在於url中不是

注意:當循環進入第二個條件時,此代碼終止。那是它終止的地方。

-(void)getdetails 
{ 

    NSLog(@"in get details"); 
    NSURL *jsonurl=[NSURL URLWithString:@"http://www.myappdemo.com/checkout/services/getonlineusers.php"]; 
    NSMutableURLRequest *request=[[[NSMutableURLRequest alloc]init ]autorelease]; 

    [request setURL:jsonurl]; 
    [request setHTTPMethod:@"POST"]; 

    [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"]; 

    NSError *error; 
    NSURLResponse *response; 
    NSData *serverReply = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error]; 
NSString *replyString = [[NSString alloc] initWithBytes:[serverReply bytes] length:[serverReply length] encoding: NSASCIIStringEncoding]; 

    if([replyString isEqualToString:@"Invalid."]){ // i have not set the php code to output "invalid" so this will not work for now ... 

     NSLog(@"%@",replyString); 
    } 
    else { 
     NSMutableArray *tempArray =[replyString JSONValue]; 

     int count=0; 
     self.temparray=tempArray; 

     for(int i=0;i<[tempArray count];i++) 
     { 
      ///////Here in this loop when it is entering it is terminating ///////// 

      NSDictionary *dict=[tempArray objectAtIndex:i]; 
      NSLog(@"DICT is %@",dict); 

      NSString *string=[dict objectForKey:@"profilepic"]; 
      NSURL *finalURL = [NSURL URLWithString:[string stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding]]; 
      NSLog(@"encoding string is %@",finalURL); 
      NSURL *url=[NSURL URLWithString:string]; 
      NSString *source = [NSString stringWithContentsOfURL:finalURL encoding:NSUTF8StringEncoding error:nil]; 
      NSFileManager *fileManager = [NSFileManager defaultManager]; 
      BOOL fileExists = [[NSFileManager defaultManager] fileExistsAtPath:url]; 
      NSLog(@"url is %@",url); 
      NSData *data=[NSData dataWithContentsOfURL:url]; 

      if(!(data==nil)) 
      { 
     NSLog(@"data is there"); 

       UIImage *image=[[UIImage alloc]initWithData:data]; 

       [self.array addObject:image]; 
       [image release]; 
      } 
      else { 
       /* 
       NSLog(@"if data is null condition block"); 
       UIImage *image=[[UIImage alloc]init]; 
       [self.array addObject:image]; 
       [image release];  
       */ 
      } 

      count=count+1; 

      NSLog(@"count is %d",count); 

     } 

    } 

    [replyString release]; 
} 
+0

而你的代碼格式化:) – deanWombourne 2011-03-21 14:48:58

回答

1

應用程序終止的地方,什麼是異常?你是否已經逐步瞭解了每次迭代期間數組對象的外觀?它在NSData initWithContentsOfURL失敗嗎?你爲什麼不把它作爲一個單獨的同步請求來測試,然後測試一下,看看你是否得到了迴應?

關於您的第一個(以及任何後續的)同步請求,最好添加一個檢查以確保您收到了有效的響應(對於格式化,此時代碼標記不能很好地播放)

if (response!=nil) {if([response isKindOfClass:[NSHTTPURLResponse class]]) { 
// you have a valid response }} 
相關問題