Hot questions for Handling NetworkOnMainThreadException in Retrofit

Top 10 Java Open Source / Retrofit / Handling NetworkOnMainThreadException

RxJava 2 / Retrofit 2 - NetworkOnMainThreadException

Question: I need to perform a request, if my token is expired I need to refresh it and retry the request.

This is how I'm trying to do it, at the moment I can refresh the token but it throws me a NetworkOnMainThreadException. It finishes the request,update the token and reaches logs, but that exception its killing me. How can I avoid that?

public Observable<Estabelecimento> listarEstabelecimentos() {
    return Observable.defer(this::getListarEstabelecimentoObservable)
            .retryWhen(throwableObservable -> throwableObservable.flatMap(
                    throwable -> {
                        if (throwable instanceof UnauthorizedException) {
                            return mRequestManager.getTokenObservable(AutoAtendimentoApplication.getContext())
                                    .doOnNext(response -> /* log stuff */)
                                    .flatMap((Function<AuthResponse, ObservableSource<?>>) response2 ->
                                            getListarEstabelecimentoObservable()
                                                    .doOnNext(estabelecimento ->
                                                            /* log stuff */)
                                                    )
                                    );
                        }
                        return Observable.error(throwable);
                    }));
}

NetWorkErrorHandler:

public <T> T  processError(Response<T> response ) {
    switch (response.code()) {
        case 401:
            throw new UnauthorizedException();
        default:
            return response.body();
    }
}

Token:

private Observable<AuthResponse> getToken(Context context,
                                          @GrantType.GrantTypeDef String grantType, @Nullable String refreshToken) {

    SessionManager sessionManager = SessionManager.getInstance(context);
    Usuario usuario = sessionManager.getUser();

    AuthRequest request = new AuthRequest(usuario.getUsername(),
            usuario.getPassword(), grantType, refreshToken);

    return mAuthAPIService.getToken(request)
            .observeOn(AndroidSchedulers.mainThread())
            .subscribeOn(Schedulers.io())
            .map(response -> storeTokens(response, context));
}

I'm pretty sure the problem happens inside the flatmap and put subscribeOn does not solve the problem

Answer: Your network is running on main thread, you can solve it by adding observeOn(AndroidSchedulers.mainThread()) and subscribeOn(Schedulers.io()) to your observable.

The observeOn(...) means that the results are emmited on the specified thread - in this case the UI thread.

The subscribeOn(...) call is basically where the request is processed. If omitted the computation is done on current thread.


Retrofit - android.os.NetworkOnMainThreadException

Question: I am using Retrofit 2 to get json and parse it to POJO. My purpose is getting one value of that object.

compile 'com.squareup.retrofit2:retrofit:2.0.0-beta4'
compile 'com.squareup.retrofit2:converter-gson:2.0.0-beta4'

My REST client:

public interface MyClient {
    @GET("/part1/part2")
    Call<MyItem> getMyItem(@Query("param1") String param1,
                                                 @Query("param2") String param2,
                                                 @Query("param3") String param3);
}

I found great great tool to create service:

public class ServiceGenerator {
    public static final String API_BASE_URL = "http://my.api.com";

    private static OkHttpClient.Builder httpClient = new OkHttpClient.Builder();

    private static Retrofit.Builder builder =
            new Retrofit.Builder()
                    .baseUrl(API_BASE_URL)
                    .addConverterFactory(GsonConverterFactory.create());

    public static <S> S createService(Class<S> serviceClass) {
        Retrofit retrofit = builder.client(httpClient.build()).build();
        return retrofit.create(serviceClass);
    }
}

Then I am creating new service using Service generator class:

MyClient api = ServiceGenerator.createService(MyClient.class);
        Call<MyItem> call = api.getMyItem(param1, param2, param3);
        MyItem myItem= null;
        try {
            myItem= call.execute().body();
            Log.d("MyTag", myItem.getValue());
        } catch (IOException e) {
            e.printStackTrace();
        }

When I am trying to run this code I am getting this error:

android.os.NetworkOnMainThreadException 
at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1147) 
at java.net.InetAddress.lookupHostByName(InetAddress.java:418) 
at java.net.InetAddress.getAllByNameImpl(InetAddress.java:252) 
at java.net.InetAddress.getAllByName(InetAddress.java:215) 
...

I thought that Retrofit automatically does the job in the background thread. Or I misunderstood something. What is wrong in this situation and how to solve it?

Answer: It does if you use the asynchronous version - enqueue. You're using the synchronous version, which runs on the calling thread.

Call it this way instead:

MyClient api = ServiceGenerator.createService(MyClient.class);
Call<MyItem> call = api.getMyItem(param1, param2, param3);
call.enqueue(new Callback<MyItem>() {
    @Override
    public void onResponse(Call<MyItem> call, Response<MyItem> response) {
        MyItem myItem=response.body();
    }

    @Override
    public void onFailure(Call<MyItem> call, Throwable t) {
        //Handle failure
    }
});

