PokeAPI / pokekotlin

Kotlin (or Java, Scala, etc) client for PokeApi
Apache License 2.0
172 stars 43 forks source link

Crashes without internet #57

Closed Advice-Dog closed 7 years ago

Advice-Dog commented 7 years ago

While using the example, I noticed that the code would crash my application if my device didn't have internet. There was also no callstack present. Should have some way to handle this case, and also something in the logging.

sargunv commented 7 years ago

Thanks for the report. Will look into it next week.

sargunv commented 7 years ago

Throwing an exception when there's no internet is intended. You should catch the exception and show an appropriate error message to the user. Could you tell me which exception you get?

Advice-Dog commented 7 years ago

I actually wasn't able to get any exception at all, there was no callstack given. Which sounds weird, right.

Advice-Dog commented 7 years ago

This is all I was able to see: E/LOG: ║ Getting item for Pokedex. E/AndroidRuntime: FATAL EXCEPTION: AsyncTask #1 Process: com.app.poke, PID: 396

sargunv commented 7 years ago

Looks like Android is catching your exception from your AsyncTask and reporting the error. You'll need to put a try/catch in the AsyncTask.

Advice-Dog commented 7 years ago

Ok, I tried it a few more times.

Without a try catch, I did see an exception once ( Stream was closed ), but I just retried that and it went back to no exception again.

With a try/catch, it was able to catch it, but why am I unable to see that that call will throw an exception? Is it because of Kotlin?

sargunv commented 7 years ago

Yes, one of Kotlin's features is the lack of checked exceptions. On Wed, Dec 14, 2016 at 3:18 PM Chris notifications@github.com wrote:

Ok, I tried it a few more times.

Without a try catch, I did see an exception once ( Stream was closed ), but I just retried that and it went back to no exception again.

With a try/catch, it was able to catch it, but why am I unable to see that that call will throw an exception? Is it because of Kotlin?

— You are receiving this because you commented.

Reply to this email directly, view it on GitHub https://github.com/PokeAPI/pokekotlin/issues/57#issuecomment-267144565, or mute the thread https://github.com/notifications/unsubscribe-auth/ABQlpcQbUQbF2cdB-1rdbF-NuO1pnTEWks5rIE8ogaJpZM4LGlj1 .

Advice-Dog commented 7 years ago

Alright.

Would you mind updating the Java example to show how to get the network call off the main thread, and also to make sure people are aware that it can throw exceptions?

sargunv commented 7 years ago

Sure, will do. Created issue #58.

pkill37 commented 7 years ago

I'm having the same issue with the Java example. I'm well versed in Java, but very new to Android.

I have:

PokeApi pokeApi = new PokeApiClient();
PokemonSpecies bulbasaur = pokeApi.getPokemonSpecies(1);

But the app still crashes and there's no stack trace to help me. Any help? @sargunster @Advice-Dog

Advice-Dog commented 7 years ago

Do you have internet? If not, then you need to make sure you try/catch the network call. Also make sure it's on another thread, as you can't do network calls on the main thread.

pkill37 commented 7 years ago

I guess I should learn a bit more about Android first. Indeed, there was a NetworkOnMainThreadException I didn't pay much attention to. I'm using AsyncTasks now to make the request in a worker thread. Thank you @Advice-Dog! 😄

Advice-Dog commented 7 years ago

No problems, good luck. 👍