List of usage examples for com.squareup.okhttp Response code
int code
To view the source code for com.squareup.okhttp Response code.
Click Source Link
From source file:io.gameup.android.GameUpSession.java
License:Apache License
/** * Perform a key-value storage write operation, storing data as JSON. Data * is private per-user and per-game.//from w w w .j ava2 s . c o m * * NOTE: This is not designed to store confidential data, such as payment * information etc. * * @param apiKey The API key to use. * @param key The key to store the given data under. * @param value The object to serialise and store. * @throws IOException when a network or communication error occurs. */ public void storagePut(final @NonNull String apiKey, final @NonNull String key, final @NonNull Object value) throws IOException { final Request request = RequestFactory.put( new Uri.Builder().scheme(GameUp.SCHEME).encodedAuthority(GameUp.API_SERVER).appendPath("v0") .appendPath("gamer").appendPath("storage").appendPath(key).build(), apiKey, token, GsonFactory.get().toJson(value)); final Response response = OkHttpClientFactory.getClient().newCall(request).execute(); if (response.code() != HttpURLConnection.HTTP_NO_CONTENT) { throw new IOException("Operation returned HTTP " + response.code()); } }
From source file:io.gameup.android.GameUpSession.java
License:Apache License
/** * Perform a key-value storage read operation. * * @param apiKey The API key to use.//from w ww. j a va2 s . c o m * @param key The key to attempt to read data from. * @param type The class literal to attempt to deserialise as. * @return The entity requested, or null if there was no data. * @throws IOException when a network or communication error occurs. */ public <T> T storageGet(final @NonNull String apiKey, final @NonNull String key, final @NonNull Class<T> type) throws IOException { Reader in = null; try { final Request request = RequestFactory.get( new Uri.Builder().scheme(GameUp.SCHEME).encodedAuthority(GameUp.API_SERVER).appendPath("v0") .appendPath("gamer").appendPath("storage").appendPath(key).build(), apiKey, token); final Response response = OkHttpClientFactory.getClient().newCall(request).execute(); switch (response.code()) { case HttpURLConnection.HTTP_OK: in = response.body().charStream(); try { final StorageGetWrapper responseWrapper = GsonFactory.get().fromJson(in, StorageGetWrapper.class); return GsonFactory.get().fromJson(responseWrapper.getValue(), type); } catch (final JsonParseException e) { throw new IOException("Response data does not match expected entity"); } case HttpURLConnection.HTTP_NOT_FOUND: return null; default: throw new IOException("Operation returned HTTP " + response.code()); } } finally { Utils.closeQuietly(in); } }
From source file:io.gameup.android.GameUpSession.java
License:Apache License
/** * Perform a key-value storage delete operation. Will silently ignore absent * data.//from w w w . java2s . co m * * @param apiKey The API key to use. * @param key The key to delete data from. * @throws IOException when a network or communication error occurs. */ public void storageDelete(final @NonNull String apiKey, final @NonNull String key) throws IOException { final Request request = RequestFactory .delete(new Uri.Builder().scheme(GameUp.SCHEME).encodedAuthority(GameUp.API_SERVER).appendPath("v0") .appendPath("gamer").appendPath("storage").appendPath(key).build(), apiKey, token); final Response response = OkHttpClientFactory.getClient().newCall(request).execute(); if (response.code() != HttpURLConnection.HTTP_NO_CONTENT) { throw new IOException("Operation returned HTTP " + response.code()); } }
From source file:io.gameup.android.GameUpSession.java
License:Apache License
/** * Report progress towards a given achievement. * * @param apiKey The API key to use.// w ww. j a va2s . com * @param achievementId The internal Achievement ID to interact with. * @param count The progress amount to report. * @return An Achievement instance if this call results in an achievement * being completed or progress is reported, null otherwise. * @throws IOException when a network or communication error occurs. */ public Achievement achievement(final @NonNull String apiKey, final @NonNull String achievementId, final int count) throws IOException { final Request request = RequestFactory.post( new Uri.Builder().scheme(GameUp.SCHEME).encodedAuthority(GameUp.API_SERVER).appendPath("v0") .appendPath("gamer").appendPath("achievement").appendPath(achievementId).build(), apiKey, token, GsonFactory.get().toJson(new AchievementProgress(count))); final Response response = OkHttpClientFactory.getClient().newCall(request).execute(); Reader in = null; try { switch (response.code()) { case HttpURLConnection.HTTP_OK: in = response.body().charStream(); try { return GsonFactory.get().fromJson(in, Achievement.class); } catch (final JsonParseException e) { throw new IOException("Response data does not match expected entity"); } case HttpURLConnection.HTTP_NO_CONTENT: return null; default: throw new IOException("Operation returned HTTP " + response.code()); } } finally { Utils.closeQuietly(in); } }
From source file:io.gameup.android.GameUpSession.java
License:Apache License
/** * Get a list of achievements available for the game, including any gamer * data such as progress or completed timestamps. * * @param apiKey The API key to use.// w ww . j ava 2s . co m * @return A List containing Achievement instances, may be empty if none are * returned for the current game. * @throws IOException when a network or communication error occurs. */ public AchievementList achievement(final @NonNull String apiKey) throws IOException { Reader in = null; try { final Request request = RequestFactory .get(new Uri.Builder().scheme(GameUp.SCHEME).encodedAuthority(GameUp.API_SERVER) .appendPath("v0").appendPath("gamer").appendPath("achievement").build(), apiKey, token); final Response response = OkHttpClientFactory.getClient().newCall(request).execute(); if (response.code() != HttpURLConnection.HTTP_OK) { throw new IOException("Operation returned HTTP " + response.code()); } in = response.body().charStream(); try { return GsonFactory.get().fromJson(in, AchievementList.class); } catch (final JsonParseException e) { throw new IOException("Response data does not match expected entity"); } } finally { Utils.closeQuietly(in); } }
From source file:io.gameup.android.GameUpSession.java
License:Apache License
/** * Submit a new score to the specified leaderboard. The new score will only * overwrite any previously submitted value if it's "better" according to * the sorting rules of the leaderboard, but updated ranking details are * returned in all cases.// www .ja va2 s.c om * * @param apiKey The API key to use. * @param leaderboardId The private ID of the leaderboard to submit to. * @param score The score to submit. * @return A Rank instance containing updated detailed rank data for the * current gamer. * @throws IOException when a network or communication error occurs. */ public Rank leaderboard(final @NonNull String apiKey, final @NonNull String leaderboardId, final long score) throws IOException { final Request request = RequestFactory.post( new Uri.Builder().scheme(GameUp.SCHEME).encodedAuthority(GameUp.API_SERVER).appendPath("v0") .appendPath("gamer").appendPath("leaderboard").appendPath(leaderboardId).build(), apiKey, token, GsonFactory.get().toJson(new LeaderboardSubmission(score))); final Response response = OkHttpClientFactory.getClient().newCall(request).execute(); Reader in = null; try { switch (response.code()) { case HttpURLConnection.HTTP_OK: in = response.body().charStream(); try { return GsonFactory.get().fromJson(in, Rank.class); } catch (final JsonParseException e) { throw new IOException("Response data does not match expected entity"); } default: throw new IOException("Operation returned HTTP " + response.code()); } } finally { Utils.closeQuietly(in); } }
From source file:io.gameup.android.GameUpSession.java
License:Apache License
/** * Request leaderboard metadata, the current top ranked gamers, and the * current gamer's detailed ranking on a specified leaderboard. * * @param apiKey The API key to use.// w w w . j a va2 s. com * @param leaderboardId The private ID of the leaderboard to request. * @return A corresponding LeaderboardAndRank instance. * @throws IOException when a network or communication error occurs. */ public LeaderboardAndRank leaderboard(final @NonNull String apiKey, final @NonNull String leaderboardId) throws IOException { Reader in = null; try { final Request request = RequestFactory.get( new Uri.Builder().scheme(GameUp.SCHEME).encodedAuthority(GameUp.API_SERVER).appendPath("v0") .appendPath("gamer").appendPath("leaderboard").appendPath(leaderboardId).build(), apiKey, token); final Response response = OkHttpClientFactory.getClient().newCall(request).execute(); if (response.code() != HttpURLConnection.HTTP_OK) { throw new IOException("Operation returned HTTP " + response.code()); } in = response.body().charStream(); try { return GsonFactory.get().fromJson(in, LeaderboardAndRank.class); } catch (final JsonParseException e) { throw new IOException("Response data does not match expected entity"); } } finally { Utils.closeQuietly(in); } }
From source file:io.github.hidroh.materialistic.accounts.UserServicesClient.java
License:Apache License
@Override public void submit(Context context, final String title, final String content, final boolean isUrl, final Callback callback) { Pair<String, String> credentials = AppUtils.getCredentials(context); if (credentials == null) { callback.onDone(false);//from w w w. ja va2 s . c o m return; } /** * The flow: * POST /submit with acc, pw * if 302 to /login, considered failed * POST /r with fnid, fnop, title, url or text * if 302 to /newest, considered successful * if 302 to /x, considered error, maybe duplicate or invalid input * if 200 or anything else, considered error */ // fetch submit page with given credentials mClient.newCall(new Request.Builder() .url(HttpUrl.parse(BASE_WEB_URL).newBuilder().addPathSegment(SUBMIT_PATH).build()) .post(new FormEncodingBuilder().add(LOGIN_PARAM_ACCT, credentials.first) .add(LOGIN_PARAM_PW, credentials.second).build()) .build()).enqueue(new com.squareup.okhttp.Callback() { @Override public void onFailure(Request request, IOException e) { postError(callback); } @Override public void onResponse(Response response) throws IOException { final boolean redirect = response.code() == HttpURLConnection.HTTP_MOVED_TEMP; if (redirect) { // redirect = failed login postResult(callback, false); } else { // grab fnid from HTML and submit doSubmit(title, content, getInputValue(response.body().string(), SUBMIT_PARAM_FNID), isUrl, callback); } } }); }
From source file:io.github.hidroh.materialistic.accounts.UserServicesClient.java
License:Apache License
@WorkerThread private void doSubmit(String title, String content, String fnid, boolean isUrl, final Callback callback) { if (TextUtils.isEmpty(fnid)) { postError(callback);/*from w ww. j a v a 2 s . c om*/ return; } mClient.newCall(new Request.Builder() .url(HttpUrl.parse(BASE_WEB_URL).newBuilder().addPathSegment(SUBMIT_POST_PATH).build()) .post(new FormEncodingBuilder().add(SUBMIT_PARAM_FNID, fnid).add(SUBMIT_PARAM_FNOP, DEFAULT_FNOP) .add(SUBMIT_PARAM_TITLE, title).add(isUrl ? SUBMIT_PARAM_URL : SUBMIT_PARAM_TEXT, content) .build()) .build()).enqueue(new com.squareup.okhttp.Callback() { @Override public void onFailure(Request request, IOException e) { postError(callback); } @Override public void onResponse(Response response) throws IOException { boolean redirect = response.code() == HttpURLConnection.HTTP_MOVED_TEMP; if (!redirect) { postError(callback); return; } String location = response.header(HEADER_LOCATION); switch (location) { case DEFAULT_SUBMIT_REDIRECT: postResult(callback, true); break; default: postError(callback); break; } } }); }
From source file:io.github.hidroh.materialistic.accounts.UserServicesClient.java
License:Apache License
private com.squareup.okhttp.Callback wrap(final Callback callback) { return new com.squareup.okhttp.Callback() { @Override//from w w w .j a v a 2s .c om public void onFailure(Request request, IOException e) { UserServicesClient.this.postError(callback); } @Override public void onResponse(Response response) throws IOException { // redirect = successful submit postResult(callback, response.code() == HttpURLConnection.HTTP_MOVED_TEMP); } }; }