kaaes / spotify-web-api-android

A wrapper for Spotify Web API. It uses Retrofit to create Java interfaces from API endpoints
http://kaaes.github.io/spotify-web-api-android
MIT License
380 stars 138 forks source link

NetworkOnMainThreadException #155

Open LucasArgate opened 7 years ago

LucasArgate commented 7 years ago

Hello guys, when i use this command: UserPrivate user = spotify.getMe();

i get the error:

 D/Retrofit: ---> HTTP GET https://api.spotify.com/v1/me
08-21 19:19:10.180 5804-5804/deezer.bot.muziks.deezerbotmuziks I/System.out: (HTTPLog)-Static: isSBSettingEnabled false
08-21 19:19:10.180 5804-5804/deezer.bot.muziks.deezerbotmuziks I/System.out: (HTTPLog)-Static: isSBSettingEnabled false
08-21 19:19:10.190 5804-5804/deezer.bot.muziks.deezerbotmuziks D/Retrofit: ---- ERROR https://api.spotify.com/v1/me
08-21 19:19:10.190 5804-5804/deezer.bot.muziks.deezerbotmuziks D/Retrofit: android.os.NetworkOnMainThreadException
                                                                               at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1273)
                                                                               at java.net.InetAddress.lookupHostByName(InetAddress.java:431)
                                                                               at java.net.InetAddress.getAllByNameImpl(InetAddress.java:252)
                                                                               at java.net.InetAddress.getAllByName(InetAddress.java:215)
                                                                               at com.android.okhttp.internal.Network$1.resolveInetAddresses(Network.java:29)
                                                                               at com.android.okhttp.internal.http.RouteSelector.resetNextInetSocketAddress(RouteSelector.java:220)
                                                                               at com.android.okhttp.internal.http.RouteSelector.nextProxy(RouteSelector.java:176)
                                                                               at com.android.okhttp.internal.http.RouteSelector.next(RouteSelector.java:108)
                                                                               at com.android.okhttp.internal.http.HttpEngine.createNextConnection(HttpEngine.java:482)
                                                                               at com.android.okhttp.internal.http.HttpEngine.nextConnection(HttpEngine.java:465)
                                                                               at com.android.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:447)
                                                                               at com.android.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:353)
                                                                               at com.android.okhttp.internal.huc.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:476)
                                                                               at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:418)
                                                                               at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getResponseCode(HttpURLConnectionImpl.java:540)
                                                                               at com.android.okhttp.internal.huc.DelegatingHttpsURLConnection.getResponseCode(DelegatingHttpsURLConnection.java:105)
                                                                               at com.android.okhttp.internal.huc.HttpsURLConnectionImpl.getResponseCode(HttpsURLConnectionImpl.java:25)
                                                                               at retrofit.client.UrlConnectionClient.readResponse(UrlConnectionClient.java:73)
                                                                               at retrofit.client.UrlConnectionClient.execute(UrlConnectionClient.java:38)
                                                                               at retrofit.RestAdapter$RestHandler.invokeRequest(RestAdapter.java:326)
                                                                               at retrofit.RestAdapter$RestHandler.invoke(RestAdapter.java:240)
                                                                               at java.lang.reflect.Proxy.invoke(Proxy.java:393)
                                                                               at $Proxy0.getMe(Unknown Source)
                                                                               at deezer.bot.muziks.deezerbotmuziks.HomeSpotify$1.onClick(HomeSpotify.java:60)
                                                                               at android.view.View.performClick(View.java:5697)
                                                                               at android.widget.TextView.performClick(TextView.java:10815)
                                                                               at android.view.View$PerformClick.run(View.java:22526)
                                                                               at android.os.Handler.handleCallback(Handler.java:739)
                                                                               at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                               at android.os.Looper.loop(Looper.java:158)
                                                                               at android.app.ActivityThread.main(ActivityThread.java:7229)
                                                                               at java.lang.reflect.Method.invoke(Native Method)
                                                                               at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
                                                                               at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
08-21 19:19:10.190 5804-5804/deezer.bot.muziks.deezerbotmuziks D/Retrofit: ---- END ERROR

Before this, i get correcly service:

accessToken = response.getAccessToken();
api.setAccessToken(accessToken);
spotify = api.getService()

What i doing wrong ?

thanks!

eddiecrawford commented 7 years ago

@LucasArgate You need to wrap spotify.getMe() in a separate thread. I would recommend using Androids asyncTask class to call all network operations, and then overriding the onPostExecute method to use the UserInfo data for whatever you need. IE:

public UserPrivate fetchUserInfo() { new AsyncTask<Void, Void, UserPrivate>() {

        @Override
        protected UserPrivate doInBackground(Void... voids) {
            UserPrivate userPrivate = null;
            try {
                userPrivate = mService.getMe();
            } catch (Exception e) {
                Log.e(TAG, "Error fetching UserInfo: " + e);
            }
            return userPrivate;
        }

        @Override
        protected void onPostExecute(UserPrivate userPrivate) {
            // Do what you need to with user data
        }
    }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
}