2011-10-13 41 views
0

我想張貼的圖片和視頻,通過我的應用程序使用yFrog到Twitter,但似乎沒有發生,甚至當我做出的請求......任何人都可以看到我在做什麼錯或點我在正確的方向?謝謝發佈至yFrog objc

-(IBAction)yFrogToTwitter 
{ 

// create the URL 

//used to render bigger images videos 
//NSURL *postURL = [NSURL URLWithString:@"http://render.imageshack.us/renderapi/start"]; 
//below is used to directly upload to twitter 
NSURL *postURL = [NSURL URLWithString:@"http://yfrog.com/api/uploadAndPost"]; 


// create the connection 
NSMutableURLRequest *postRequest = [NSMutableURLRequest requestWithURL:postURL 

cachePolicy:NSURLRequestUseProtocolCachePolicy 
                 timeoutInterval:30.0]; 

// change type to POST (default is GET) 
[postRequest setHTTPMethod:@"POST"]; 



// create data 
NSMutableData *postBody = [NSMutableData data]; 

//NSString *media = PickedImage.image; 
NSString *username = twitterEngine.username; 
NSString *password = twitterEngine.password; 
NSString *message = TweetBody.text; 
NSString *source = @"ThemeCatcher"; 
NSString *api_key= kYFrogKey; 

// just some random text that will never occur in the body 
NSString *stringBoundary = @"0xKhTmLbOuNdArY---This_Is_ThE_BoUnDaRyy---pqo"; 
// header value 
NSString *headerBoundary = [NSString stringWithFormat:@"multipart/form-data; 
boundary=%@",stringBoundary]; 
// set header 
[postRequest addValue:headerBoundary forHTTPHeaderField:@"Content-Type"]; 

// username part 
[postBody appendData:[[NSString stringWithFormat:@"--%@\r\n", stringBoundary] 
dataUsingEncoding:NSUTF8StringEncoding]]; 
[postBody appendData:[[NSString stringWithString:@"Content-Disposition: form-data; 
name=\"username\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]]; 
[postBody appendData:[username dataUsingEncoding:NSUTF8StringEncoding]]; 
[postBody appendData:[@"\r\n" dataUsingEncoding:NSUTF8StringEncoding]]; 

// password part 
[postBody appendData:[[NSString stringWithFormat:@"--%@\r\n", stringBoundary] 
dataUsingEncoding:NSUTF8StringEncoding]]; 
[postBody appendData:[[NSString stringWithString:@"Content-Disposition: form-data; 
name=\"password\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]]; 
[postBody appendData:[password dataUsingEncoding:NSUTF8StringEncoding]]; 
[postBody appendData:[@"\r\n" dataUsingEncoding:NSUTF8StringEncoding]]; 


// api_key 
[postBody appendData:[[NSString stringWithFormat:@"--%@\r\n", stringBoundary] 
dataUsingEncoding:NSUTF8StringEncoding]]; 
[postBody appendData:[[NSString stringWithString:@"Content-Disposition: form-data; 
name=\"key\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]]; 
[postBody appendData:[api_key dataUsingEncoding:NSUTF8StringEncoding]]; 
[postBody appendData:[@"\r\n" dataUsingEncoding:NSUTF8StringEncoding]]; 


// message part 
[postBody appendData:[[NSString stringWithFormat:@"--%@\r\n", stringBoundary] 
dataUsingEncoding:NSUTF8StringEncoding]]; 
[postBody appendData:[[NSString stringWithString:@"Content-Disposition: form-data; 
name=\"message\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]]; 
[postBody appendData:[message dataUsingEncoding:NSUTF8StringEncoding]]; 
[postBody appendData:[@"\r\n" dataUsingEncoding:NSUTF8StringEncoding]]; 


// source part 
[postBody appendData:[[NSString stringWithFormat:@"--%@\r\n", stringBoundary] 
dataUsingEncoding:NSUTF8StringEncoding]]; 
[postBody appendData:[[NSString stringWithString:@"Content-Disposition: form-data; 
name=\"source\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]]; 
[postBody appendData:[source dataUsingEncoding:NSUTF8StringEncoding]]; 
[postBody appendData:[@"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];  


// media part 
[postBody appendData:[[NSString stringWithFormat:@"--%@\r\n", stringBoundary] 
dataUsingEncoding:NSUTF8StringEncoding]]; 
[postBody appendData:[@"Content-Disposition: form-data; name=\"media\"; 
filename=\"fish.jpg\"\r\n" dataUsingEncoding:NSUTF8StringEncoding]]; 
[postBody appendData:[@"Content-Type: image/jpeg\r\n" 
dataUsingEncoding:NSUTF8StringEncoding]]; 
[postBody appendData:[@"Content-Transfer-Encoding: binary\r\n\r\n" 
dataUsingEncoding:NSUTF8StringEncoding]]; 


NSData *imageData = UIImagePNGRepresentation(PickedImage.image); 


// add Image to body 
[postBody appendData:imageData]; 
[postBody appendData:[@"\r\n" dataUsingEncoding:NSUTF8StringEncoding]]; 

// final boundary 
[postBody appendData:[[NSString stringWithFormat:@"--%@\r\n", stringBoundary] 
dataUsingEncoding:NSUTF8StringEncoding]]; 


// add body to post 
[postRequest setHTTPBody:postBody]; 

// pointers to some necessary objects 
//NSURLResponse* response; 
//NSError* error; 

[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES]; 

NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:postRequest 
delegate:self]; 

if(theConnection) 
{ 
    webData = [[NSMutableData data] retain]; 
} 
else 
{ 
    NSLog(@"theConnection is NULL"); 
} 


} 
+1

如果答案是錯誤的或者部分正確的,請將您的意見,用戶誰回答你的問題,使他們能夠改善自己的答案。謝謝。 –

+0

謝謝。我會盡力在這裏做我的一部分,因爲我愛#1,並希望我做什麼或沒有通過這個冒犯任何人。我只是在做我認爲正確的事情,但是完全得到你們兩位的支持,並且將在未來的所有問題中牢記這一點。 – FreeAppl3

+0

當然沒有 - 大家的幫助你,因此SO。 THX – bryanmac

回答

1

您不打算委託回調方法(至少在上面的代碼中),我看你自己設定的委託,但我看不到回調。另外,還要確保你正在檢查的錯誤回調

這裏有您需要實現委託回調方法:

http://developer.apple.com/library/mac/#documentation/Foundation/Reference/NSURLConnectionDelegate_Protocol/Reference/Reference.html#//apple_ref/occ/intf/NSURLConnectionDelegate

didFailWithError是如果你有問題的大單。確保你閱讀了所有的NSError數據並至少記錄下它。

例如:

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

- (void) connectionDidFinishLoading:(NSURLConnection *)connection { 
    [connection release]; 

    NSString* responseString = [[NSString alloc] initWithData:responseData  encoding:NSUTF8StringEncoding]; 
    NSLog(@"result: %@", responseString); 

    [responseString release]; 
} 

- (void) connection:(NSURLConnection *)connection didFailWithError:(NSError *)error { 
     NSLog(@"error - read error object for details"); 
} 
+0

你總是似乎知道如何幫助我!非常感謝!!!它現在給了我一個請求,我想我必須抓住它並將網址放在一個主體中,然後張貼到Twitter上。謝謝你,我會花費數天時間試圖弄清楚這一點。 – FreeAppl3

+0

我站好了。如果您查看編輯後的代碼,我將顯示用於yfrog的不同xml標註。只是以爲我會發布,如果有人在這尋找答案絆倒。 – FreeAppl3

0

我XMLParser的下面

// 
// yFrogParser.h 
// PreviewMaker 
// 
// Created by Anthony Cornell on 4/12/12. 
// Copyright (c) 2012 iDevice Designs. All rights reserved. 
// 

#import <Foundation/Foundation.h> 
#import "yFrogObject.h" 

@interface yFrogParser : NSObject<NSXMLParserDelegate>{ 

NSMutableData *recivedData; 
NSMutableArray *imageInfo; 
NSMutableString *currentNodeContent; 
NSXMLParser  *parser; 

yFrogObject *imageObjects; 
NSMutableArray *imageArray; 

} 

@property (readonly, retain) NSMutableArray *imageInfo; 
@property (nonatomic, retain) NSMutableArray *imageArray; 
@property (nonatomic, retain) NSMutableData *recivedData; 

-(id) loadXMLByData:(NSMutableData *)data; 

@end 



// 
// yFrogParser.m 
// PreviewMaker 
// 
// Created by Anthony Cornell on 4/12/12. 
// Copyright (c) 2012 iDevice Designs. All rights reserved. 
// 

#import "yFrogParser.h" 

@implementation yFrogParser 

@synthesize imageInfo,imageArray,recivedData; 

-(id) loadXMLByData:(NSMutableData *)data{ 


imageArray   = [[NSMutableArray alloc] init]; 
recivedData = [[NSMutableData alloc]initWithData:data]; 
parser   = [[NSXMLParser alloc] initWithData:recivedData]; 
parser.delegate = self; 
[parser parse]; 

return self;  

} 
- (void) parser:(NSXMLParser *)parser didStartElement:(NSString *)elementname 
namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName 
attributes:(NSDictionary *)attributeDict 
{ 


if ([elementname isEqualToString:@"links"]) 
{ 
    imageObjects = [yFrogObject alloc]; 

} 
} 

- (void) parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName  
namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName 
{ 

if ([elementName isEqualToString:@"image_link"]) { 

    imageObjects.responceURL = currentNodeContent; 
} 

if ([elementName isEqualToString:@"image_bb"]) { 

    imageObjects.forumUrl = currentNodeContent; 
    // NSLog(@"image forum url %@",currentNodeContent); 
}  


if ([elementName isEqualToString:@"links"]) 
{ 
    [imageArray addObject:imageObjects]; 
    [imageObjects release]; 
    imageObjects = nil; 
    [currentNodeContent release]; 
    currentNodeContent = nil; 

} 
} 



- (void) parser:(NSXMLParser *)parser foundCharacters:(NSString *)string 
{ 
currentNodeContent = (NSMutableString *) [string  
              stringByTrimmingCharactersInSet:[NSCharacterSet  
whitespaceAndNewlineCharacterSet]]; 
} 


@end 

在您的委託做到這一點自然

-(void) connectionDidFinishLoading:(NSURLConnection *)connection { 
[connection release]; 


parser = [[yFrogParser alloc]init]; 
[parser loadXMLByData:webData]; 

yFrogObject *object = [[parser imageArray]objectAtIndex:0]; 



NSString* responseString = [[NSString alloc] initWithData:webData 
encoding:NSUTF8StringEncoding]; 

//NSLog(@"Parser result: %@", [object responceURL]); 
//NSLog(@"yFrog responce results: %@", responseString); 

[responseString release]; 

} 

在分享心情的東西!希望這有助於