2

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

Community
  • 1
  • 1
Awais Imran
  • 85
  • 2
  • 4
  • 3
    use a activity context in place of getApplicationContext(). if asynctask is not a inner class of the activity pass the context to the constructor of the asynctask. use the context to display toast – Raghunandan May 23 '13 at 20:23
  • Can you demonstrate? – DSlomer64 Sep 09 '15 at 16:58

3 Answers3

5

@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.

Shobhit Puri
  • 25,769
  • 11
  • 95
  • 124
  • It worked beautifully! Thanks to both of you. One more question: my code for copying the link to the system clipboard isn't working from onPostExecute. Keeps saying getSystemService is undefined for UploadToImgurTask. :( – Awais Imran May 25 '13 at 09:04
  • Again getSystemService() is a Context method, so you need a reference to context object. you can try using `context.getSystemService()`. Hope this works. – Shobhit Puri May 25 '13 at 20:29
1

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

user2281330
  • 181
  • 2
  • 11
0

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.

Ashish Tikarye
  • 850
  • 8
  • 11