0

JSON解析后我无法准确输出。

strURLRootViewNSStringRootViewController.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&Expires=1365850483&Signature=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);
}

我在编码中没有得到确切的问题。如果我在编码方面错了,请告诉我出了什么问题。

提前致谢。

4

2 回答 2

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;
}

希望这对你有帮助.... :)

于 2012-05-22T08:03:46.827 回答
1

......... Maulik Trambadiya 解决方案 .........................

-(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 *strURLRank=@"www.gmail.com";
    NSString *str=@"%2fblog?AccessID=member-10fd5cb877&Expires=1365850483&Signature=";
    NSString *str1=@"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 解决方案完成............

于 2012-05-23T06:35:23.143 回答