2012-05-22 30 views
0

JSON解析後我無法得到確切的結果。在JSON解析中,我在動態解析時遇到問題

strURLRootViewNSString對象 「RootViewController.m」類的......

strURLRootView = txt_EnterURL.text; 

strURLRank「CheckRank_ViewController.m」類的NSString對象...

strURLRank = strURLRootView; 

-(void)viewDidLoad 

{ 

    [self startJSONParsing]; 

    [super viewDidLoad]; 
} 

-(void)startJSONParsing 

{ 

    responseData = [[NSMutableData data] retain]; 
    ArrData = [NSMutableArray array]; 

    NSString *stringWithoutSpaces = [NSString stringWithFormat:@"http://lsapi.seomoz.com/linkscape/url-metrics/%@%2fblog?AccessID=member-10fd5cb877&Expires=1365850483&Signature=m%2F7hZu1y7Sa00NGLFKHYY%2FO3TB4%3D",strURLRank]; 

如果我使用

@ 「http://lsapi.seomoz.com/linkscape/url-metrics/www.yahoo.com%2fblog?AccessID=member-10fd5cb877 &過期= 1365850483 &簽名= M%2F7hZu1y7Sa00NGLFKHYY%2FO3TB4%3D」

這個URL靜態地它會給出確切的輸出。

NSLog(@"stringWithoutSpaces is:--> %@\n\n",stringWithoutSpaces); 

    NSURLRequest *request1 = [NSURLRequest requestWithURL: 


          [NSURL URLWithString:stringWithoutSpaces]]; 
    NSLog(@"request1 is:--> %@\n",request1); 

    [[NSURLConnection alloc] initWithRequest:request1 delegate:self]; 


} 

(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response 

{ 

    [responseData setLength:0]; 

} 

(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data 

{ 

    [responseData appendData:data]; 

} 

(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error 

{ 

    NSLog(@"Error occured at the time of parsing..."); 

} 

(void)connectionDidFinishLoading:(NSURLConnection *)connection 

{ 

    [connection release]; 
    NSString *responseString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding]; 
    [responseData release]; 

    results = [responseString JSONValue]; 

    lbl_RootDomain.text = [results objectForKey:@"upl"]; 
    lbl_URL.text = [results objectForKey:@"uu"]; 
    lbl_Title.text = [results objectForKey:@"ut"]; 
    lbl_SubDomain.text = [NSString stringWithFormat:@"%f",[[results objectForKey:@"fmrp"] floatValue]]; 
    lbl_MozRank.text = [NSString stringWithFormat:@"%f",[[results objectForKey:@"umrp"] floatValue]]; 
    lbl_DomainAutho.text = [NSString stringWithFormat:@"%d",[[results objectForKey:@"pda"] intValue]]; 
    lbl_XternalLinks.text = [NSString stringWithFormat:@"%d",[[results objectForKey:@"ueid"] intValue]]; 
    lbl_Links.text = [NSString stringWithFormat:@"%d",[[results objectForKey:@"uid"] intValue]]; 
    lbl_PageAutho.text = [NSString stringWithFormat:@"%f",[[results objectForKey:@"upa"] floatValue]]; 

    NSLog(@"results data is:--> %@",results); 

    NSString *str = [NSString stringWithFormat:@"Root Domain: %@\n URL: %@\n Title: %@\n Subdomain mozRank: %@\n MozRank: %@\n Domain Authority: %@\n External Links: %@\n Links: %@\n Page Authority: %@\n",lbl_RootDomain.text,lbl_URL.text,lbl_Title.text,lbl_SubDomain.text,lbl_MozRank.text,lbl_DomainAutho.text,lbl_XternalLinks.text,lbl_Links.text,lbl_PageAutho.text]; 

    NSLog(@"mail data is:--> \n\n %@",str); 
} 

我沒有得到確切的編碼問題。如果我在編碼方面錯了,請告訴我什麼是錯的。

在此先感謝。

+0

我想,如果可能strURLRank是返回null值或者也在這裏如果空間然後URL不支持空間...檢查strURLRank的值.... –

回答

1

......... Maulik Trambad IYA解決方案..........

-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response 
{ 
    [responseData setLength: 0]; 
} 

-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data 
{ 
    [responseData appendData:data]; 
} 

-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error 
{ 
    [connection release]; 
    [responseData release]; 

} 

-(void)connectionDidFinishLoading:(NSURLConnection *)connection 
{ 
    NSString *responseString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding]; 
    responseData = nil; 
    NSLog(@"%@",responseString); 
    SBJsonParser *parser = [[[SBJsonParser alloc] init] autorelease]; 
    NSDictionary *responseDict = [parser objectWithString:responseString]; 
    NSLog(@"%@",responseDict); 
    [responseDict retain];  
} 

- (NSString*) _formEncodeString: (NSString*) string 
{ 
    NSString* encoded = (NSString*) CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault, 
                      (CFStringRef) string, NULL, CFSTR("!*'();:@&=+$,/?%#[]"), kCFStringEncodingUTF8); 
    return [encoded autorelease]; 
} 

- (void)viewWillAppear:(BOOL)animated 
{ 

    responseData = [[NSMutableData data] retain]; 
    ArrData = [NSMutableArray array]; 
    NSString *[email protected]"www.gmail.com"; 
    NSString *[email protected]"%2fblog?AccessID=member-10fd5cb877&Expires=1365850483&Signature="; 
    NSString *[email protected]"m%2F7hZu1y7Sa00NGLFKHYY%2FO3TB4%3D"; 
    NSString *stringWithoutSpaces = [NSString stringWithFormat:@"http://lsapi.seomoz.com/linkscape/url-metrics/%@",strURLRank]; 
    NSLog(@"%@",stringWithoutSpaces); 

    stringWithoutSpaces=[NSString stringWithFormat:@"%@%@%@",stringWithoutSpaces,str,str1]; 

    // stringWithoutSpaces=[stringWithoutSpaces stringByReplacingOccurrencesOfString:@" " withString:@"%20"]; 
    // stringWithoutSpaces = [stringWithoutSpaces stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; 
    NSLog(@"%@",stringWithoutSpaces); 

    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:stringWithoutSpaces]]; 
    NSLog(@"%@",request); 
    [[NSURLConnection alloc] initWithRequest:request delegate:self]; 

    [super viewWillAppear:animated]; 
} 

......... Maulik Trambadiya解決成品..........

+0

@Maulik ...現在工作得很好......謝謝。 –

1

檢查strURLRank不爲空,然後使用婁代碼後...

NSString *stringWithoutSpaces = [NSString stringWithFormat:@"http://lsapi.seomoz.com/linkscape/url-metrics/%@%2fblog?AccessID=member-10fd5cb877&Expires=1365850483&Signature=m%2F7hZu1y7Sa00NGLFKHYY%2FO3TB4%3D",strURLRank]; 

NSString *urlString=[self trimString:stringWithoutSpaces]; 

和使用urlString .....

以下方法定義你的.m文件...

-(NSString*) trimString:(NSString *)theString { 
    NSString *theStringTrimmed = [theString stringByTrimmingCharactersInSet: [NSCharacterSet whitespaceAndNewlineCharacterSet]]; 
    return theStringTrimmed; 
} 

希望這有助於你.... :)

+0

我已經試過了....但這個問題還是... –

+0

是你確定strURLRank不爲空? –

+0

yes definately .... –