Skip to content

Instantly share code, notes, and snippets.

@chetangiridhar
Created November 17, 2016 09:47
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save chetangiridhar/64135df970ff33a5025904a48c2bdeff to your computer and use it in GitHub Desktop.
Save chetangiridhar/64135df970ff33a5025904a48c2bdeff to your computer and use it in GitHub Desktop.
Example of AsyncTask in Android
public class MyAsync extends AsyncTask {
private Context mContext;
public MyAsync(Context context) {
//Relevant Context should be provided to newly created components (whether application context or activity context)
//getApplicationContext() - Returns the context for all activities running in application
mContext = context.getApplicationContext();
}
//Execute this before the request is made
@Override
protected void onPreExecute() {
// A toast provides simple feedback about an operation as popup.
// It takes the application Context, the text message, and the duration for the toast as arguments
Toast.makeText(mContext, "Going for the network call..", Toast.LENGTH_LONG).show();
}
//Perform the request in background
@Override
protected Integer doInBackground(String... params) {
HttpURLConnection connection;
try {
//Open a new URL connection
connection = (HttpURLConnection) new URL(params[0])
.openConnection();
//Defines a HTTP request type
connection.setRequestMethod("POST");
//Sets headers: Content-Type, Authorization
connection.setRequestProperty("Content-Type", "application/json");
connection.setRequestProperty("Accept", "application/json");
connection.setRequestProperty("Authorization", "Token fab11c9b6bd4215a989c5bf57eb678");
//Add POST data in JSON format
JSONObject jsonParam = new JSONObject();
try {
jsonParam.put("campaign_id", "4193");
} catch (JSONException e) {
e.printStackTrace();
}
//Create a writer object and make the request
OutputStreamWriter outputStream = new OutputStreamWriter(connection.getOutputStream());
outputStream.write(jsonParam.toString());
outputStream.flush();
outputStream.close();
//Get the Response code for the request
return connection.getResponseCode();
} catch (IOException e) {
e.printStackTrace();
}
return -1;
}
//Run this once the background task returns.
@Override
protected void onPostExecute(Integer integer) {
//Print the response code as toast popup
Toast.makeText(mContext, "Response code: " + integer,
Toast.LENGTH_LONG).show();
}
}
@spieczko
Copy link

spieczko commented Oct 7, 2018

Chetangiridhar,
Thanks for sharing this code. I'm playing with it now but I can't get the mContext method to fire to initialize it to something other than null.
What am I missing to get this method to fire?

public MyAsync(Context context) {
//Relevant Context should be provided to newly created components (whether application context or activity context)
//getApplicationContext() - Returns the context for all activities running in application
mContext = context.getApplicationContext();
}

Thanks,
-Steve (Android novice)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment