Example usage for com.google.gson JsonObject addProperty

List of usage examples for com.google.gson JsonObject addProperty

Introduction

In this page you can find the example usage for com.google.gson JsonObject addProperty.

Prototype

public void addProperty(String property, Character value) 

Source Link

Document

Convenience method to add a char member.

Usage

From source file:app.abhijit.iter.data.source.remote.IterApi.java

License:Open Source License

public static String fetchStudentSubjects(String studentId) throws Exception {
    JsonObject request = new JsonObject();
    request.addProperty("sid", "attendance");
    request.addProperty("instituteid", INSTITUTE_ID);
    request.addProperty("registrationid", REGISTRATION_ID);
    request.addProperty("studentid", studentId);
    return Http.post(API_ENDPOINT, "jdata=" + request.toString());
}

From source file:app.abhijit.iter.data.source.StudentDataSource.java

License:Open Source License

public void fetch() {
    String selectedRegistrationNumber = mLocalStore.getString(SELECTED_REGISTRATION_NUMBER_KEY, null);
    if (!StringUtils.equals(selectedRegistrationNumber, mSelectedRegistrationNumber)) {
        if (mFetchStudentAsyncTask != null) {
            mFetchStudentAsyncTask.cancel(true);
            mFetchStudentAsyncTask = null;
        }/*  www .  j a  v a2  s .co  m*/
        if (mFetchStudentSubjectsAsyncTask != null) {
            mFetchStudentSubjectsAsyncTask.cancel(true);
            mFetchStudentSubjectsAsyncTask = null;
        }
        mSelectedRegistrationNumber = selectedRegistrationNumber;
    }

    if (mSelectedRegistrationNumber != null && mErrorMessage == null && mFetchStudentAsyncTask == null
            && mFetchStudentSubjectsAsyncTask == null) {
        if (!mLocalStore.contains(mSelectedRegistrationNumber)) {
            JsonObject student = new JsonObject();
            student.addProperty(STUDENT_REGISTRATION_NUMBER_KEY, mSelectedRegistrationNumber);
            mLocalStore.edit().putString(mSelectedRegistrationNumber, student.toString()).apply();
            mFetchStudentAsyncTask = new FetchStudentAsyncTask();
            mFetchStudentAsyncTask.execute();
        } else {
            JsonObject student = mJsonParser.parse(mLocalStore.getString(mSelectedRegistrationNumber, null))
                    .getAsJsonObject();
            if (!student.has(STUDENT_NAME_KEY)) {
                mFetchStudentAsyncTask = new FetchStudentAsyncTask();
                mFetchStudentAsyncTask.execute();
            } else {
                boolean isSubjectDataOld = (new Date().getTime()
                        - student.get(STUDENT_TIMESTAMP_KEY).getAsLong()) > CACHE_TIMEOUT;
                if (isSubjectDataOld || !student.has(STUDENT_SUBJECTS_KEY)) {
                    String studentId = student.get(STUDENT_ID_KEY).getAsString();
                    mFetchStudentSubjectsAsyncTask = new FetchStudentSubjectsAsyncTask();
                    mFetchStudentSubjectsAsyncTask.execute(studentId);
                }
            }
        }
    }

    if (mErrorMessage != null) {
        EventBus.getDefault().post(new Error(mErrorMessage));
        mErrorMessage = null;
    }

    ArrayList<Student> students = new ArrayList<>();

    for (Map.Entry<String, ?> entry : mLocalStore.getAll().entrySet()) {
        if (entry.getKey().equals(SELECTED_REGISTRATION_NUMBER_KEY))
            continue;
        JsonObject student = mJsonParser.parse(mLocalStore.getString(entry.getKey(), null)).getAsJsonObject();
        students.add(generateStudent(student));
    }

    EventBus.getDefault().post(ImmutableList.copyOf(students));
}

From source file:app.abhijit.iter.data.source.StudentDataSource.java

License:Open Source License

public void refresh() {
    if (mSelectedRegistrationNumber != null && mFetchStudentAsyncTask == null
            && mFetchStudentSubjectsAsyncTask == null) {
        JsonObject student = mJsonParser.parse(mLocalStore.getString(mSelectedRegistrationNumber, null))
                .getAsJsonObject();/*from  w w  w. j av a 2s.c  o m*/
        student.addProperty(STUDENT_TIMESTAMP_KEY, 0);
        mLocalStore.edit().putString(mSelectedRegistrationNumber, student.toString()).apply();
    }
    fetch();
}

From source file:app.abhijit.iter.data.source.StudentDataSource.java

