vimeo / vimeo-networking-java

The Vimeo Java (Android) SDK
MIT License
121 stars 49 forks source link

Use JsonReader.setLenient(true) to accept malformed JSON at line 1 column 1 path $ #416

Open mohammed-ezzedine opened 4 years ago

mohammed-ezzedine commented 4 years ago

Issue Summary

Getting the error:

Use JsonReader.setLenient(true) to accept malformed JSON at line 1 column 1 path $

Here's my code:

public void onCreate(Bundle savedInstanceState) {
        Log.i(TAG, "onCreate");
        super.onCreate(savedInstanceState);
        final Video video = VideoPlayActivity.video;

        TestApp d = new TestApp();
        d.onCreate();
        mApiClient = d.getmApiClient();

        String uri = video.getVideoUri().toString();
        mApiClient.getInstance().fetchNetworkContent(uri, new ModelCallback<Video>(Video.class) {
            @Override
            public void success(Video video) {
                Log.i(TAG, "get success.");
                // use the video
            }

            @Override
            public void failure(VimeoError error) {
                Log.i(TAG, error.getErrorMessage());
                // voice the error
            }
        });
}

And these are the important methods in TestApp:

public class TestApp extends Application {
@Override
    public void onCreate() {
        super.onCreate();
        Log.i(TAG, "onCreate");

        sContext = this;

        Configuration.Builder configBuilder = getClientIdAndClientSecretBuilder();
        VimeoClient.initialize(configBuilder.build());
        mApiClient = VimeoClient.getInstance();
    }

    public Configuration.Builder getClientIdAndClientSecretBuilder() {
        String clientId = "dc62dc72e8c7cf4d0b7bb025d41e2a7649ddc0a4";
        String clientSecret = "SRbzt7mLfbA0l5aEV8lXINE54nRGkSp8S2i+wOp9paOqQsEKkwC5MtdwFPHiVqX7DTCooPVWAxPVjG8/pjjPbl+7LNTXTuSDit1oZGtCjkBYFqahb95J4pAHzEumdNhs";
        return new Configuration.Builder(clientId, clientSecret, SCOPE);
    }
}
rvanderlinden commented 4 years ago

I'm experiencing the same issue. Did you manage to resolve this issue @mezdn?

Code:

fun configureVimeoClient(context: Context) {
    val configBuilder: Configuration.Builder = Configuration.Builder("MY_ACCESS_TOKEN")
        .setCacheDirectory(context.cacheDir)

    if(isDebug) {
        configBuilder.enableCertPinning(false)
        configBuilder.setLogLevel(Vimeo.LogLevel.VERBOSE)
    }

    VimeoClient.initialize(configBuilder.build())
} 
val vimeoUrl = "https://vimeo.com/foo/bar"
VimeoClient.getInstance().fetchNetworkContent(vimeoUrl, object : ModelCallback<Video>(Video::class.java) {
                override fun success(video: Video?) {
                    var foundVideoUrl = false
                    video?.files?.forEach { videoFile ->
                        val link = videoFile.getLink()
                        if(!foundVideoUrl && link != null) {
                            foundVideoUrl = true
                            Timber.d("Video url: $link")
                        }
                    }

                    if(!foundVideoUrl) {
                        Timber.d("Video url not found.")
                    }
                }

                override fun failure(error: VimeoError?) {
                        Timber.d("Vimeo fetch error ${error?.logString}.")
                }
            })
rvanderlinden commented 4 years ago

For anyone else encountering this issue: It is most likely a problem with the url or ModelCallback model-object you are providing. The json issue most likely indicates the response does not match the expected model object.

For my case specifically, "https://vimeo.com/foo/bar" is the web-url for a vimeo video. The api to fetch Video data is "https://api.vimeo.com/videos/foo". The base api url ("https://api.vimeo.com/") can be found in Vimeo.VIMEO_BASE_URL_STRING. This means the actual uri to fetch Video data becomes "videos/foo".

The following code is now working for me:

        val scope = "public private video_files"
        val isDebug = true
        val loggingInterceptor = HttpLoggingInterceptor()
        loggingInterceptor.level = if (isDebug) HttpLoggingInterceptor.Level.BODY else HttpLoggingInterceptor.Level.NONE

        val configBuilder: Configuration.Builder = Configuration.Builder("MY_CLIENT_ID", "MY_CLIENT_SECRET", scope)
            .setBaseUrl(Vimeo.VIMEO_BASE_URL_STRING)
            .setAccessToken("MY_ACCESS_TOKEN")
            .setGsonDeserializer(GsonDeserializer())
            .setCacheDirectory(context.cacheDir)
            .addNetworkInterceptor(loggingInterceptor)

        if(isDebug) {
            configBuilder.enableCertPinning(false)
            configBuilder.setLogLevel(Vimeo.LogLevel.VERBOSE)
        }

        VimeoClient.initialize(configBuilder.build())
val vimeoUrl = "videos/foo" 
VimeoClient.getInstance().fetchNetworkContent(vimeoUrl, object : ModelCallback<Video>(Video::class.java) {
                override fun success(video: Video?) {
                }

                override fun failure(error: VimeoError?) {
                }
            })
iganinja commented 4 years ago

For anyone else encountering this issue: It is most likely a problem with the url or ModelCallback model-object you are providing. The json issue most likely indicates the response does not match the expected model object.

For my case specifically, "https://vimeo.com/foo/bar" is the web-url for a vimeo video. The api to fetch Video data is "https://api.vimeo.com/videos/foo". The base api url ("https://api.vimeo.com/") can be found in Vimeo.VIMEO_BASE_URL_STRING. This means the actual uri to fetch Video data becomes "videos/foo".

The following code is now working for me:

        val scope = "public private video_files"
        val isDebug = true
        val loggingInterceptor = HttpLoggingInterceptor()
        loggingInterceptor.level = if (isDebug) HttpLoggingInterceptor.Level.BODY else HttpLoggingInterceptor.Level.NONE

        val configBuilder: Configuration.Builder = Configuration.Builder("MY_CLIENT_ID", "MY_CLIENT_SECRET", scope)
            .setBaseUrl(Vimeo.VIMEO_BASE_URL_STRING)
            .setAccessToken("MY_ACCESS_TOKEN")
            .setGsonDeserializer(GsonDeserializer())
            .setCacheDirectory(context.cacheDir)
            .addNetworkInterceptor(loggingInterceptor)

        if(isDebug) {
            configBuilder.enableCertPinning(false)
            configBuilder.setLogLevel(Vimeo.LogLevel.VERBOSE)
        }

        VimeoClient.initialize(configBuilder.build())
val vimeoUrl = "video/foo" 
VimeoClient.getInstance().fetchNetworkContent(vimeoUrl, object : ModelCallback<Video>(Video::class.java) {
                override fun success(video: Video?) {
                }

                override fun failure(error: VimeoError?) {
                }
            })

Thank you very much rvanderlinden for your advice, now I have it working like a charm. Just a small typo in the vimeoUrl part:

val vimeoUrl = "video/foo"

it should be "videos/foo" instead of "video/foo". In plural instead of singular.

rvanderlinden commented 4 years ago

@iganinja Glad to be of help. Sorry about the missing s, it probably slipped in when converting project-code to example. Edited for future reference.