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:ca.ualberta.cs.team16app.elasitcSearch.ESClient.java

License:Open Source License

/**
 * search by keywords/*w  ww .j  a  v  a2  s . co  m*/
 */
public ArrayList<Claim> searchClaims(String Keyword) {
    try {
        ArrayList<Claim> filteredStories = new ArrayList<Claim>();

        HttpGet searchRequest = new HttpGet(this.getQueryHttpURI(Keyword));

        searchRequest.setHeader("Accept", "application/json");
        HttpResponse response = httpclient.execute(searchRequest);
        String status = response.getStatusLine().toString();
        System.out.println(status);

        String json = getEntityContent(response);

        Type elasticSearchSearchResponseType = new TypeToken<ElasticSearchSearchResponse<Claim>>() {
        }.getType();

        ElasticSearchSearchResponse<Claim> esResponse = gson.fromJson(json, elasticSearchSearchResponseType);
        System.err.println(esResponse);
        for (ElasticSearchResponse<Claim> r : esResponse.getHits()) {
            Claim claim = r.getSource();
            filteredStories.add(claim);
        }
        // searchRequest.releaseConnection();

        return filteredStories;
    } catch (Exception e) {
        return null;
    }
}

From source file:ca.ualberta.cs.team16app.elasitcSearch.ESClient.java

License:Open Source License

/**
 * Given an input claim object, this function checks whether the input claim
 * is present on the WebServer or not.//  www. j  a  v a 2  s . c  o m
 * 
 * @param claim
 * @return boolean claimExists
 */
