-
-
Save aguynamedrich/80c59fb70e195ca142bb to your computer and use it in GitHub Desktop.
Sample generic AsyncTask class
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.yourdomain.yourapp; | |
public class YourActivity extends Activity { | |
public void makeServiceCall() { | |
OnPostExecuteListener listener = new OnPostExecuteListener() { | |
public void onPostExecute(String result) { | |
// do something with the result | |
} | |
}; | |
String url = "http://www.yourapi.com/your_endpoint"; | |
new ServiceTask(listener).execute(url); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.yourdomain.yourapp; | |
import android.os.AsyncTask; | |
public class ServiceTask extends AsyncTask<String, Void, String> { | |
OnPostExecuteListener listener = null; | |
public ServiceTask(OnPostExecuteListener listener) { | |
this.listener = listener; | |
} | |
@Override | |
protected String doInBackground(String... params) { | |
String url = params[0]; | |
String data = ServiceHelper.get(url); | |
return data; | |
} | |
@Override | |
protected void onPostExecute(String result) { | |
if(listener != null) | |
listener.onPostExecute(result); | |
} | |
public interface OnPostExecuteListener { | |
public void onPostExecute(String result); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment