-1

Im trying to call a method once a network call has been completed. I have tried using LocalBroadcastManager for this. I initially had it send the broadcast once the network call is complete, which worked fine, until i had multiple methods waiting for the same intent, started causing problems.

Is there a better solution or is this the best way? If it is, could you point me to somewhere where i can learn how to use this in-depth or explain how its used?

Sorry i forgot to mention I am using Volley Library to do my network calls?

Thanks

Stillie
  • 2,647
  • 6
  • 28
  • 50
  • Implement Async task for this. See for example http://stackoverflow.com/questions/9671546/asynctask-android-example – Rohit5k2 Aug 12 '15 at 07:03

2 Answers2

1

you are using BroadcastManager than definitely because of android slow cycle it call receive method multiple time at some point. you can found same problem here.

you need to do is just to prevent calling method multiple time so use above link for hint of your problem, and hint is to take flag and Handler for prevent multiple call.

Community
  • 1
  • 1
Mayur R. Amipara
  • 1,223
  • 1
  • 11
  • 32
0

You should use callbacks to wait for the network task to be done. The basis idea is the same like an onClickListener. You pass an interface implementation which will do the task what you want once the callback is called. It should be something like this:

public interface OnComplete {
    void onComplete();
    void onError(String errorMsg);
}

When you call your network stuff you should pass an implementation like this:

NetworkManager.callServer("serverurl", new OnComplete() {
    @Overwrite        
    void onComplete() {
        //Do your stuff here
    }
    @Overwrite
    void onError(String errorMsg){
    }
}

And you call it from the NetworkManager:

//from the AsyncTask when you done with the network stuff
onComplete.onComplete();

If you have to wait for multiple calls you can use the CyclicBarrier: http://docs.oracle.com/javase/7/docs/api/java/util/concurrent/CyclicBarrier.html

Gabor Novak
  • 286
  • 1
  • 7