我正在尝试使用他们的API将个人资料图片上传到Twitter。我在网上找到了一些关于如何格式化URLRequest的好例子,但是我似乎无法让它工作。任何帮助,将不胜感激。我在Twitter API中使用的网址是http://twitter.com/account/update_profile_image.json
-(void)uploadImageForAccount:(NSURL *)theURL intUserName:(NSString *)_userName initPassWord:(NSString *)_passWord image:(NSData *)image{
userName = [_userName retain];
passWord = [_passWord retain];
UIApplication *app = [UIApplication sharedApplication];
app.networkActivityIndicatorVisible = YES;
requestedData = [[NSMutableData alloc] initWithLength:0];
NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:theURL cachePolicy:NSURLRequestReloadIgnoringLocalCacheData
timeoutInterval:60];
[theRequest setHTTPMethod:@"POST"];
NSString *stringBoundary = [NSString stringWithString:@"0xKhTmLbOuNdArY"];
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@", stringBoundary];
[theRequest addValue:contentType forHTTPHeaderField:@"Content-Type"];
// Credentials
NSString *creds = [NSString stringWithFormat:@"%@:%@", userName, passWord];
[theRequest addValue:[NSString stringWithFormat:@"Basic %@",[self base64encode:creds]] forHTTPHeaderField:@"Authorization"];
NSMutableData *postBody = [NSMutableData data];
[postBody appendData:[[NSString stringWithFormat:@"\r\n\r\n--%@\r\n", stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name=\"source\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithString:@"canary"] dataUsingEncoding:NSUTF8StringEncoding]];
NSString *mimeType = mimeType = @"image/jpeg";
[postBody appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"media\"; filename=\"%@\"\r\n", @"TNImage.jpeg"] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithFormat:@"Content-Type: %@\r\n", mimeType] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithString:@"Content-Transfer-Encoding: binary\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:image];
[postBody appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]];
[theRequest setHTTPBody:postBody];
NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:theRequest
delegate:self
startImmediately:YES];
if(DEBUG) NSLog(@"Connection = %@",[connection description]);
if (connection == nil) {
if(DEBUG) NSLog(@"Connection was Nil");
}
}
答案 0 :(得分:-1)
Json不使用xml而不是