Show
Ignore:
Timestamp:
12/17/07 18:39:15 (13 months ago)
Author:
mootoh
Message:

lang/objective-c/SafariHatenaBookmark: trying to fetch bookmarks in JSON format with JSON.framework, but failed to parse response...

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • lang/objective-c/SafariHatenaBookmark/SHBController.m

    r3208 r3211  
    77// 
    88 
     9#import <WebKit/WebKit.h> 
     10#import <JSON/JSON.h>  
    911#import "SHBController.h" 
    1012 
     13@implementation SHBController 
    1114 
    12 @implementation SHBController 
    13 - (void) awakeFromNib 
    14 { 
    15  //   [self reloadUserScripts: nil]; 
     15- (void) awakeFromNib { 
     16  NSLog(@"SHBController:awakeFromNib"); 
    1617 
    17         NSLog(@"SHBController:awakeFromNib"); 
    18     // Menu 
    19     NSMenuItem* item; 
     18  // Menu 
     19  NSMenuItem* item; 
    2020 
    21     item = [[NSMenuItem alloc] init]; 
    22     [item setSubmenu: topMenu]; 
     21  item = [[NSMenuItem alloc] init]; 
     22  [item setSubmenu: topMenu]; 
    2323 
    24     [topMenu setTitle: @"はてな"]; 
     24  [topMenu setTitle: @"はてな"]; 
    2525 
    26     [[NSApp mainMenu] addItem: item]; 
    27     [item release]; 
     26  [[NSApp mainMenu] addItem: item]; 
     27  [item release]; 
     28 
     29  // Notification 
     30  NSNotificationCenter* center = [NSNotificationCenter defaultCenter]; 
     31  [center addObserver: self 
     32             selector: @selector(progressStarted:) 
     33                 name: WebViewProgressStartedNotification 
     34               object: nil]; 
     35  [center addObserver: self 
     36             selector: @selector(progressFinished:) 
     37                 name: WebViewProgressFinishedNotification 
     38               object: nil]; 
     39 
     40#if 0 
     41  [center addObserver: self 
     42             selector: @selector(applicationDidFinishLaunching:) 
     43                 name: NSApplicationDidFinishLaunchingNotification 
     44               object: NSApp]; 
     45#endif // 0 
    2846} 
    2947 
     48- (void) progressStarted: (NSNotification*) n 
     49{ 
     50    WebView* webView = [n object]; 
     51    WebDataSource* source = [[webView mainFrame] provisionalDataSource]; 
     52    if (! source) { 
     53        // source = [[webView mainFrame] provisionalDataSource]; 
     54    } 
     55    NSURL* cur_url = [[source request] URL]; 
     56 
     57    NSLog(@"progressStarted: cur_url = %@", cur_url); 
     58 
     59  // construct request 
     60  NSString *urlString = [NSString stringWithFormat: 
     61    @"http://b.hatena.ne.jp/entry/json/%@", cur_url]; 
     62 
     63  NSLog(@"progressStarted: url = %@", urlString);  
     64  NSURL *url = [NSURL URLWithString:urlString]; 
     65  NSMutableURLRequest *urlRequest = [[NSMutableURLRequest alloc] initWithURL:url]; 
     66  //[urlRequest setHTTPMethod:@"POST"]; 
     67  //[urlRequest setHTTPBody:[content dataUsingEncoding:NSUTF8StringEncoding]]; 
     68 
     69  // connect it 
     70  NSURLConnection *theConnection = [NSURLConnection 
     71    connectionWithRequest:urlRequest 
     72    delegate:self]; 
     73  if (theConnection) { 
     74    receivedData = [[NSMutableData data] retain]; 
     75  } else { 
     76    NSLog(@"not connected correctly."); 
     77  } 
     78} 
     79 
     80- (void) progressFinished: (NSNotification*) n 
     81{ 
     82    WebView* webView = [n object]; 
     83    //NSURL* url = WebFrameRequestURL([webView mainFrame]); 
     84    WebDataSource* source = [[webView mainFrame] provisionalDataSource]; 
     85    if (! source) { 
     86        // source = [[webView mainFrame] provisionalDataSource]; 
     87    } 
     88    NSURL* url = [[source request] URL]; 
     89    NSLog(@"progressFinished: url = %@", url); 
     90} 
     91 
     92 
    3093- (id) init { 
    31     NSLog(@"SHBController %p - init", self); 
     94  NSLog(@"SHBController %p - init", self); 
    3295 
    33     self = [super init]; 
    34     if (! self) 
    35         return nil; 
     96  self = [super init]; 
     97  if (! self) 
     98    return nil; 
    3699 
    37     [NSBundle loadNibNamed: @"Menu.nib" owner: self]; 
    38         NSLog(@"SHBController:init: nib loaded"); 
    39     return self; 
     100  [NSBundle loadNibNamed: @"Menu.nib" owner: self]; 
     101  NSLog(@"SHBController:init: nib loaded"); 
     102  return self; 
    40103} 
    41104 
    42105- (IBAction)bookmark:(id)sender { 
    43  NSLog(@"bookmark");    
     106  NSLog(@"bookmark");    
    44107} 
    45108 
    46109- (IBAction)diary:(id)sender { 
    47  NSLog(@"diary");     
     110  NSLog(@"diary");     
    48111} 
    49112 
    50113- (IBAction)haiku:(id)sender { 
    51  NSLog(@"haiku");     
     114  NSLog(@"haiku");     
    52115} 
    53116 
     117 
     118// callbacks 
     119- (void) connection : (NSURLConnection *) connection 
     120         didReceiveResponse : (NSURLResponse *) response { 
     121  NSDictionary *dicHead = [(NSHTTPURLResponse *)response allHeaderFields]; 
     122  NSLog(@"didReceiveResponse : %@", [dicHead objectForKey:@"Status"]); 
     123  [receivedData setLength:0]; 
     124} 
     125 
     126- (void) connection : (NSURLConnection *) connection 
     127         didReceiveData : (NSData *) data { 
     128  [receivedData appendData:data]; 
     129} 
     130 
     131 - (void) connection : (NSURLConnection *) connection  
     132        didFailWithError : (NSError *) error { 
     133        NSLog(@"didFailWithError 1"); 
     134        // [connection release]; 
     135        [receivedData release];  
     136        NSLog(@"didFailWithError 2"); 
     137} 
     138 
     139- (void) connectionDidFinishLoading:(NSURLConnection *)connection { 
     140        NSLog(@"connectionDidFinishLoading: succeeded to load %d bytes", [receivedData length]); 
     141  NSString *result = [[NSString alloc] initWithData:receivedData encoding:NSUTF8StringEncoding]; 
     142  NSLog(@"connectionDidFinishLoading: result=%@", result); 
     143  id json = [result JSONValue]; 
     144  NSLog(@"connectionDidFinishLoading: id=%@", json); 
     145   
     146//      [connection release]; 
     147        [receivedData release]; 
     148} 
     149 
     150 
    54151@end