Question

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

Was it helpful?

Solution

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

OTHER TIPS

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

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top