java - Retrofit call inside AsyncTask -


i've started developing android app , decided use retrofit client of rest service, not sure if approach good:

i. i've implemented asynchronous call api, called inside asynctask's doinbackground method. concern: having read this article got me confused. aren't asynctasks suitable kind of tasks? should make call api directly activity? understand retrofit's callback methods executed on ui thread, how call on http? retrofit create threads that?

ii. want authenticationresponse saved inside sharedpreferences object, doesn't seem available inside success method of callback. suggestions/ practices?

thank in advance :)

here's doinbackgroundmethod:

    @override     protected string doinbackground(string... params) {         log.d(location_login_task_tag, params[0]);          locationapi.getinstance().auth(new authenticationrequest(params[0]), new callback<authenticationresponse>() {              @override             public void success(authenticationresponse authenticationresponse, response response) {                 log.i("location_login_success", "successfully logged user locationapi");             }              @override             public void failure(retrofiterror error) {                 log.e("location_login_error", "error while authenticating user in locationapi", error);             }         });         return null;     } 

i. retrofit supports 3 ways make request:

  • synchronic

you have declare method returns response value example:

  @get("/your_endpoint")   authenticationresponse auth(@body authenticationrequest authrequest); 

this method done in thread in called. you can't call in main/ui thread.

  • asynchronous

you have declare void method contains callback response last param example:

  @get("/your_endpoint")   voud auth(@body authenticationrequest authrequest, callback<authenticationresponse> callback); 

the execution of request called in new background thread, , callback methods done in thread method called. can call method in main/ui thread without new thread/asynctask.

  • using rxandroid

the last way know method uses rxandroid. have declare method returns response observable value. example:

  @get("/your_endpoint")   observable<authenticationresponse> auth(@body authenticationrequest authrequest); 

this method supports make network request in new thread. don't have create new thread/asynctask. action1 callback subscribe method called in ui/main thread.

ii. can call method in activity , can write data sharedpreferences shown below:

sharedpreferences sp = preferencemanager.getdefaultsharedpreferences(context); sharedpreferences.edit()             .put...//put data authenticationresponse                     //object passed params in callback method.             .apply(); 

Comments

Popular posts from this blog

google chrome - Developer tools - How to inspect the elements which are added momentarily (by JQuery)? -

angularjs - Showing an empty as first option in select tag -

php - Cloud9 cloud IDE and CakePHP -