1

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.

behzad
  • 25
  • 7

1 Answers1

1

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";
}
Tenten Ponce
  • 2,436
  • 1
  • 13
  • 40