简体   繁体   English

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

[英]Android: adding image to tweet using Fabric, Twitter REST API and Retrofit

I am working on an app in which the user can post certain things to Twitter and can also take a picture to add to a Tweet. 我正在开发一个应用程序,用户可以将某些内容发布到Twitter,也可以将照片添加到推文中。 I am new to Fabric and Retrofit, so I have some questions concerning how to implement the image upload and how to use the uploaded image with the Statuses Service's update function. 我是Fabric和Retrofit的新手,所以我对如何实现图像上传以及如何使用Statuses Service的更新功能使用上传的图像有一些疑问。 (I am aware that TweetComposer allows to post images with tweets easily, but I don't want the additional Twitter UI element and I also want to be able to set the location associated with the tweet programatically, which is not possible with TweetComposer). (我知道TweetComposer允许轻松发布带有推文的图像,但我不想要额外的Twitter UI元素,我也希望能够以编程方式设置与推文相关联的位置,这对于TweetComposer来说是不可能的)。

So far what I have understood that in order to include images in a tweet posted using the Statuses Service update method, we need to upload the image itself first to Twitter. 到目前为止我所理解的是,为了在使用状态服务更新方法发布的推文中包含图像,我们需要首先将图像本身上传到Twitter。 I have found that uploading images through the REST API is done by using this URL: 我发现通过REST API上传图像是通过使用以下URL完成的:

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

I have also found that it is possible to customise the Twitter API client provided in Fabric to access service endpoints. 我还发现可以自定义Fabric中提供的Twitter API客户端来访问服务端点。

class MyTwitterApiClient extends TwitterApiClient {
    public MyTwitterApiClient(TwitterSession session) {
        super(session);
    }

    /**
     * Provide CustomService with defined endpoints
     */
    public CustomService getCustomService() {
        return getService(CustomService.class);
    }
}

// example users/show service endpoint
interface CustomService {
    @GET("/1.1/users/show.json")
    void show(@Query("user_id") long id, Callback<User> cb);
}

However, I would be glad to get some more explanation. 但是,我很乐意得到更多解释。 First of all, how do I change the domain (to upload.twitter.com) in the API client and what type should I expect in the callback? 首先,如何在API客户端中更改域(到upload.twitter.com)以及我应该在回调中期望什么类型?

Also, if the image is uploaded successfully and I manage to get its ID, how can I use it to attach this media object to a tweet created by the Statuses Service's update function? 此外,如果图像上传成功并且我设法获取其ID,我如何使用它将此媒体对象附加到由状态服务的更新功能创建的推文? I haven't found any way in the documentation to attach entities. 我没有在文档中找到任何附加实体的方法。

So far I have this: 到目前为止我有这个:

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);

}

Is there anyone more knowledgeable who could give me some more guidance? 有没有更有知识的人可以给我一些指导? Thanks in advance! 提前致谢!

Finally, in the latest version of Twitter Kit support has been added for media upload and for tweeting with media IDs (using the StatusesService). 最后,在最新版本的Twitter Kit中添加了支持媒体上传和使用媒体ID的推文(使用StatusesService)。

I have been able to tweet with picture using StatusesService by adding the code below: 我已经能够通过添加以下代码使用StatusesService发送图片:

File photo = new File(mCurrentPhotoPath);
TypedFile typedFile = new TypedFile("application/octet-stream", photo);

MediaService ms = twitterclient.getMediaService();

ms.upload(typedFile, null, 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(), null, false, mypos.latitude, mypos.longitude, null, true, false, 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) {
         //...
    }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM