安卓:添加图像鸣叫使用面料,Twitter的REST API和改造 [英] Android: adding image to tweet using Fabric, Twitter REST API and Retrofit

查看:226
本文介绍了安卓:添加图像鸣叫使用面料,Twitter的REST API和改造的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我工作的一个应用程序中,用户可以发表某些东西到Twitter,也可以拍摄照片添加到鸣叫。我是新来的面料和改造,所以我就如何实现图片上传以及如何与的状态服务的更新功能使用上传的图片的一些问题。 (我知道TweetComposer可以轻松地发布与鸣叫图像,但我不希望更多的Twitter的用户界面元素,我也希望能够设置的鸣叫编程,这是不可能的TweetComposer相关联的位置)。

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. 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. (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).

到目前为止我所了解,为了以包括使用状态更新服务发布方式鸣叫的图片,我们需要先上传图片本身到Twitter。我发现,通过REST API上传图片做是通过使用这个网址:

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. I have found that uploading images through the REST API is done by using this URL:

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

我还发现,它有可能以自定义面料提供Twitter的API客户端来访问服务端点。

I have also found that it is possible to customise the Twitter API client provided in Fabric to access service endpoints.

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

不过,我很高兴能得到一些更多的解释。首先,我怎么更改API客户端域(以upload.twitter.com),我应该在回调期望是什么类型的?

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?

此外,如果图像上传成功,我设法得到它的ID,我怎么可以用它来此媒体对象附加到通过的状态服务的更新功能创建的鸣叫?我还没有找到文档中任何附加的实体。

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? 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!

推荐答案

最后,在Twitter的工具包支持最新版本增加了对媒体上传和与介质ID啁啾(使用StatusesService)。

Finally, in the latest version of Twitter Kit support has been added for media upload and for tweeting with media IDs (using the StatusesService).

我已经能够使用StatusesService图片鸣叫加入code如下:

I have been able to tweet with picture using StatusesService by adding the code below:

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("@tkl_testi " + 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) {
         //...
    }
});

这篇关于安卓:添加图像鸣叫使用面料,Twitter的REST API和改造的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