frostblooded

Registering class

Jan 6th, 2016
199
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.62 KB | None | 0 0
  1. package net.mc21.attendancecheck;
  2.  
  3. import android.os.AsyncTask;
  4. import android.util.Log;
  5.  
  6. import com.google.android.gms.gcm.GoogleCloudMessaging;
  7. import com.google.android.gms.iid.InstanceID;
  8.  
  9. import org.json.JSONException;
  10. import org.json.JSONObject;
  11.  
  12. import java.io.IOException;
  13.  
  14. public class RegisterDevice extends AsyncTask {
  15.     @Override
  16.     protected void onPreExecute(){
  17.         MainActivity.me.setContentView(R.layout.please_wait);
  18.     }
  19.  
  20.     @Override
  21.     protected Object doInBackground(Object... params) {
  22.         try {
  23.             InstanceID instanceID = InstanceID.getInstance(MainActivity.context);
  24.             String token = instanceID.getToken("<key>", GoogleCloudMessaging.INSTANCE_ID_SCOPE, null);
  25.             MainActivity.saveRegistrationToken(token);
  26.  
  27.             JSONObject json = new JSONObject();
  28.             json.put("token", token);
  29.  
  30.             HttpClient.send(json.toString().getBytes(),
  31.                     "http://<server_ip>/register_token", HttpClient.RequestType.POST);
  32.             return true;
  33.         } catch (IOException e) {
  34.             Log.i(MainActivity.TAG, e.getMessage());
  35.             MainActivity.displayToast("Can't connect to server!");
  36.         } catch (JSONException e) {
  37.             Log.i(MainActivity.TAG, e.getMessage());
  38.             MainActivity.displayToast("An error ocurred! Please try again!");
  39.         }
  40.  
  41.         return false;
  42.     }
  43.  
  44.     @Override
  45.     protected void onPostExecute(Object thereWasNoError){
  46.         if((boolean)thereWasNoError){
  47.             MainActivity.saveRegisteredState();
  48.         }
  49.  
  50.         MainActivity.me.createLayout();
  51.     }
  52. }
Advertisement
Add Comment
Please, Sign In to add comment