License:Open Source License

private void processStudent(String[] response) {
    mFetchStudentAsyncTask = null;//from ww  w .ja v a 2 s  . co m

    if (response[0] == null || response[1] == null) {
        mErrorMessage = "Could not load registration number " + mSelectedRegistrationNumber;
        delete(mSelectedRegistrationNumber);
        fetch();
        return;
    }

    String studentId = response[0];

    if (studentId.equals("0")) {
        mErrorMessage = "Registration number " + mSelectedRegistrationNumber + " is not valid";
        delete(mSelectedRegistrationNumber);
        fetch();
        return;
    }

    JsonObject student = new JsonObject();

    try {
        JsonObject studentDetails = mJsonParser.parse(response[1]).getAsJsonArray().get(0).getAsJsonObject();
        if (!studentDetails.has("name")) {
            throw new Exception();
        }
        student.addProperty(STUDENT_ID_KEY, studentId);
        student.addProperty(STUDENT_NAME_KEY, studentDetails.get("name").getAsString());
        student.addProperty(STUDENT_REGISTRATION_NUMBER_KEY, mSelectedRegistrationNumber);
        student.addProperty(STUDENT_TIMESTAMP_KEY, new Date().getTime());
    } catch (Exception e) {
        mErrorMessage = "Could not load registration number " + mSelectedRegistrationNumber;
        delete(mSelectedRegistrationNumber);
        fetch();
        return;
    }

    mLocalStore.edit().putString(mSelectedRegistrationNumber, student.toString()).apply();

    fetch();
}

From source file:app.abhijit.iter.data.source.StudentDataSource.java

License:Open Source License

private void processStudentSubjects(String response) {
    mFetchStudentSubjectsAsyncTask = null;

    JsonObject student = mJsonParser.parse(mLocalStore.getString(mSelectedRegistrationNumber, null))
            .getAsJsonObject();/*from ww w  .j  a  v a  2s .co  m*/
    JsonObject subjects = new JsonObject();

    try {
        JsonArray studentSubjects = mJsonParser.parse(response).getAsJsonArray();
        for (int i = 0; i < studentSubjects.size(); i++) {
            if (!studentSubjects.get(i).getAsJsonObject().has("subjectcode")
                    || !studentSubjects.get(i).getAsJsonObject().has("subject")
                    || !studentSubjects.get(i).getAsJsonObject().has("totalpresentclass")
                    || !studentSubjects.get(i).getAsJsonObject().has("totalclasses")) {
                continue;
            }
            String code = studentSubjects.get(i).getAsJsonObject().get("subjectcode").getAsString();
            String name = studentSubjects.get(i).getAsJsonObject().get("subject").getAsString();
            int presentClasses = studentSubjects.get(i).getAsJsonObject().get("totalpresentclass").getAsInt();
            int totalClasses = studentSubjects.get(i).getAsJsonObject().get("totalclasses").getAsInt();
            if (totalClasses <= 0 || presentClasses < 0 || presentClasses > totalClasses)
                continue;
            long lastUpdated = new Date().getTime();
            int oldPresentClasses = presentClasses;
            int oldTotalClasses = totalClasses;
            if (student.has(STUDENT_SUBJECTS_KEY)
                    && student.get(STUDENT_SUBJECTS_KEY).getAsJsonObject().has(code)) {
                oldPresentClasses = student.get(STUDENT_SUBJECTS_KEY).getAsJsonObject().get(code)
                        .getAsJsonObject().get(STUDENT_SUBJECT_PRESENT_CLASSES_KEY).getAsInt();
                oldTotalClasses = student.get(STUDENT_SUBJECTS_KEY).getAsJsonObject().get(code)
                        .getAsJsonObject().get(STUDENT_SUBJECT_TOTAL_CLASSES_KEY).getAsInt();
                if (presentClasses == oldPresentClasses && totalClasses == oldTotalClasses) {
                    lastUpdated = student.get(STUDENT_SUBJECTS_KEY).getAsJsonObject().get(code)
                            .getAsJsonObject().get(STUDENT_SUBJECT_LAST_UPDATED_KEY).getAsLong();
                }
            }
            JsonObject subject = new JsonObject();
            subject.addProperty(STUDENT_SUBJECT_CODE_KEY, code);
            subject.addProperty(STUDENT_SUBJECT_NAME_KEY, name);
            subject.addProperty(STUDENT_SUBJECT_PRESENT_CLASSES_KEY, presentClasses);
            subject.addProperty(STUDENT_SUBJECT_TOTAL_CLASSES_KEY, totalClasses);
            subject.addProperty(STUDENT_SUBJECT_OLD_PRESENT_CLASSES_KEY, oldPresentClasses);
            subject.addProperty(STUDENT_SUBJECT_OLD_TOTAL_CLASSES_KEY, oldTotalClasses);
            subject.addProperty(STUDENT_SUBJECT_LAST_UPDATED_KEY, lastUpdated);
            subjects.add(code, subject);
        }
    } catch (Exception e) {
        if (student.has(STUDENT_SUBJECTS_KEY)) {
            subjects = student.get(STUDENT_SUBJECTS_KEY).getAsJsonObject();
        } else {
            subjects = new JsonObject();
        }
    }

    student.add(STUDENT_SUBJECTS_KEY, subjects);
    student.addProperty(STUDENT_TIMESTAMP_KEY, new Date().getTime());
    mLocalStore.edit().putString(mSelectedRegistrationNumber, student.toString()).apply();

    fetch();
}

