Android Open Source - TAQueue Queue Request






From Project

Back to project page TAQueue.

License

The source code is released under:

MIT License

If you think the Android project TAQueue listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.familybiz.greg.taqueue.network;
/*from   ww  w.  j a  v  a2 s.  c  om*/
import android.util.Log;

import com.familybiz.greg.taqueue.MainActivity;
import com.familybiz.greg.taqueue.model.User;
import com.familybiz.greg.taqueue.model.queue.QueueData;
import com.familybiz.greg.taqueue.model.queue.QueueStudent;
import com.familybiz.greg.taqueue.model.queue.QueueTA;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Greg Anderson
 */
public class QueueRequest implements NetworkRequest.OnJsonObjectReceivedListener {

  public QueueRequest() {
    MainActivity.NETWORK_REQUEST.addOnJsonObjectReceivedListener(this);
  }

  public void updateQueue(String id, String token) {
    String url = "/queue";
    MainActivity.NETWORK_REQUEST.executeGetRequest(url, id, token);
  }

  public void deleteUser(String userUrl) {
    User user = MainActivity.getUser();
    MainActivity.NETWORK_REQUEST.executeDeleteRequest("/" + userUrl + "/" + user.getId(), user.getId(), user.getToken());
  }

  @Override
  public void onJsonObjectReceived(String jsonObject) {
    if (mOnQueueInformationReceivedListener == null)
      return;

    try {
      List<QueueStudent> studentSet = new ArrayList<QueueStudent>();
      List<QueueTA> taSet = new ArrayList<QueueTA>();

      JSONObject queueJson = new JSONObject(jsonObject);
      boolean active = queueJson.getBoolean("active");
      boolean frozen = queueJson.getBoolean("frozen");
      String queueId = queueJson.getString("id");
      boolean questionBased = queueJson.getBoolean("is_question_based");
      String status = queueJson.getString("status");

      // Check if there are any students, then parse
      Object studentsObject = queueJson.get("students");
      if (studentsObject != JSONObject.NULL) {
        JSONArray students = queueJson.getJSONArray("students");

        studentSet = new ArrayList<QueueStudent>();
        for (int i = 0; i < students.length(); i++) {
          QueueStudent student = parseStudent(students.getJSONObject(i));
          studentSet.add(student);
        }
      }

      Object tasObject = queueJson.get("tas");
      if (tasObject != JSONObject.NULL) {
        JSONArray tas = queueJson.getJSONArray("tas");

        taSet = new ArrayList<QueueTA>();
        for (int i = 0; i < tas.length(); i++) {
          JSONObject ta = tas.getJSONObject(i);

          String id = ta.getString("id");

          QueueStudent student = null;
          Object studentObject = ta.get("student");
          if (studentObject != JSONObject.NULL) {
            JSONObject studentJson = ta.getJSONObject("student");
            student = parseStudent(studentJson);
          }
          String username = ta.getString("username");

          taSet.add(new QueueTA(id, student, username));
        }
      }
      QueueData queue = new QueueData(active, frozen, queueId, questionBased, status, studentSet, taSet);

      mOnQueueInformationReceivedListener.onQueueInformationReceived(queue);
    }
    catch (JSONException e) {
      Log.e("QueueRequest", "Error parsing response json");
      e.printStackTrace();
    }
  }

  /**
   * Helper method for parsing a student from the queue.
   * @throws JSONException
   */
  private QueueStudent parseStudent(JSONObject student) throws JSONException {
    String id = student.getString("id");
    boolean inQueue = student.getBoolean("in_queue");
    String location = student.getString("location");
    String question = student.getString("question");
    String taId = student.getString("ta_id");
    String username = student.getString("username");

    return new QueueStudent(id, inQueue, location, question, taId, username);
  }


  /***************************** LISTENERS *****************************/


  public interface OnQueueInformationReceivedListener {
    public void onQueueInformationReceived(QueueData queue);
  }

  private OnQueueInformationReceivedListener mOnQueueInformationReceivedListener;

  public void setOnQueueInformationReceivedListener(OnQueueInformationReceivedListener onQueueInformationReceivedListener) {
    mOnQueueInformationReceivedListener = onQueueInformationReceivedListener;
  }
}




Java Source Code List

com.familybiz.greg.taqueue.ApplicationTest.java
com.familybiz.greg.taqueue.MainActivity.java
com.familybiz.greg.taqueue.MoreInformationActivity.java
com.familybiz.greg.taqueue.NetworkTestFragment.java
com.familybiz.greg.taqueue.SettingsActivity.java
com.familybiz.greg.taqueue.model.Instructor.java
com.familybiz.greg.taqueue.model.School.java
com.familybiz.greg.taqueue.model.StudentQueue.java
com.familybiz.greg.taqueue.model.Student.java
com.familybiz.greg.taqueue.model.TA.java
com.familybiz.greg.taqueue.model.User.java
com.familybiz.greg.taqueue.model.queue.QueueData.java
com.familybiz.greg.taqueue.model.queue.QueueStudent.java
com.familybiz.greg.taqueue.model.queue.QueueTA.java
com.familybiz.greg.taqueue.network.CustomStringRequest.java
com.familybiz.greg.taqueue.network.NetworkRequest.java
com.familybiz.greg.taqueue.network.QueueRequest.java
com.familybiz.greg.taqueue.network.SchoolRequest.java
com.familybiz.greg.taqueue.network.StudentRequest.java
com.familybiz.greg.taqueue.network.TARequest.java
com.familybiz.greg.taqueue.view.lists.InstructorListFragment.java
com.familybiz.greg.taqueue.view.lists.ListFragment.java
com.familybiz.greg.taqueue.view.lists.QueueListFragment.java
com.familybiz.greg.taqueue.view.lists.SchoolListFragment.java
com.familybiz.greg.taqueue.view.login.LoginFragment.java
com.familybiz.greg.taqueue.view.login.StudentLoginFragment.java
com.familybiz.greg.taqueue.view.login.TALoginFragment.java
com.familybiz.greg.taqueue.view.queue.ColorableStudentArrayAdapter.java
com.familybiz.greg.taqueue.view.queue.ColorableTAArrayAdapter.java
com.familybiz.greg.taqueue.view.queue.QueueFragment.java
com.familybiz.greg.taqueue.view.queue.StudentQueueFragment.java
com.familybiz.greg.taqueue.view.queue.TAQueueFragment.java