Advertisement
olemork

Simple Endpoint AsyncTask class

Nov 24th, 2012
369
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.24 KB | None | 0 0
  1. /*
  2.  * A simple class for handling calls to Cloud Endpoints on background thread.
  3.  * This way the UI thread will not get blocked.
  4.  */
  5. public class NoteendpointAsyncTask extends AsyncTask<Note, Void, Void> {
  6.  
  7.     @Override
  8.     protected Void doInBackground(Note... notes) {
  9.        
  10.         Noteendpoint.Builder endpointBuilder = new Noteendpoint.Builder(
  11.                 AndroidHttp.newCompatibleTransport(),
  12.                 new JacksonFactory(),
  13.                 new HttpRequestInitializer() {                 
  14.                     @Override
  15.                     public void initialize(HttpRequest arg0) throws IOException {}
  16.                     });
  17.  
  18.         Noteendpoint endpoint = CloudEndpointUtils.updateBuilder(endpointBuilder).build();
  19.        
  20.         for(Note note : notes) {       
  21.             try {
  22.                 // Make the call into Noteendpoint, requesting insertion of the given Note.
  23.                 endpoint.insertNote(note).execute();
  24.             }
  25.             catch (IOException e) {
  26.                 // Catching some exceptions
  27.             }
  28.         }
  29.         return null;
  30.     }
  31. }
  32.  
  33. /*
  34.  * The above class can then be used in someActivity.java like this
  35.  */
  36.  
  37. Note testNote = new Note();
  38. testNote.setDescription("One small note for me, one giant note for mankind");
  39. testNote.setId("someId");
  40. testNote.setEmailAddress("test@example.com");
  41.        
  42. NoteendpointAsyncTask noteAsync = new NoteendpointAsyncTask();
  43. noteAsync.execute(testNote);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement