0

JSON 解析後に正確な出力を取得できません。

strURLRootView「 RootViewController.m」クラスのNSStringオブジェクトです...

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

......... マウリック・トランバディヤ ソリューション ..........

-(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 に答える
1

strURLRank が null でないことを確認した後、次のコードを使用します ...

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 に答える