1

I got a tricky interview question and hope somebody can help me out..

Scenario: You have a web services with Json or xml. On click of a button, how exactly you would get the data, parse the data and display it on the User Interface; beside this, how would you get the same task to update the data every fifteen minutes;

The last one and the most tricky one: how would you handle the case where both button and 15 minute refresh would happen at the same time.

(I think I should use AsyncTask to handle the last one but can't figure out how exactly should I do)

Thanks in advance!

Sherman_Meow
  • 247
  • 3
  • 13

1 Answers1

1

I am not sure but you can use AsyncHttpClient or other similar HTTP clients to request the JSON object from the server. Then either using GSON or in other ways you can parse the JSON object. If its XML then you can use XML parsers.

For doing it every 15 minutes you have use AlarmManager and Service to schedule the 15 minute HTTP requests.

For the case of handling the last case, if you use AsyncTask then it might be that you send two requests and get 2 responses back. Then which one will you use to update GUI? I am not sure but there can be a way out by using Synchronization. You might create a separate class which handles the HTTP stuff. That can be synchronized, so that only one instance of the object is used at once. This way either the Button touch will call or the Service. Since there would be a lock on creating a object, till the time its released the second one won't be able to obtain an instance.

Not sure but I could think of nothing else.

Community
  • 1
  • 1
Shobhit Puri
  • 25,769
  • 11
  • 95
  • 124
  • Thank you! I agree that the solution for the last case is using Synchronization, while you gave me more details. Appreciate your help;) – Sherman_Meow Aug 29 '13 at 16:34