Search code examples
javaandroidsdkevernote

Evernote getNoteContent() Asynctask Error


I´m doing a proyect using the SDK of Evernote for Android. I´m trying to get the content from one of my notes(onItemClick) but everytime I use the method, the application crash with a Thread Exception. This is the part of the code where I use the method:

public void onFragmentInteraction(Note note) throws EDAMUserException, EDAMSystemException, EDAMNotFoundException, TException, ExecutionException, InterruptedException {
    String noteGuid = note.getGuid();
    String Content = new GetNoteTask().execute(noteGuid).get();
    Intent intent = new Intent(this, ViewNoteActivity.class);
    intent.putExtra(intent.EXTRA_TEXT, Content);
    startActivity(intent);

}

public class GetNoteTask extends AsyncTask<String, Void, String> {
private EvernoteSession mEvernoteSession;


public String noteContent(String guid) throws EDAMUserException, EDAMSystemException, TException, EDAMNotFoundException {
    final EvernoteNoteStoreClient noteStoreClient = mEvernoteSession.getEvernoteClientFactory().getNoteStoreClient();
        String noteText;
        noteText = noteStoreClient.getNoteContent(guid);
        return noteText;
    }

@Override
protected String doInBackground(String... params) {
    String guid = params[0];
    String noteContent = null;
    try {
        noteContent = noteContent(guid);
    } catch (EDAMUserException e) {
        e.printStackTrace();
    } catch (EDAMSystemException e) {
        e.printStackTrace();
    } catch (TException e) {
        e.printStackTrace();
    } catch (EDAMNotFoundException e) {
        e.printStackTrace();
    }
    return noteContent;
}
}

The first method is located in my MainActivity which extends a Fragment. The second one is my Task to get the content note. It breaks when I initialize the Evernote client in the asynctask.

 final EvernoteNoteStoreClient noteStoreClient = mEvernoteSession.getEvernoteClientFactory().getNoteStoreClient();
        String noteText;

Solution

  • I already found the solution. I can´t call the EvernoteClient without the used mEvernoteSession which is declared in my Main Activity, so I tried to send the noteStoreClient but it was always empty because the method was private and not protected, so I didn't fill it when I was sending it.

    Finally I created a Static class where I save my variables:

     public static class MyTaskParams{
            public String guid;
            public EvernoteNoteStoreClient noteStore;
    
    
        public MyTaskParams(String noteGuid, EvernoteNoteStoreClient noteStoreClient) {
            this.guid=noteGuid;
            this.noteStore=noteStoreClient;
        }
    

    and I sended it later to the AsyncTask.

     MyTaskParams params = new MyTaskParams(noteGuid,noteStoreClient);
    
            GetNoteTask GetNoteTask = new GetNoteTask();
    

    So finally my Asynctask looks like:

    public class GetNoteTask extends AsyncTask<MainActivity.MyTaskParams,Void , String> {
    
    public String noteContent(EvernoteNoteStoreClient noteStoreClient, String guid) throws EDAMUserException, EDAMSystemException, TException, EDAMNotFoundException {
            String noteText;
            noteText = noteStoreClient.getNoteContent(guid);
            return noteText;
        }
    
    
    protected String doInBackground(MainActivity.MyTaskParams... params) {
    
        String noteContent = null;
        String guid = params[0].guid;
        EvernoteNoteStoreClient noteStoreClient = params[0].noteStore;
        try {
    
            noteContent = noteContent(noteStoreClient, guid);
        } catch (EDAMUserException e) {
            e.printStackTrace();
        } catch (EDAMSystemException e) {
            e.printStackTrace();
        } catch (TException e) {
            e.printStackTrace();
        } catch (EDAMNotFoundException e) {
            e.printStackTrace();
        }
        return noteContent;
    }