Fabric android sdk upload video to twitter failure

android
video

#1

I’m now trying to upload the video from android by using Fabric SDK, but seems always failed.
however it’s able to upload the image.

This is my code:
public boolean shareVideoToTwitter(String videoUrl, final String strDesc) {
if (videoUrl == null || strDesc == null)
return false;

	TwitterSession session = Twitter.getSessionManager().getActiveSession();
	if (session == null)
		return false;
	
	if (Twitter.getApiClient(session) == null) {
		return false;
	}
	
	MediaService twitterMediaService = Twitter.getApiClient(session).getMediaService();
	
	if (twitterMediaService == null)
		return false;

	//videoUrl : /storage/emulated/0/Android/data/com.example.helloworld/cache//1444817802300.mp4
	File video = new File(videoUrl);
	
	TypedFile typedFile = new TypedFile("application/octet-stream", video);
	//TypedFile typedFile = new TypedFile("video/mp4", video);
	
	twitterMediaService.upload(typedFile, null, null, new Callback<Media>() {

		@Override
		public void failure(TwitterException arg0) {
			Log.d(TAG, "twitter upload image/video failure");
			arg0.printStackTrace();
		}

		@Override
		public void success(Result<Media> mediaResult) {
			Log.d(TAG, "twitter upload image/video success");
			StatusesService statusesService = TwitterCore.getInstance().getApiClient().getStatusesService();
			statusesService.update(strDesc, null, false, null, null, null, true, false, mediaResult.data.mediaIdString, new Callback<Tweet>() {
				@Override
				public void success(Result<Tweet> tweetResult) {
					Log.d(TAG, "twitter update status success");
				}

				@Override
				public void failure(TwitterException e) {
					Log.d(TAG, "twitter update status failure");
				}
			});
		}
	});

	return true;
}

this is the error log.

I/libmdmdetect( 331): ESOC framework not supported
E/Twitter ( 4412): Unexpected response: {“request”:"/1.1/media/upload.json",“error”:“media type unrecognized.”}
E/Twitter ( 4412): java.lang.NullPointerException: Attempt to get length of null array
E/Twitter ( 4412): at com.twitter.sdk.android.core.Callback.failure(Callback.java:45)
E/Twitter ( 4412): at retrofit.CallbackRunnable$2.run(CallbackRunnable.java:53)
E/Twitter ( 4412): at android.os.Handler.handleCallback(Handler.java:739)
E/Twitter ( 4412): at android.os.Handler.dispatchMessage(Handler.java:95)
E/Twitter ( 4412): at android.os.Looper.loop(Looper.java:211)
E/Twitter ( 4412): at android.app.ActivityThread.main(ActivityThread.java:5317)
E/Twitter ( 4412): at java.lang.reflect.Method.invoke(Native Method)
E/Twitter ( 4412): at java.lang.reflect.Method.invoke(Method.java:372)
E/Twitter ( 4412): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1016)
E/Twitter ( 4412): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:811)
D/example_helloword( 4412): twitter upload image/video failure
W/System.err( 4412): com.twitter.sdk.android.core.TwitterApiException: 400 Bad Request
W/System.err( 4412): at retrofit.RestAdapter$RestHandler.invokeRequest(RestAdapter.java:383)
W/System.err( 4412): at retrofit.RestAdapter$RestHandler.access$100(RestAdapter.java:220)
W/System.err( 4412): at retrofit.RestAdapter$RestHandler$2.obtainResponse(RestAdapter.java:278)
W/System.err( 4412): at retrofit.CallbackRunnable.run(CallbackRunnable.java:42)
W/System.err( 4412): at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:422)
W/System.err( 4412): at java.util.concurrent.FutureTask.run(FutureTask.java:237)
W/System.err( 4412): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
W/System.err( 4412): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
W/System.err( 4412): at java.lang.Thread.run(Thread.java:818)

any idea why failure here?


#2

The base url for the APiClient is api.twitter.com. Video uses upload.twitter.com.


#3

Then which API in Fabric Android SDK should i use to upload the video? or it doesn’t support upload video from Fabric Android SDK? Thanks.

Edit:
I found this post

Seems that currently no easy way in Fabric Android SDK to upload video, my question is that any plan change to
TwitterApiClient class to support upload video in future in official Fabric Android SDK?


#4

Hi all, trying to upload a video on android, extending fabrics TwitterApiClient.
INIT : success returns media id
APPEND : success 2XX
FINALIZE : com.twitter.sdk.android.core.TwitterApiException: 400 Bad Request.

any help is much appreciated.

@foobarhk might find this helpful. First create a class that extends the TwitterApiClient and change the endpoint to the upload endpoint. Then you can make those three calls.

public class TwitterVideoApiClient extends TwitterApiClient{

private static final String TWITTER_UPLOAD_URL = "https://upload.twitter.com";
private  RestAdapter twitterVideoApiClientAdapter;

public TwitterVideoApiClient(TwitterSession session) {

    super(session);

    final Gson gson = new GsonBuilder()
            .registerTypeAdapterFactory(new SafeListAdapter())
            .registerTypeAdapterFactory(new SafeMapAdapter())
            .create();

    SSLSocketFactory sslSocketFactory = TwitterCore.getInstance().getSSLSocketFactory();
    TwitterAuthConfig authConfig = TwitterCore.getInstance().getAuthConfig();
    ExecutorService executorService = TwitterCore.getInstance().getFabric()
            .getExecutorService();

    twitterVideoApiClientAdapter = new RestAdapter.Builder()
            .setClient(new AuthenticatedClient(authConfig, session, sslSocketFactory))
            .setEndpoint(TWITTER_UPLOAD_URL)
            .setConverter(new GsonConverter(gson))
            .setExecutors(executorService, new MainThreadExecutor())
            .build();
}

public TwitterUploadVideoInitService videoUploadInitService() {
    return twitterVideoApiClientAdapter.create(TwitterUploadVideoInitService.class);
}

public TwitterUploadVideoAppendService videoUploadAppendService() {
    return twitterVideoApiClientAdapter.create(TwitterUploadVideoAppendService.class);
}

public TwitterUploadVideoFinalizeService videoUploadFinalizeService() {
    return twitterVideoApiClientAdapter.create(TwitterUploadVideoFinalizeService.class);
}

public interface TwitterUploadVideoInitService {
    @FormUrlEncoded()
    @POST("/1.1/media/upload.json")
    void initialize(@Field("command") String command,
                    @Field("media_type") String mediaType,
                    @Field("total_bytes") String totalBytes,
                    Callback<Media> callback);
}

public interface TwitterUploadVideoAppendService {
    @Multipart
    @POST("/1.1/media/upload.json")
    void append(@Part("command") String command,
                @Part("media_id") String mediaId,
                @Part("segment_index") int segmentIndex,
                @Part("media") TypedFile media,
                Callback <Twitter>  callback);
}

public interface TwitterUploadVideoFinalizeService {
    @FormUrlEncoded()
    @POST("/1.1/media/upload.json")
    void finalize(@Field("command") String command,
                  @Field("media_id") String mediaId,
                  Callback<Twitter> callback);
}

#5