In onResponse(), use response.body() to get your response, such as: MyItem myItem=response.body();


NetworkOnMainThreadException in concatMap() with Retrofit 2 and RxJava

Question: In my app I'm trying to use RxJava to react when the user search for a new movie and retrieve movie data from a webservice. In order to do this I thought to use concatMap to concatenate the action of getting the new movie query to requesting the movie to the webservice. Doing this I get a NetworkOnMainThread exception and I don't understand the reason.

createSearchViewObservable(searchView)
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .concatMap(new Func1<String, Observable<MoviesWrapper>>() {
                @Override
                public Observable<MoviesWrapper> call(String title) {
                    RestMovieSource repo = new RestMovieSource();
                   return repo.searchMovieByTitle(title);
                }
            })
            .subscribe(new Subscriber<MoviesWrapper>() {
                @Override
                public void onCompleted() {

                }

                @Override
                public void onError(Throwable e) {
                    e.printStackTrace();
                    Toast.makeText(getContext(), e.getCause().getMessage(), Toast.LENGTH_SHORT).show();
                }

                @Override
                public void onNext(MoviesWrapper moviesWrapper) {
                    for (Movie movie : moviesWrapper.getResults()) {
                        Toast.makeText(getContext(), movie.getTitle(), Toast.LENGTH_SHORT).show();
                    }
                }
            });
D/OkHttp: --> GET /3/search/movie?api_key=xxxx&query=Jurassic%20World HTTP/1.1
D/OkHttp: --> END GET
 android.os.NetworkOnMainThreadException
      at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1147)
      at java.net.InetAddress.lookupHostByName(InetAddress.java:418)
      at java.net.InetAddress.getAllByNameImpl(InetAddress.java:252)
      at java.net.InetAddress.getAllByName(InetAddress.java:215)
      at com.squareup.okhttp.Dns$1.lookup(Dns.java:39)
      at com.squareup.okhttp.internal.http.RouteSelector.resetNextInetSocketAddress(RouteSelector.java:184)
      at com.squareup.okhttp.internal.http.RouteSelector.nextProxy(RouteSelector.java:153)
      at com.squareup.okhttp.internal.http.RouteSelector.next(RouteSelector.java:95)
      at com.squareup.okhttp.internal.http.HttpEngine.createNextConnection(HttpEngine.java:345)
      at com.squareup.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:328)
      at com.squareup.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:246)
      at com.squareup.okhttp.Call.getResponse(Call.java:276)
      at com.squareup.okhttp.Call$ApplicationInterceptorChain.proceed(Call.java:234)
      at com.squareup.okhttp.logging.HttpLoggingInterceptor.intercept(HttpLoggingInterceptor.java:180)
      at com.squareup.okhttp.Call$ApplicationInterceptorChain.proceed(Call.java:223)
      at com.squareup.okhttp.Call.getResponseWithInterceptorChain(Call.java:196)
      at com.squareup.okhttp.Call.execute(Call.java:79)
      at retrofit.OkHttpCall.execute(OkHttpCall.java:116)

Answer: I solved the problem defining subscribeOn and observeOn for the returning observable from the concatMap(). The subscribeOn/observeOn outside are specific only to the first observable from createSearchViewObservable(searchView).

createSearchViewObservable(searchView)
        .subscribeOn(Schedulers.io())
        .observeOn(AndroidSchedulers.mainThread())
        .concatMap(new Func1<String, Observable<MoviesWrapper>>() {
            @Override
            public Observable<MoviesWrapper> call(String title) {
                RestMovieSource repo = new RestMovieSource();
               return repo.searchMovieByTitle(title)
                      .subscribeOn(Schedulers.io())  
                      .observeOn(AndroidSchedulers.mainThread());;
            }
        })
        .subscribe(...)

NetworkOnMainThreadException with retrofit-beta2 and rxjava

Question: I recently upgraded from retroft-beta1 and this was working. I have the following interface for the API:

public interface Service {
    @POST("path")
    Observable<Object> service();
}

And the following call:

service.service()
        .observeOn(AndroidSchedulers.mainThread())
        .subscribe();

And it throws a NetworkOnMainThreadException. But this was working in retrofit-beta1.

Answer: From retrofit-beta2, calls to Observable methods now behave synchronously. So subscribeOn must be used:

service.service()
        .subscribeOn(Schedulers.io())
        .observeOn(AndroidSchedulers.mainThread())
        .subscribe();

Retrofit 2 error: NetworkOnMainThreadException

Question: I'm trying to do a synchronous request to my server in Android. (Code works flawlessly in a plain Java project)

I know that sync requests shouldn't be done on the main thread, so I start a new thread where the networking is done. I also know that it is possible to do Async-calls but a sync call fits my usecase better.

bool networkingIsDoneHere(){
  dostuff();
  int number = doNetworkCall();
  if(number < 500){
    return true;
  }
  return false
}