From source file:app.abhijit.iter.data.source.StudentDataSource.java

License:Open Source License

private void removeOldSubjectData(String registrationNumber, JsonObject student, JsonObject subject) {
    String code = subject.get(STUDENT_SUBJECT_CODE_KEY).getAsString();
    int presentClasses = subject.get(STUDENT_SUBJECT_PRESENT_CLASSES_KEY).getAsInt();
    int totalClasses = subject.get(STUDENT_SUBJECT_TOTAL_CLASSES_KEY).getAsInt();
    subject.addProperty(STUDENT_SUBJECT_OLD_PRESENT_CLASSES_KEY, presentClasses);
    subject.addProperty(STUDENT_SUBJECT_OLD_TOTAL_CLASSES_KEY, totalClasses);
    student.get(STUDENT_SUBJECTS_KEY).getAsJsonObject().add(code, subject);
    mLocalStore.edit().putString(registrationNumber, student.toString()).apply();
}

From source file:aproject.App.java

License:Apache License

public static JsonObject main(JsonObject args) {
    String name = "stranger";
    if (args.has("name"))
        name = args.getAsJsonPrimitive("name").getAsString();
    JsonObject response = new JsonObject();
    response.addProperty("greeting", "Hello " + name + "!");
    return response;
}

From source file:assignment3.Assignment3.java

public static String getJSON() {
    String output = "";
    try {/*from   w ww .j av a 2s .co m*/
        JsonArray arr = new JsonArray();
        Connection conn = getConnection();
        Statement stmt = conn.createStatement();
        ResultSet rs = stmt.executeQuery("SELECT * FROM students");
        while (rs.next()) {
            JsonObject obj = new JsonObject();
            obj.addProperty("id", rs.getInt("id"));
            obj.addProperty("fName", rs.getString("fName"));
            obj.addProperty("lName", rs.getString("lName"));
            obj.addProperty("bio", rs.getString("bio"));
            arr.add(obj);
        }
        output = arr.toString();
        conn.close();
    } catch (SQLException ex) {
        output = "SQL Exception Error: " + ex.getMessage();
    }
    return output;
}

From source file:assignment3.Assignment3.java

public static String getJSON(int id) {
    String output = "";
    try {/* www.j  ava2  s.co m*/
        JsonArray arr = new JsonArray();
        Connection conn = getConnection();
        String query = "SELECT * FROM students WHERE id = ?";
        PreparedStatement pstmt = conn.prepareStatement(query);
        pstmt.setInt(1, id);
        ResultSet rs = pstmt.executeQuery();
        while (rs.next()) {
            JsonObject obj = new JsonObject();
            obj.addProperty("id", rs.getInt("id"));
            obj.addProperty("fName", rs.getString("fName"));
            obj.addProperty("lName", rs.getString("lName"));
            obj.addProperty("bio", rs.getString("bio"));
            arr.add(obj);
        }
        output = arr.toString();
        conn.close();
    } catch (SQLException ex) {
        output = "SQL Exception Error: " + ex.getMessage();
    }
    return output;
}

From source file:at.gv.egovernment.moa.id.protocols.oauth20.json.OAuthJsonToken.java

License:EUPL

@Override
public JsonObject getHeader() {
    JsonObject header = new JsonObject();
    header.addProperty(ALGORITHM_HEADER, signer.getOAuthSignatureAlgorithm().getAlgorithm());
    String keyId = getKeyId();//w w w  . j  a  va2 s.  c o  m
    if (keyId != null) {
        header.addProperty(KEY_ID_HEADER, keyId);
    }
    return header;
}