Awais Imran
Awais Imran

Reputation: 85

onPostExecute not allowing me to create Toast message

Please see this before for context: Anonymous Uploading File object to Imgur API (JSON) gives Authentication Error 401 (it has the code for doInBackground() method in case anyone is interested)

Using an AsyncTask class, I am uploading an image to Imgur. The uploading process is done within doInBackground() method. It returns String link to onPostExecute which should display the link in the form of a Toast message.

@Override
protected void onPostExecute(String result) 
{
    super.onPostExecute(result);
    Toast.makeText(getApplicationContext(), "Uploaded! Link: " + result, Toast.LENGTH_SHORT).show();
}

However, doing this gives the following error:

The method getApplicationContext() is undefined for the type UploadToImgurTask

Trying to copy the return string to the clipboard gives a similar issue.

@Override
protected void onPostExecute(String result) 
{
    super.onPostExecute(result);
    ClipboardManager clipboard = (ClipboardManager)getSystemService(Context.CLIPBOARD_SERVICE); 
    ClipData clip = ClipData.newPlainText("label", "Text to copy");
    clipboard.setPrimaryClip(clip);
}

The method getSystemService(String) is undefined for the type UploadToImgurTask

Upvotes: 2

Views: 3981

Answers (3)

Ashish Tikarye
Ashish Tikarye

Reputation: 870

try this code

public static void pushprofList(Activity context){
 static Context = mConext;

protected void onPostExecute(String result) {

      Toast toast=Toast.makeText(mConext,"Succefully Updated Profile",Toast.LENGTH_LONG);
             toast.show();

    }
  }

this working completely and display toast message.

Upvotes: 0

user2281330
user2281330

Reputation: 181

In place of getApplicationContext(), use AsyncTask's Parent class name with ".this" like MyActivity.this if it extends from Activity Otherwise use getActivity(). Hoping your problem will be solved with this

Upvotes: 1

Shobhit Puri
Shobhit Puri

Reputation: 26017

@Raghunandan is right. So, inside your UploadToImgurTask class you can have:

private Context context;
//in constructor:
public UploadToImgurTask(Context context){
        this.context=context;
}

Then in onPostExecute you can simply use:

Toast.makeText(context, "Uploaded! Link: " + result, Toast.LENGTH_SHORT).show();

Hope this helps you.

Upvotes: 5

Related Questions