Changeset 3211 for lang/objective-c

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

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

Location:
lang/objective-c/SafariHatenaBookmark
Files:
3 modified

Legend:

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

    r3208 r3211  
    1212@interface SHBController : NSObject { 
    1313        IBOutlet NSMenu* topMenu; 
     14  id receivedData; 
    1415} 
    1516 
  • 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 
  • lang/objective-c/SafariHatenaBookmark/SafariHatenaBookmark.xcodeproj/project.pbxproj

    r3208 r3211  
    1212                86A7D1D10D16639600D35D4C /* SHBController.m in Sources */ = {isa = PBXBuildFile; fileRef = 86A7D1CE0D16639600D35D4C /* SHBController.m */; }; 
    1313                86A7D1D20D16639600D35D4C /* PluginLoader.m in Sources */ = {isa = PBXBuildFile; fileRef = 86A7D1D00D16639600D35D4C /* PluginLoader.m */; }; 
     14                86C597AA0D166CC10000340D /* WebKit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 86C597A90D166CC10000340D /* WebKit.framework */; }; 
     15                86C597D00D1673140000340D /* JSON.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 86C597CF0D1673140000340D /* JSON.framework */; }; 
    1416                8D5B49B0048680CD000E48DA /* InfoPlist.strings in Resources */ = {isa = PBXBuildFile; fileRef = 089C167DFE841241C02AAC07 /* InfoPlist.strings */; }; 
    1517                8D5B49B4048680CD000E48DA /* Cocoa.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 1058C7ADFEA557BF11CA2CBB /* Cocoa.framework */; }; 
     
    2830                86A7D1CF0D16639600D35D4C /* PluginLoader.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = PluginLoader.h; sourceTree = "<group>"; }; 
    2931                86A7D1D00D16639600D35D4C /* PluginLoader.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = PluginLoader.m; sourceTree = "<group>"; }; 
     32                86C597A90D166CC10000340D /* WebKit.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = WebKit.framework; path = /System/Library/Frameworks/WebKit.framework; sourceTree = "<absolute>"; }; 
     33                86C597CF0D1673140000340D /* JSON.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; path = JSON.framework; sourceTree = "<group>"; }; 
    3034                8D5B49B6048680CD000E48DA /* SafariHatenaBookmark.bundle */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = SafariHatenaBookmark.bundle; sourceTree = BUILT_PRODUCTS_DIR; }; 
    3135                8D5B49B7048680CD000E48DA /* Info.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; }; 
     
    3943                        files = ( 
    4044                                8D5B49B4048680CD000E48DA /* Cocoa.framework in Frameworks */, 
     45                                86C597AA0D166CC10000340D /* WebKit.framework in Frameworks */, 
     46                                86C597D00D1673140000340D /* JSON.framework in Frameworks */, 
    4147                        ); 
    4248                        runOnlyForDeploymentPostprocessing = 0; 
     
    8288                                86A7D1CD0D16639600D35D4C /* SHBController.h */, 
    8389                                86A7D1CE0D16639600D35D4C /* SHBController.m */, 
     90                                86C597CF0D1673140000340D /* JSON.framework */, 
     91                                86C597A90D166CC10000340D /* WebKit.framework */, 
    8492                                86A7D1CF0D16639600D35D4C /* PluginLoader.h */, 
    8593                                86A7D1D00D16639600D35D4C /* PluginLoader.m */, 
     
    201209                        buildSettings = { 
    202210                                COPY_PHASE_STRIP = NO; 
     211                                FRAMEWORK_SEARCH_PATHS = ( 
     212                                        "$(inherited)", 
     213                                        "\"$(SRCROOT)\"", 
     214                                ); 
    203215                                GCC_DYNAMIC_NO_PIC = NO; 
    204216                                GCC_ENABLE_FIX_AND_CONTINUE = YES; 
     
    219231                        buildSettings = { 
    220232                                DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym"; 
     233                                FRAMEWORK_SEARCH_PATHS = ( 
     234                                        "$(inherited)", 
     235                                        "\"$(SRCROOT)\"", 
     236                                ); 
    221237                                GCC_MODEL_TUNING = G5; 
    222238                                GCC_PRECOMPILE_PREFIX_HEADER = YES;