c-eg / themoviedbapi

A Java wrapper around the JSON API provided by TheMovieDB.org
https://www.themoviedb.org/
Other
281 stars 95 forks source link

Add Exception for TmdbApi constructor when there is no Internet connection. #80

Closed nateshmbhat closed 11 months ago

nateshmbhat commented 6 years ago

If we aren't connected to internet , It gives Thread errors related to the Async Task in which we are using the Tmdb api . Please add an exception about "no internet connection"

holgerbrandl commented 6 years ago

How would we do so in a platform agnostic way to make sure that the lib would work on android and on the server-side.

nateshmbhat1 commented 6 years ago

I don't think adding an exception would break this functionality. Since without it would definitely error out, it's best to add exception. Any other fix and workaround are welcome if u suggest.

holgerbrandl commented 6 years ago

It throws a MovieDbException exception already, if there's no internet:

info.movito.themoviedbapi.tools.MovieDbException: https://api.themoviedb.org/3/configuration?api_key=foo

    at info.movito.themoviedbapi.tools.WebBrowser.request(WebBrowser.java:187)
    at info.movito.themoviedbapi.tools.WebBrowser.request(WebBrowser.java:112)
    at info.movito.themoviedbapi.TmdbApi.requestWebPageInternal(TmdbApi.java:91)
    at info.movito.themoviedbapi.TmdbApi.requestWebPage(TmdbApi.java:85)
    at info.movito.themoviedbapi.AbstractTmdbApi.mapJsonResult(AbstractTmdbApi.java:58)
    at info.movito.themoviedbapi.AbstractTmdbApi.mapJsonResult(AbstractTmdbApi.java:45)
    at info.movito.themoviedbapi.AbstractTmdbApi.mapJsonResult(AbstractTmdbApi.java:40)
    at info.movito.themoviedbapi.TmdbConfig.getConfig(TmdbConfig.java:18)
    at info.movito.themoviedbapi.TmdbApi.<init>(TmdbApi.java:54)
    at info.movito.themoviedbapi.TmdbApi.<init>(TmdbApi.java:44)
    at info.movito.themoviedbapi.AbstractTmdbApiTest.setUpClass(AbstractTmdbApiTest.java:57)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:564)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
    at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:24)
    at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:160)
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
    at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
    at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
    at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
Caused by: java.net.UnknownHostException: api.themoviedb.org
    at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:488)
    at java.base/sun.net.www.protocol.http.HttpURLConnection$10.run(HttpURLConnection.java:1963)
    at java.base/sun.net.www.protocol.http.HttpURLConnection$10.run(HttpURLConnection.java:1958)
    at java.base/java.security.AccessController.doPrivileged(Native Method)
    at java.base/sun.net.www.protocol.http.HttpURLConnection.getChainedException(HttpURLConnection.java:1957)
    at java.base/sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1525)
    at java.base/sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1509)
    at java.base/java.net.HttpURLConnection.getResponseCode(HttpURLConnection.java:527)
    at java.base/sun.net.www.protocol.https.HttpsURLConnectionImpl.getResponseCode(HttpsURLConnectionImpl.java:329)
    at info.movito.themoviedbapi.tools.WebBrowser.request(WebBrowser.java:153)

What else should it do here?

nateshmbhat1 commented 6 years ago

It didn't give this exception for me. I was running the code inside async task, background method.

holgerbrandl commented 6 years ago

What's your stacktrace?

nateshmbhat commented 6 years ago

It doesn't throw an exception at all. But logcat shows this error :

07-04 18:40:02.322 10683-10841/com.techy.nateshmbhat.moviego W/System.err: SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
07-04 18:40:02.323 10683-10841/com.techy.nateshmbhat.moviego W/System.err: SLF4J: Defaulting to no-operation (NOP) logger implementation
    SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
07-04 18:40:02.326 10683-10706/com.techy.nateshmbhat.moviego D/FA: Logging event (FE): screen_view(_vs), Bundle[{firebase_event_origin(_o)=auto, firebase_previous_class(_pc)=MainActivity, firebase_previous_id(_pi)=-6645009706440117657, firebase_screen_class(_sc)=UpcomingMovies, firebase_screen_id(_si)=-6645009706440117655}]
07-04 18:40:02.354 10683-10841/com.techy.nateshmbhat.moviego I/DpmTcmClient: RegisterTcmMonitor from: com.android.okhttp.TcmIdleTimerMonitor
07-04 18:40:02.368 10683-10841/com.techy.nateshmbhat.moviego E/AndroidRuntime: FATAL EXCEPTION: AsyncTask #1
    Process: com.techy.nateshmbhat.moviego, PID: 10683
07-04 18:40:02.369 10683-10841/com.techy.nateshmbhat.moviego E/AbstractTracker: Can't create handler inside thread that has not called Looper.prepare()
07-04 18:40:02.372 10683-10841/com.techy.nateshmbhat.moviego D/OSTracker: OS Event: crash
07-04 18:40:02.373 10683-10841/com.techy.nateshmbhat.moviego E/AbstractTracker: mTrackerAsyncQueryHandler is null
07-04 18:40:02.437 10683-10725/com.techy.nateshmbhat.moviego D/OpenGLRenderer: endAllActiveAnimators on 0x730a3c4800 (RippleDrawable) with handle 0x73001cf560

This is how I am using your API in my code :

public class MovieInterface extends AsyncTask<Void, Void, MovieResultsPage> {

    Context context;
    ArrayList<MovieDb> moviesList = new ArrayList<>();
    String mode ;
    ListView moviesListView;

    MovieInterface(Context context, ListView moviesListView ) {
        this.context = context;
        this.moviesListView = moviesListView;
    }

    @Override
    protected MovieResultsPage doInBackground(Void... strings) {

        MovieResultsPage upcomingMovies = null ;
            TmdbMovies movies = new TmdbApi("mykey").getMovies();

           upcomingMovies = movies.getUpcoming(null, 1, "IN");

        return upcomingMovies ;
    }

    @Override
    protected void onPostExecute(final MovieResultsPage movieDbs) {
        super.onPostExecute(movieDbs) ; 
    }
}
nateshmbhat commented 6 years ago

Error occurs at constructor for TmdbApi , putting a try catch worked there .