MediaService success to upload, but nothing show in the timeline

restapi
android
fabric
twitterkit
java

#1

Hi, I just learn to integrate the twitter SDK with fabric. I’ve problem when I want to post photo to my timeline, the problem is when I post that photo with this code below the image not show in my timeline? But when I try to posting text with StatusServie it works. Could you help me? Thanks before.

TwitterApiClient twitterApiClient = TwitterCore.getInstance().getApiClient(twitterSession);
MediaService ms = twitterApiClient.getMediaService();
RequestBody rb = RequestBody.create(MediaType.parse("image/*"), result);

Call<Media> mediaCall = ms.upload(rb,null,null);
mediaCall.enqueue(new Callback<Media>() {
	@Override
	public void success(Result<Media> result) {
		Log.e("twitter","success:"+result.data.mediaIdString+"\n"+result.response.raw());
		Toast.makeText(context,"success",0).show();

	}

	@Override
	public void failure(TwitterException exception) {
		Log.e("twitter","failed:"+exception.getCause()+"\n"+exception.getMessage());
		Toast.makeText(context,"failed",0).show();
	}
});

#2

Hey there, great question!

The MediaService helps you connect to the media/upload REST endpoint (see docs here: https://dev.twitter.com/rest/media). The expected behavior there is that you’ll get a success response with a media_id. It stores the photo on our side, but doesn’t actually create a Tweet.

To do that, you would post to the statuses/update endpoint, passing in the media_id. That’s why using the StatusService directly worked.


#3

Awesome! I just try your suggestion. Thanks for your help. May you always be blessed.

So this is my code :
File result;
String message;

final TwitterApiClient twitterApiClient = TwitterCore.getInstance().getApiClient(twitterSession);
MediaService ms = twitterApiClient.getMediaService();
RequestBody rb = RequestBody.create(MediaType.parse(“image/*”), result);

Call<Media> mediaCall = ms.upload(rb,null,null);
mediaCall.enqueue(new Callback<Media>() {
	@Override
	public void success(Result<Media> result) {

		StatusesService statusesService = twitterApiClient.getStatusesService();
		Call<Tweet> call = statusesService.update(message,null,false,null,null,null,true,false,result.data.mediaIdString);
		call.enqueue(new Callback<Tweet>() {
			@Override
			public void success(Result<Tweet> result) {
				//Do something with result
				Log.e("twitter","success:"+result.response.raw());
				Toast.makeText(context,"success:"+result.toString(),0).show();
			}

			public void failure(TwitterException exception) {
				//Do something on failure
				Log.e("twitter","failed:"+exception.getCause()+"\n"+exception.getMessage());
				Toast.makeText(context,"failed:"+exception.toString(),0).show();
			}
		});

	}

	@Override
	public void failure(TwitterException exception) {
		Log.e("twitter","failed:"+exception.getCause()+"\n"+exception.getMessage());
		Toast.makeText(context,"failed",0).show();
	}
});