The problem is that I still get the "NetworkOnMainThreadException" error. Does retrofit somehow run on the main thread, while being executed on a side thread?

Starting new thread:

Thread timerThread = new Thread() {
        @Override
        public void run() {
            while (true) {
                try {
                    networkingIsDoneHere();
                    sleep(getExecutionInterval());
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    };

    @Override
    public void startRule() {
        timerThread.start();
    }

Retrofit code

private Retrofit createRetrofitAdapter() throws Exception {
        return retrofit = new Retrofit.Builder()
                .baseUrl("https://maps.googleapis.com/maps/api/")
                .addConverterFactory(GsonConverterFactory.create())
                .build();

    }

    public interface ApiGoogleMapsService {
        @GET("url...")
        Call<MapApiCall> getDurationJson(@Query("origins") String origin, @Query("destinations") String destination);
    }

error:

Caused by: android.os.NetworkOnMainThreadException
be.kul.gj.annotationfw W/System.err:     at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1273)
be.kul.gj.annotationfw W/System.err:     at com.android.org.conscrypt.OpenSSLSocketImpl.shutdownAndFreeSslNative(OpenSSLSocketImpl.java:1131)
...

Answer: Instead of starting a new thread yourself, Retrofit can automatically do requests on a separate thread. You can do this by calling enqueue();.

Call<MapApiCall> call = api.getDurationJson(foo);

call.enqueue(new Callback<MapApiCall>() {
       @Override
       public void onResponse(Call<MapApiCall> call, Response<MapApiCall> response) {
           //Do something with response
       }

       @Override
       public void onFailure(Call<MapApiCall> call, Throwable t) {
           //Do something with failure
       }
   });

If you want synchronous requests use execute() within your seperate thread:

Call<MapApiCall> call = api.getDurationJson();  
MapApiCall apiCall = call.execute().body(); 

If you, for some reason, want to disable the NetworkOnMainThreadException without using threads use:

StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);

However, this is really not recommended, if you don't know what StrictMode does, do NOT use it.


RxJava and Retrofit2: NetworkOnMainThreadException

Question: I realize that I am using subscribeOn()/observeOn() on the MainThread. What are the set of options I can pass into subscribeOn()? What are the set of options I can pass into observeOn()?

12-17 21:36:09.154 20550-20550/rx.test D/MainActivity2: [onCreate]
12-17 21:36:09.231 20550-20550/rx.test D/MainActivity2: starting up observable...
12-17 21:36:09.256 20550-20550/rx.test D/MainActivity2: [onError] 
12-17 21:36:09.256 20550-20550/rx.test W/System.err: android.os.NetworkOnMainThreadException

GovService.java

public interface GovService {
    @GET("/txt2lrn/sat/index_1.json")
    Observable<MyTest> getOneTestRx();
}

MyTest.java

public class MyTest {
    private String name, url;
    private int num;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public int getNum() {
        return num;
    }

    public void setNum(int num) {
        this.num = num;
    }

    @Override
    public String toString() {
        return "Name: " + this.name + ", num: " + this.num + ", url: " + this.url;
    }
}

MainActivity2.java

public class MainActivity2 extends AppCompatActivity {
    private final String TAG = getClass().getSimpleName();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.d(TAG, "[onCreate]");
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        RecyclerView mRV = (RecyclerView) findViewById(R.id.rv);
        mRV.setLayoutManager(new LinearLayoutManager(this));// setup LayoutManager
        mRV.setItemAnimator(new DefaultItemAnimator());// setup ItemAnimator

        // setup retrofit
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl("http://goanuj.freeshell.org")
                .addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                .build();
        GovService service = retrofit.create(GovService.class);

        Log.d(TAG, "starting up observable...");
        Observable<MyTest> o = service.getOneTestRx();
        o.subscribeOn(Schedulers.io());
        o.observeOn(AndroidSchedulers.mainThread());
        o.subscribe(new Subscriber<MyTest>() {
            @Override
            public void onCompleted() {
                Log.d(TAG, "[onCompleted] ");
            }

            @Override
            public void onError(Throwable t) {
                Log.d(TAG, "[onError] ");
                t.printStackTrace();
            }

            @Override
            public void onNext(MyTest m) {
                Log.d(TAG, "[onNext] " + m.toString());
            }
        });
    }
}

Answer: Rewrite the last part of your code to:

service.getOneTestRx()
    .subscribeOn(Schedulers.io())
    .observeOn(AndroidSchedulers.mainThread())
    .subscribe(new Subscriber<MyTest>() {
        @Override
        public void onCompleted() {
            Log.d(TAG, "[onCompleted] ");
        }

        @Override
        public void onError(Throwable t) {
            Log.d(TAG, "[onError] ");
            t.printStackTrace();
        }

        @Override
        public void onNext(MyTest m) {
            Log.d(TAG, "[onNext] " + m.toString());
        }
    });

Worth mentioning that one needs to chain the calls as here because Observable is not the builder pattern (where you modify the settings of an existing object)