java - How to do async OkHttp request in another class and then callback to activity -


i want move okhttp async request separate class use multiple times in multiple activities in application. possible? i've read lot can't find solution me.

mainactivity.java

public void mainactivitymethod(string jsondata) {      // handle jsondata string } 

myclass.java

public void dorequest() {      okhttpclient okhttpclient = new okhttpclient();     request request = new request.builder()             .url(url)             .build();      call call = okhttpclient.newcall(request);      call.enqueue(new callback() {         @override         public void onfailure(request request, ioexception e) {             e.printstacktrace();         }          @override         public void onresponse(final response response) throws ioexception {             if (response.issuccessful()) {                  // fetch request data                 string jsondata = response.body().string();                  mainactivitymethod(jsondata);              }         }     });  } 

or use otto. can use otto publish event , subscribe event in activity. helps in loose coupling.


Comments