Android:使用Fabric,Twitter REST API和Retrofit为tweet添加图像

Android:使用Fabric,Twitter REST API和Retrofit为tweet添加图像,第1张

概述我正在开发一个应用程序,用户可以将某些内容发布到Twitter,也可以将照片添加到推文中.我是Fabric和Retrofit的新手,所以我对如何实现图像上传以及如何使用Statuses Service的更新功能使用上传的图像有一些疑问. (我知道TweetComposer允许轻松发布带有推文的图像,但我不想要额外的Twitter UI元素,我也希望能够以编程方式设置与推文相关联的位置,这对于Twe 我正在开发一个应用程序,用户可以将某些内容发布到Twitter,也可以将照片添加到推文中.我是Fabric和Retrofit的新手,所以我对如何实现图像上传以及如何使用Statuses Service的更新功能使用上传的图像有一些疑问. (我知道TweetComposer允许轻松发布带有推文的图像,但我不想要额外的Twitter UI元素,我也希望能够以编程方式设置与推文相关联的位置,这对于TweetComposer来说是不可能的).

到目前为止我所理解的是,为了在使用状态服务更新方法发布的推文中包含图像,我们需要首先将图像本身上传到Twitter.我发现通过REST API上传图像是通过使用以下URL完成的:

https://upload.twitter.com/1.1/media/upload.json

我还发现可以自定义Fabric中提供的Twitter API客户端来访问服务端点.

class MyTwitterapiclient extends Twitterapiclient {    public MyTwitterapiclient(TwitterSession session) {        super(session);    }    /**     * ProvIDe CustomService with @R_419_5552@d endpoints     */    public CustomService getCustomService() {        return getService(CustomService.class);    }}// example users/show service endpointinterface CustomService {    @GET("/1.1/users/show.Json")    voID show(@query("user_ID") long ID,Callback<User> cb);}

但是,我很乐意得到更多解释.首先,如何在API客户端中更改域(到upload.twitter.com)以及我应该在回调中期望什么类型?

此外,如果图像上传成功并且我设法获取其ID,我如何使用它将此媒体对象附加到由状态服务的更新功能创建的推文?我没有在文档中找到任何附加实体的方法.

到目前为止我有这个:

public class MyTwitterapiclient extends Twitterapiclient {    public MyTwitterapiclient(TwitterSession session)    {        super(session);    }    public UploadMediaService getUploadMediaService() {        return getService(UploadMediaService.class);    }}interface UploadMediaService {    @Multipart    @POST("1.1/media/upload.Json")    voID upload(@Part("media") Typedfile file,@Part("additional_owners") String owners,Callback cb);}

有没有更有知识的人可以给我一些指导?提前致谢!

解决方法 最后,在最新版本的Twitter Kit中添加了支持媒体上传和使用媒体ID的推文(使用StatusesService).

我已经能够通过添加以下代码使用StatusesService发送图片:

file photo = new file(mCurrentPhotopath);Typedfile typedfile = new Typedfile("application/octet-stream",photo);MediaService ms = twitterclIEnt.getMediaService();ms.upload(typedfile,null,new Callback<Media>() {    @OverrIDe    public voID success(Result<Media> mediaResult) {    StatusesService statusesService = TwitterCore.getInstance().getapiclient(session).getStatusesService();    statusesService.update("@##### " + eventtype + " lähellä paikkaa: " + place.getText().toString() + ";\n" + comment.getText().toString(),false,mypos.latitude,mypos.longitude,true,mediaResult.data.mediaIDString,new Callback<Tweet>() {        @OverrIDe        public voID success(Result<Tweet> tweetResult) {             //...        }        @OverrIDe        public voID failure(TwitterException e) {            //...                              }      });    }    @OverrIDe    public voID failure(TwitterException e) {         //...    }});
总结

以上是内存溢出为你收集整理的Android:使用Fabric,Twitter REST API和Retrofit为tweet添加图像全部内容,希望文章能够帮你解决Android:使用Fabric,Twitter REST API和Retrofit为tweet添加图像所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/1127883.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-30
下一篇2022-05-30

发表评论

登录后才能评论

评论列表(0条)

    保存