1

我正在做一个 java 应用程序,它将视频上传到 twitvid 并取回视频的链接并将其发布到 twitter。但我找不到任何将视频上传到 twitvid 的好例子。http://twitvid.pbworks.com/w/page/22556295/FrontPage上的文档也令人困惑,没有给出示例。任何人都可以分享一个例子或好的文档吗?这些库位于http://twitvid.pbworks.com/w/page/22556292/Client%20Libraries

4

1 回答 1

1

试试这个:显然,你需要导入必要的库,如果你使用的是 eclipse,它会告诉你需要导入什么。

URL url = new URL("http://im.twitvid.com/api/uploadAndPost");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoOutput(true);
connection.setInstanceFollowRedirects(false);
connection.setRequestMethod("POST");
connection.setRequestProperty("Content-Type", "application/xml");

OutputStream os = connection.getOutputStream();

TransformerFactory tf = TransformerFactory.newInstance();
Transformer transformer = tf.newTransformer();
FileReader fileReader = new FileReader("filename.xml");
StreamSource source = new StreamSource(fileReader);
StreamResult result = new StreamResult(os);
transformer.transform(source, result);

os.flush();
connection.getResponseCode();
connection.disconnect();

其中 filename.xml 看起来像这样(来自http://twitvid.pbworks.com/w/page/22556308/Twitvid%20API%20Method%3A%20uploadandpost):

 <?xml version='1.0' encoding='UTF-8'?>

     <rsp status='ok'>

           <status_id>1111</status_id>

           <user_id>TwitUsername</user_id>

           <media_id>3GS34</media_id>

           <media_url>http://twitvid.com/3GS34</media_url>

           <message>This is my tweet!</message>

           <geo_latitude>57.64911</geo_latitude>

           <geo_longitude>10.40744</geo_longitude>

      </rsp>

您需要将 .xml 中的值替换为与您相关的值。我链接的 twitvid 页面描述了上述所有字段中的内容。祝你好运,希望有帮助。

编辑:很多这些字段是可选的,例如,您可能不需要 geo_latitude/longitude。该页面应该解释一切。我知道这可能看起来令人困惑,但请尝试使用它。希望上面的代码能解决你的问题。

于 2013-05-08T02:18:01.707 回答