private boolean claimExists(Claim claim) {
    boolean claimExists = false;

    try {
        HttpGet getRequest = new HttpGet(WEBSERVICE_URI + CLAIM_FOLDER + claim.getClaimId());
        getRequest.addHeader("Accept", "application/json");
        HttpResponse response = httpclient.execute(getRequest);
        String json = getEntityContent(response);

        // We have to tell GSON what type we expect
        Type elasticSearchResponseType = new TypeToken<ElasticSearchResponse<Claim>>() {
        }.getType();
        // Now we expect to get a claim response
        ElasticSearchResponse<Claim> esResponse = gson.fromJson(json, elasticSearchResponseType);
        // We get the claim from it
        claimExists = esResponse.getExtists();
        // getRequest.releaseConnection();
    } catch (ClientProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return claimExists;
}

From source file:ca.ualberta.lixin.lixin1_reflex.GameModeFourPlayers.java

License:Open Source License

private void loadFromFile() {
    try {/*from  w  ww.  j a  v  a2s  .  c  o  m*/
        FileInputStream fis = openFileInput(FILENAME);
        BufferedReader in = new BufferedReader(new InputStreamReader(fis));
        Gson gson = new Gson();
        // https://google-gson.googlecode.com/svn/trunk/gson/docs/javadocs/com
        Type arrayListType = new TypeToken<ArrayList<PlayerStatus>>() {
        }.getType();
        playerResult = gson.fromJson(in, arrayListType);

    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        playerResult = new ArrayList<PlayerStatus>();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        throw new RuntimeException(e);
    }
}

From source file:ca.ualberta.slevinsk.gameshow.BuzzerCounterManager.java

License:Apache License

public Type getTypeToken() {
    return new TypeToken<BuzzerCounterContainer>() {
    }.getType();
}

From source file:ca.ualberta.slevinsk.gameshow.ReactionTimersManager.java

License:Apache License

public static Type getTypeToken() {
    return new TypeToken<ReactionTimerList>() {
    }.getType();
}

From source file:ca.ualberta.trinkettrader.Inventory.Trinket.Pictures.Picture.java

License:Apache License

/**
 * Attempts to find this object on the elasticsearch server. If the object
 * cannot be found then pushes the current version to the server.
 *
 * @param type class of this object//from  ww  w .  ja  v a2s .  c  o  m
 * @throws IOException
 */
@Override
public <T extends ElasticStorable> void getFromNetwork(Class<T> type) throws IOException {
    // Alexis C.; http://stackoverflow.com/questions/27253555/com-google-gson-internal-linkedtreemap-cannot-be-cast-to-my-class; 2015-11-28
    // Android-Droid; http://stackoverflow.com/questions/8120220/how-to-use-parameters-with-httppost; 2015-11-18
    final HttpGet getRequest = new HttpGet(this.getResourceUrl() + this.getUid());
    final HttpClient httpClient = new DefaultHttpClient();
    Thread thread = new Thread(new Runnable() {
        @Override
        public void run() {
            try {
                HttpResponse response = httpClient.execute(getRequest);
                Log.i("HttpResponse", response.getStatusLine().toString());
                Type searchHitType = new TypeToken<SearchHit<Picture>>() {
                }.getType();
                SearchHit<Picture> returned = new Gson()
                        .fromJson(new InputStreamReader(response.getEntity().getContent()), searchHitType);
                onGetResult(returned.getSource());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    });
    thread.start();
}

From source file:ca.ualberta.trinkettrader.Trades.Trade.java

License:Apache License

/**
 * Searches for ElasticStorable objects on the network matching the attribute and attribute
 * value pairs. Calls onSearchResult with the results when the search completes.
 * <p/>// www .jav a  2  s.co  m
 *
 * @param postParameters pairs of attributes to use when searching
 * @param type
 * @throws IOException
 */
@Override
public <T extends ElasticStorable> void searchOnNetwork(ArrayList<NameValuePair> postParameters, Class<T> type)
        throws IOException {
    // Alexis C.; http://stackoverflow.com/questions/27253555/com-google-gson-internal-linkedtreemap-cannot-be-cast-to-my-class; 2015-11-28
    // Android-Droid; http://stackoverflow.com/questions/8120220/how-to-use-parameters-with-httppost; 2015-11-18
    String username = LoggedInUser.getInstance().getProfile().getEmail();
    final HttpGet searchRequest = new HttpGet(this.getSearchUrl() + "?q=\"recieverUsername:" + username
            + " OR senderUsername:" + username + "\"");
    searchRequest.setHeader("Accept", "application/json");

    final HttpClient httpClient = new DefaultHttpClient();
    Thread thread = new Thread(new Runnable() {
        @Override
        public void run() {
            try {
                ArrayList<Trade> result = new ArrayList<>();
                HttpResponse response = httpClient.execute(searchRequest);
                Log.i("HttpResponse", response.getStatusLine().toString());
                Log.i("HttpResponse Body", EntityUtils.toString(response.getEntity(), "UTF-8"));

                Type searchResponseType = new TypeToken<SearchHit<Trade>>() {
                }.getType();
                InputStreamReader streamReader = new InputStreamReader(response.getEntity().getContent());
                SearchResponse<Trade> esResponse = new Gson().fromJson(streamReader, searchResponseType);

                for (SearchHit<Trade> hit : esResponse.getHits().getHits()) {
                    result.add(hit.getSource());
                }

                onSearchResult(result);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    });
    thread.start();
}

From source file:ca.ualberta.trinkettrader.User.LoggedInUser.java

License:Apache License

/**
 * Pass in the context to load the cached data of the user of the current application instance
 * into the LoggedInUser singleton class.
 *
 * @param c//from w  w  w.j a v a2 s  . c o m
 */
// joshua2ua; https://github.com/joshua2ua/lonelyTwitter; 2015-11-26
public void loadFromFile(Context c) {
    try {

        FileInputStream fis = c.openFileInput(FILENAME);
        BufferedReader in = new BufferedReader(new InputStreamReader(fis));
        //https://google-gson.googlecode.com/svn/trunk/gson/docs/javadocs/dcom/google/gson/Gson.html; 2015-09-23
        Type loggedInUserType = new TypeToken<LoggedInUser>() {
        }.getType();
        Gson gson = new Gson();
        ourInstance = gson.fromJson(in, loggedInUserType);

    } catch (FileNotFoundException e) {
        //TODO: Assumption: LoggedInUser instantiated upon login, no file == no previous data
        //TODO: Therefore do nothing
    }
    this.setNeedToSave(Boolean.FALSE);
}

From source file:ca.ualberta.trinkettrader.User.User.java

License:Apache License

/**
 * Attempts to find this object on the elasticsearch server. If the object
 * cannot be found then pushes the current version to the server.  Each object is searched for using
 * its know URL, which is composed of its resource url ({@link User#getResourceUrl()}) and an ID
 * unique to the user associated with the object ({@link User#getUid()}).  Implementation of the
 * {@link ElasticStorable ElasticStorable} method.
 *
 * @param type - class of the object being saved
 * @throws IOException//w w w .  j  ava2 s  .  c  om
 */
@Override
public <T extends ElasticStorable> void getFromNetwork(Class<T> type) throws IOException {
    // Alexis C.; http://stackoverflow.com/questions/27253555/com-google-gson-internal-linkedtreemap-cannot-be-cast-to-my-class; 2015-11-28
    // Android-Droid; http://stackoverflow.com/questions/8120220/how-to-use-parameters-with-httppost; 2015-11-18
    final HttpGet getRequest = new HttpGet(this.getResourceUrl() + this.getUid());
    final HttpClient httpClient = new DefaultHttpClient();
    Thread thread = new Thread(new Runnable() {
        @Override
        public void run() {
            try {
                HttpResponse response = httpClient.execute(getRequest);
                Log.i("HttpResponse", response.getStatusLine().toString());
                Type searchHitType = new TypeToken<SearchHit<LoggedInUser>>() {
                }.getType();
                SearchHit<LoggedInUser> returned = new Gson()
                        .fromJson(new InputStreamReader(response.getEntity().getContent()), searchHitType);
                onGetResult(returned.getSource());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    });
    thread.start();
}

From source file:ca.ualberta.trinkettrader.User.User.java

License:Apache License

public void getFromNetwork() throws IOException {
    // Alexis C.; http://stackoverflow.com/questions/27253555/com-google-gson-internal-linkedtreemap-cannot-be-cast-to-my-class; 2015-11-28
    // Android-Droid; http://stackoverflow.com/questions/8120220/how-to-use-parameters-with-httppost; 2015-11-18
    final HttpGet getRequest = new HttpGet(this.getResourceUrl() + this.getUid());
    final HttpClient httpClient = new DefaultHttpClient();
    Thread thread = new Thread(new Runnable() {
        @Override/*  w w  w .j a  va  2 s  .com*/
        public void run() {
            try {
                HttpResponse response = httpClient.execute(getRequest);
                Log.i("HttpResponse", response.getStatusLine().toString());
                Type searchHitType = new TypeToken<SearchHit<User>>() {
                }.getType();
                SearchHit<User> returned = new Gson()
                        .fromJson(new InputStreamReader(response.getEntity().getContent()), searchHitType);
                onGetResult(returned.getSource());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    });
    thread.start();
}