behzad
behzad

Reputation: 25

Retrofit Synchronous Requests error

I'm write this code for call retrofit Synchronous Requests :

public String LOGININTO() throws IOException {
     final String[] message = {""};
     LOGININTERFACE taskService = ServiceGenerator.createService(LOGININTERFACE.class);
     Call<LGOINMODEL> tasks = taskService.savePost("0016642902","0016642902","password");
     LGOINMODEL model=null;
     model=tasks.execute().body();
     return "OK";
}


but when run my code get this error:
at

Caused by: android.os.NetworkOnMainThreadException retrofit2.ExecutorCallAdapterFactory$ExecutorCallbackCall.execute(ExecutorCallAdapterFactory.java:91)

How can I solve that Problem?

thanks.

Upvotes: 1

Views: 1261

Answers (1)

Tenten Ponce
Tenten Ponce

Reputation: 2506

Wrap your execution on a new Thread():

public String LOGININTO() throws IOException {
 final String[] message = {""};
 LOGININTERFACE taskService = ServiceGenerator.createService(LOGININTERFACE.class);
 Call<LGOINMODEL> tasks = taskService.savePost("0016642902","0016642902","password");
 LGOINMODEL model=null;
 Thread thread = new Thread(new Runnable() {
    @Override
    public void run() {
       model=tasks.execute().body();
    }
 });
 thread.start();

 return "OK";
}

Upvotes: 1

Related Questions