Example usage for com.google.gson.reflect TypeToken TypeToken

List of usage examples for com.google.gson.reflect TypeToken TypeToken

Introduction

In this page you can find the example usage for com.google.gson.reflect TypeToken TypeToken.

Prototype

@SuppressWarnings("unchecked")
protected TypeToken() 

Source Link

Document

Constructs a new type literal.

Usage

From source file:amadeuslms.amadeus.events.NewMessageEvent.java

License:Open Source License

public NewMessageEvent(final Map<String, String> data) {
    Type type = new TypeToken<MessageResponse>() {
    }.getType();/*from   w  w  w.  ja va2 s  .c o  m*/

    this.response = new Gson().fromJson(data.get("response").toString(), type);
}

From source file:amadeuslms.amadeus.services.MessagingService.java

License:Open Source License

private void setNotification(RemoteMessage remoteMessage) {
    final NotificationCompat.Builder builder = new NotificationCompat.Builder(this);

    Map<String, String> data = remoteMessage.getData();

    Type type = new TypeToken<MessageResponse>() {
    }.getType();//ww w  . java  2  s .  c om

    MessageResponse response = new Gson().fromJson(data.get("response").toString(), type);

    String user_img = data.get("user_img").toString();

    builder.setTicker(data.get("title").toString());
    builder.setContentTitle(data.get("user_name").toString());
    builder.setContentText(data.get("body").toString());
    builder.setSmallIcon(R.drawable.ic_logo_vector_white);
    builder.setAutoCancel(true);

    if (!TextUtils.isEmpty(user_img)) {
        String path = TokenCacheController.getTokenCache(this).getWebserver_url() + user_img;

        try {
            builder.setLargeIcon(Picasso.with(this).load(path).get());
        } catch (IOException e) {
            System.out.println("Error: " + e.getMessage());
        }
    } else {
        try {
            final InputStream is = this.getAssets().open("images/no_image.png");

            Bitmap bmp = BitmapFactory.decodeStream(is);

            builder.setLargeIcon(bmp);
        } catch (IOException e) {
            System.out.println("Error: " + e.getMessage());
        }
    }

    long[] pattern = { 500, 500, 500, 500, 500, 500, 500, 500, 500 };

    builder.setVibrate(pattern);

    builder.setVisibility(NotificationCompat.VISIBILITY_PUBLIC);
    builder.setPriority(NotificationCompat.PRIORITY_HIGH);

    Intent intent = new Intent(this, ChatActivity.class);
    intent.putExtra(ChatActivity.USER_TO, response.getData().getMessage_sent().getUser());
    intent.putExtra(ChatActivity.SUBJECT, response.getData().getMessage_sent().getSubject());
    intent.putExtra("FROM_NOTIFICATION", true);

    PendingIntent pendingIntent = PendingIntent.getActivity(this, (int) System.currentTimeMillis(), intent,
            PendingIntent.FLAG_UPDATE_CURRENT);
    builder.setContentIntent(pendingIntent);

    NotificationManager notificationManager = (NotificationManager) getSystemService(
            Context.NOTIFICATION_SERVICE);
    notificationManager.notify(6534, builder.build());
}

From source file:androhashcheck.MainFrame.java

private void getUserTasks() {
    updateStatus("getting user tasks");
    new Thread(new Runnable() {

        @Override//from   ww w .  j a  v a2 s  .  co  m
        public void run() {
            String url = ConfigClass.serverURL + "/api/tasks";
            try {
                URL obj = new URL(url);
                HttpURLConnection con = (HttpURLConnection) obj.openConnection();
                con.setRequestMethod("GET");
                con.setRequestProperty("token", ConfigClass.token);
                int responseCode = con.getResponseCode();
                BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
                String inputLine;
                StringBuilder response = new StringBuilder();

                while ((inputLine = in.readLine()) != null) {
                    response.append(inputLine);
                }
                in.close();
                //print result
                System.out.println(response.toString());
                Gson gson = new Gson();
                Map<String, Task[]> map = new Gson().fromJson(response.toString(),
                        new TypeToken<HashMap<String, Task[]>>() {
                        }.getType());
                Task[] tasks = map.get("tasks");
                taskList = new ArrayList<>();
                for (Task task : tasks) {
                    TaskObject taskObject = new TaskObject();
                    taskObject.setFileName(task.getApk_name());
                    taskObject.setTask(task);
                    taskList.add(taskObject);
                }
                updateStatus("updated user tasks");
            } catch (IOException | JsonSyntaxException ignorable) {
                listModel.addElement("unable to get user tasks");
            }
        }
    }).start();
}

From source file:api.ApiLogHelper.java

License:Open Source License

/**
 * Error message info concat//  w w w .j a v a  2  s.  co  m
 *
 * @noinspection WeakerAccess
 * @param header       - custom error message header
 * @param coordinates  - process coordinates - schedule, task, node...
 * @param response       - ApiResponse instance. Containing http response code, response, stack trace, etc
 * @return String      - returning concatinated message
 */
public static String getMessage(String header, Map<String, String> coordinates, ApiResponse response) {
    String footer = "Schedule id: "
            + ((coordinates.get("scheduleId") == null) ? "NONE" : coordinates.get("scheduleId")) + "\n"
            + "Task name: " + ((coordinates.get("taskName") == null) ? "NONE" : coordinates.get("taskName"))
            + "\n" + "Node id: " + ((coordinates.get("nodeId") == null) ? "NONE" : coordinates.get("nodeId"))
            + "\n" + "Worker id: "
            + ((coordinates.get("workerId") == null) ? "NONE" : coordinates.get("workerId")) + "\n"
            + "API method: " + response.apiMethod + "\n";

    if (response.responseCode > 0) {
        footer += "Response code: " + response.responseCode + "\n";
    }

    if (!response.success) {
        /*
         * Trying to get message from bad API response
         */
        try {
            Gson gson = new Gson();
            Type responseType = new TypeToken<HashMap<String, String>>() {
            }.getType();
            Map<String, String> responseBody;
            responseBody = gson.fromJson(response.response, responseType);
            if (responseBody.get("message") != null) {
                footer += "Response message: " + responseBody.get("message") + "\n";
            }
        } catch (Exception e) {
            // no action - yii response json no found
        }
    }

    if (response.exception.length() > 0) {
        footer += "Exception: " + response.exception + "\n";
    }

    if (response.stackTrace.length() > 0) {
        footer += "Stack trace: \n" + response.stackTrace + "\n";
    }

    return header + "\n" + footer;

}

From source file:api.Ulti.java

License:Open Source License

/**
 * Queries for summoners by their summoner names. A maximum of 40 summoner names can be provided.
 *
 * @param   championNames the names of summoners to lookup
 * @return  a {@link java.util.Map} with keys being the summoner names as a {@link String} and values being
 *          a {@link dto.summoner.Summoner} object
 *//*w w  w . ja va2  s.co  m*/
public Map<String, Summoner> getSummonerByName(String... championNames) {
    if (!passLengthConstraint(championNames, 40)) {
        throw new UltiException(UltiException.Type.INVALID_PARAMETERS);
    }

    Reader json = api.query(versions.get("summoner") + "/summoner/by-name/" + prepare(championNames));
    Type type = new TypeToken<Map<String, Summoner>>() {
    }.getType();

    return gson.fromJson(json, type);
}

From source file:api.Ulti.java

License:Open Source License

/**
 * Queries for summoners by their summoner IDs. A maximum of 40 summoner IDs can be provided.
 *
 * @param   summonerIds the IDs of the summoners to lookup
 * @return  a {@link java.util.Map} with keys being the summoner IDs as a {@link String} and values being
 *          a {@link dto.summoner.Summoner} object
 *//*  w  w w . j a v  a  2 s.com*/
public Map<String, Summoner> getSummonerById(Long... summonerIds) {
    if (!passLengthConstraint(summonerIds, 40)) {
        throw new UltiException(UltiException.Type.INVALID_PARAMETERS);
    }

    Reader json = api.query(versions.get("summoner") + "/summoner/" + prepare(summonerIds));
    Type type = new TypeToken<Map<String, Summoner>>() {
    }.getType();

    return gson.fromJson(json, type);
}

From source file:api.Ulti.java

License:Open Source License

/**
 * Queries for all the masteries pages of the given summoners. A maximum of 40 summoner IDs can be provided.
 *
 * @param   summonerIds the IDs of the summoners to lookup
 * @return  a {@link java.util.Map} of {@link dto.summoner.MasteryPages} with summoner ID keys
 *          as a {@link java.lang.String}
 *//*from   w  ww.  ja  va 2  s .co  m*/
public Map<String, MasteryPages> getMasteriesBySummonerId(Long... summonerIds) {
    if (!passLengthConstraint(summonerIds, 40)) {
        throw new UltiException(UltiException.Type.INVALID_PARAMETERS);
    }

    Reader json = api.query(versions.get("summoner") + "/summoner/" + prepare(summonerIds) + "/masteries");
    Type type = new TypeToken<Map<String, MasteryPages>>() {
    }.getType();

    return gson.fromJson(json, type);
}

From source file:api.Ulti.java

License:Open Source License

/**
 * Queries for a summoners name by their ID.
 *
 * @param   summonerIds the ID of the summoner to lookup
 * @return  a {@link java.util.Map} of summoner names as a {@link java.lang.String} with their summoner ID
 *          as the key/*from   w w  w  . j a va 2 s.co  m*/
 */
public Map<String, String> getSummonerNameById(Long... summonerIds) {
    Reader json = api.query(versions.get("summoner") + "/summoner/" + prepare(summonerIds) + "/name");
    Type type = new TypeToken<Map<String, String>>() {
    }.getType();

    return gson.fromJson(json, type);
}

From source file:api.Ulti.java

License:Open Source License

/**
 * Queries for all the rune pages of the given summoner IDs. A maximum of 40 summoner IDs can be provided.
 *
 * @param   ids the ID of the summoners to lookup
 * @return  a {@link java.util.Map} of {@link dto.summoner.RunePages} with summoner ID keys
 *          as a {@link java.lang.String}
 *///from w w w  .j a  va 2s  . c om
public Map<String, RunePages> getRunePagesBySummonerId(Long... ids) {
    if (!passLengthConstraint(ids, 40)) {
        throw new UltiException(UltiException.Type.INVALID_PARAMETERS);
    }

    Reader json = api.query(versions.get("summoner") + "/summoner/" + prepare(ids) + "/runes");
    Type type = new TypeToken<Map<String, RunePages>>() {
    }.getType();

    return gson.fromJson(json, type);
}

From source file:api.Ulti.java

License:Open Source License

/**
 * Queries for the teams of each summoner provided. A maximum of 40 summoner IDs can be provided.
 *
 * @param   summonerIds the IDs of the summoners to lookup
 * @return  a {@link java.util.Map} containing a {@link java.util.List} of every {@link dto.team.Team} the
 *          summoner is part of, with the key being the summoner ID as a {@link String}
 *//*from   ww  w. ja  va2s .  c  om*/
public Map<String, List<Team>> getTeamBySummonerId(Long... summonerIds) {
    if (!passLengthConstraint(summonerIds, 10)) {
        throw new UltiException(UltiException.Type.INVALID_PARAMETERS);
    }

    Reader json = api.query(versions.get("team") + "/team/by-summoner/" + prepare(summonerIds));
    Type type = new TypeToken<Map<String, List<Team>>>() {
    }.getType();

    return gson.fromJson(json, type);
}