Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

如何发一条广播,既有文字又有图片?冰天雪地跪求答案。。 #19

Open
KittenYang opened this issue Jan 25, 2015 · 2 comments

Comments

@KittenYang
Copy link

是使用:

  • (DOUHttpRequest *)post:(DOUQuery *)query
    postBody:(NSString *)body
    callback:(DOUReqBlock)block;

还是在

  • (id)initWithSubPath:(NSString *)aSubPath parameters:(NSDictionary *)theParameters;
    使用中的参数?

求回答!

@wuzhou
Copy link

wuzhou commented Jan 26, 2015

这个我具体没试过, 能传多个参数应该就行 http://developers.douban.com/wiki/?title=shuo_v2

@KittenYang
Copy link
Author

于是我自己用iOS原生的方法完成了即可以发图片也可以发文字的功能,现贴出来供以后有相同需求找到这里的朋友一个参考。

NSString *charset = (NSString *)CFStringConvertEncodingToIANACharSetName(CFStringConvertNSStringEncodingToEncoding(NSUTF8StringEncoding));
    CFUUIDRef uuid = CFUUIDCreate(nil);
    NSString *uuidString = (NSString*)CFBridgingRelease(CFUUIDCreateString(nil, uuid));
    CFRelease(uuid);
    NSString *stringBoundary = [NSString stringWithFormat:@"0xKhTmLbOuNdArY-%@",uuidString];
    NSString *endItemBoundary = [NSString stringWithFormat:@"\r\n--%@\r\n",stringBoundary];
    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:@"https://api.douban.com/shuo/v2/statuses/"]];
    [request setHTTPMethod:@"POST"];
    [request setValue:[NSString stringWithFormat:@"multipart/form-data; charset=%@; boundary=%@", charset, stringBoundary] forHTTPHeaderField:@"Content-Type"];


    NSString *bearerToken = [NSString stringWithFormat:@"Bearer %@",     [[[NSUserDefaults standardUserDefaults] objectForKey:@"DoubanAuthData"] objectForKey:@"accessToken"]];

    NSLog(@"dbAccessToken:%@",[[[NSUserDefaults standardUserDefaults] objectForKey:@"DoubanAuthData"] objectForKey:@"accessToken"]);

    [request setValue:bearerToken forHTTPHeaderField:@"Authorization"];

    //  -- 对要POST的数据进行编码并设置HTTP请求的BODY --
    // 开头的boundary:
    // --KhTmLbOuNdArY
    NSMutableData *postData = [[NSMutableData alloc] init];
    [postData appendData:[[NSString stringWithFormat:@"--%@\r\n",stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]];

    // source参数对应的key和内容:
    // Content-disposition: form-data; name="source"
    //
    // AppKey对应的内容
    NSString *kSource = [NSString stringWithFormat:@"Content-Disposition: form-data; name=\"source\"\r\n\r\n"];
    [postData appendData:[kSource dataUsingEncoding:NSUTF8StringEncoding]];
    NSString *vSource = [NSString stringWithFormat:@"source=%@", kDBAppKey];
    [postData appendData:[vSource dataUsingEncoding:NSUTF8StringEncoding]];

    // 分割字段内容的boundary:
    // --KhTmLbOuNdArY
    [postData appendData:[endItemBoundary dataUsingEncoding:NSUTF8StringEncoding]];

    // text参数对应的key和内容:
    // Content-disposition: form-data; name="text"
    //
    // 要发送的广播内容
    NSString *kText = [NSString stringWithFormat:@"Content-Disposition: form-data; name=\"text\"\r\n\r\n"];
    [postData appendData:[kText dataUsingEncoding:NSUTF8StringEncoding]];
    NSString *vText = self.textView.text;
    [postData appendData:[vText dataUsingEncoding:NSUTF8StringEncoding]];

    // 分割字段内容的boundary:
    // --KhTmLbOuNdArY
    [postData appendData:[endItemBoundary dataUsingEncoding:NSUTF8StringEncoding]];

    // image参数对应的key和内容:
    // content-disposition: form-data; name="image"; filename="WW.jpg"
    // Content-Type: image/png
    //
    // ... contents of 图片 ...
    NSString *kImage = [NSString stringWithFormat:@"Content-Disposition: form-data; name=\"image\"; filename=\"recipe.jpg\"\r\n"];
    [postData appendData:[kImage dataUsingEncoding:NSUTF8StringEncoding]];
    NSString *tImage = [NSString stringWithFormat:@"Content-Type: %@\r\n\r\n", @"image/jpeg"];
    [postData appendData:[tImage dataUsingEncoding:NSUTF8StringEncoding]];
    UIImage *pickedImage = self.imgView.image;
    NSData *imageData = UIImageJPEGRepresentation(pickedImage, 0.0); // jpg格式,1.0表示最大压缩,0.0表示图像无压缩
    [postData appendData:imageData];

    // 结尾的boundary
    // --KhTmLbOuNdArY--
    [postData appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]];


    //  -- 设置HTTP的BODY和请求头中的Content-Length --
    NSString *length = [NSString stringWithFormat:@"%lu", (unsigned long)[postData length]];
    [request setValue:length forHTTPHeaderField:@"Content-Length"];
    [request setHTTPBody:postData];
//     NSLog(@"sendBody:%@",postData);

    NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
    if (connection) {
        [connection start];
    }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants