Q: Restful API service

D: I'm looking to make a service which I can use to make calls to a web based rest api. I've spent a couple of days looking through stackoverflow.com, reading books and looking at articles whilst playing about with some code and I can't get anything which I'm happy with.

Basically I want to start a service on app init then I want to be able to ask that service to request a url and return the results. In the meantime I want to be able to display a progress window or something similar.

I've created a service currently which uses IDL, I've read somewhere that you only really need this for cross app communication, so think these needs stripping out but unsure how to do callbacks without it. Also when I hit the post(Config.getURL("login"), values) the app seems to pause for a while (seems weird - thought the idea behind a service was that it runs on a different thread!)
More details (if needed): http://stackoverflow.com/questions/3197335/restful-api-service/

Test Case #10


File ID: #3197456-1-cc


protected void onHandleIntent(Intent intent) {
    Log.i(TAG, "Service started...");
    final ResultReceiver receiver = intent.getParcelableExtra("receiver");
    String command = intent.getStringExtra("command");
    Bundle b = new Bundle();
    if (command.equals("query")) {
        Log.i(TAG, "serving query request");
        receiver.send(STATUS_RUNNING, Bundle.EMPTY);
        try {
// get some data or something
            Log.i(TAG, "requesting Empty arrayList");
//Simulate downloading from the internet;
            Thread.sleep(2000);
            ArrayList
    
      results = new ArrayList
     
      (); Model hardkodiranModel =new Model(); hardkodiranModel.setTitle("Item, bozemski simnat od net"); results.add(hardkodiranModel); Log.i(TAG, results.toString()); b.putParcelableArrayList("results", results); receiver.send(STATUS_FINISHED, b); } catch (Exception e) { Log.i(TAG, "error in download" + e.getMessage()); b.putString(Intent.EXTRA_TEXT, e.toString()); receiver.send(STATUS_ERROR, b); } } this.stopSelf(); } 
     
    

  1. One small addition to the answer: as you do the mReceiver.setReceiver(null); in the onPause method, you should do the mReceiver.setReceiver(this); in the onResume method. Else you might not receive the events if your activity is resumed without being re-created
  2. Do not the docs say that you do not have to call stopSelf, since IntentService does that for you?
  3. A million up-votes.

Comments Quality
Accurate?:
Precise?:
Concise?:
Useful?: