use of fr.neamar.lolgamedata.volley.NoCacheRetryJsonRequest in project teamward-client by Neamar.
the class ChampionActivity method downloadChampionDetails.
private void downloadChampionDetails(int championId) {
// Instantiate the RequestQueue.
final RequestQueue queue = VolleyQueue.newRequestQueue(this);
NoCacheRetryJsonRequest jsonRequest = new NoCacheRetryJsonRequest(Request.Method.GET, ((LolApplication) getApplication()).getApiUrl() + "/champion/" + championId + "?locale=" + Locale.getDefault().toString(), null, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
displayChampionDetails(response);
Log.i(TAG, "Displaying champion details for " + championName);
queue.stop();
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.e(TAG, error.toString());
findViewById(R.id.progressBar).setVisibility(View.GONE);
queue.stop();
}
});
queue.add(jsonRequest);
}
use of fr.neamar.lolgamedata.volley.NoCacheRetryJsonRequest in project teamward-client by Neamar.
the class CounterChampionsFragment method loadCounters.
private void loadCounters(final RecyclerView recyclerView, final Account account, final String role) {
// Instantiate the RequestQueue.
final RequestQueue queue = VolleyQueue.newRequestQueue(getActivity());
final String summonerName = account.summonerName;
String region = account.region;
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getContext());
final int requiredChampionMastery = Integer.parseInt(prefs.getString("counter_required_mastery", "3"));
try {
String url = ((LolApplication) getActivity().getApplication()).getApiUrl() + "/summoner/counter?summoner=" + URLEncoder.encode(summonerName, "UTF-8") + "®ion=" + region.toLowerCase(Locale.ROOT) + "&role=" + role.toLowerCase(Locale.ROOT) + "&level=" + requiredChampionMastery;
if (BuildConfig.DEBUG && summonerName.equalsIgnoreCase("MOCK")) {
url = "https://gist.githubusercontent.com/Neamar/eb278b4d5f188546f56028c3a0310507/raw/counters.json";
}
NoCacheRetryJsonRequest jsonRequest = new NoCacheRetryJsonRequest(Request.Method.GET, url, null, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
loadIndicator.setVisibility(View.GONE);
try {
Counters counters = new Counters(account, role, response);
adapter = new CounterChampionAdapter(counters);
recyclerView.setAdapter(adapter);
Log.i(TAG, "Loaded counters!");
Activity activity = getActivity();
if (activity != null) {
Tracker.trackViewCounters(activity, account, role, requiredChampionMastery);
}
} catch (JSONException e) {
e.printStackTrace();
}
queue.stop();
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
loadIndicator.setVisibility(View.GONE);
Log.e(TAG, error.toString());
queue.stop();
error.printStackTrace();
if (error instanceof NoConnectionError) {
if (getActivity() != null) {
displaySnack(getString(R.string.no_internet_connection));
}
}
try {
String responseBody = new String(error.networkResponse.data, "utf-8");
Log.i(TAG, responseBody);
displaySnack(responseBody);
Activity activity = getActivity();
if (activity != null) {
Tracker.trackErrorViewingCounters(activity, account, responseBody.replace("Error:", ""));
}
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (NullPointerException e) {
// Do nothing, no text content in the HTTP reply.
}
}
});
queue.add(jsonRequest);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
use of fr.neamar.lolgamedata.volley.NoCacheRetryJsonRequest in project teamward-client by Neamar.
the class SyncTokenService method sendTokenToServer.
private void sendTokenToServer(final String token, final Account account) {
// Instantiate the RequestQueue.
final RequestQueue queue = VolleyQueue.newRequestQueue(this);
String url;
try {
url = ((LolApplication) getApplication()).getApiUrl() + "/push?token=" + token + "&summoner=" + URLEncoder.encode(account.summonerName, "UTF-8") + "®ion=" + account.region;
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
return;
}
NoCacheRetryJsonRequest jsonRequest = new NoCacheRetryJsonRequest(Request.Method.GET, url, null, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
Log.i(TAG, "Token registered with server for user " + account.summonerName);
queue.stop();
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.e(TAG, error.toString());
try {
String responseBody = new String(error.networkResponse.data, "utf-8");
Log.i(TAG, responseBody);
} catch (UnsupportedEncodingException | NullPointerException e) {
e.printStackTrace();
}
}
});
queue.add(jsonRequest);
}
use of fr.neamar.lolgamedata.volley.NoCacheRetryJsonRequest in project teamward-client by Neamar.
the class AddAccountActivity method saveAccount.
private void saveAccount(final String name, final String region) {
final Account newAccount = new Account(name, region, "");
final ProgressDialog dialog = ProgressDialog.show(this, "", String.format(getString(R.string.loading_summoner_data), name), true);
dialog.show();
// Instantiate the RequestQueue.
final RequestQueue queue = VolleyQueue.newRequestQueue(this);
try {
NoCacheRetryJsonRequest jsonRequest = new NoCacheRetryJsonRequest(Request.Method.GET, ((LolApplication) getApplication()).getApiUrl() + "/summoner/data?summoner=" + URLEncoder.encode(name, "UTF-8") + "®ion=" + region, null, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try {
if (dialog.isShowing()) {
dialog.dismiss();
}
} catch (IllegalArgumentException e) {
// View is not attached (rotation for instance)
}
newAccount.summonerImage = response.optString("profileIcon", "");
newAccount.summonerName = response.optString("name", newAccount.summonerName);
Intent intent = new Intent(NEW_ACCOUNT);
intent.putExtra("account", newAccount);
setResult(RESULT_OK, intent);
AccountManager accountManager = new AccountManager(AddAccountActivity.this);
accountManager.addAccount(newAccount);
Tracker.trackAccountAdded(AddAccountActivity.this, newAccount, accountManager.getAccountIndex(newAccount));
queue.stop();
finish();
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
try {
if (dialog.isShowing()) {
dialog.dismiss();
}
} catch (IllegalArgumentException e) {
// View is not attached (rotation for instance)
}
Log.e(TAG, error.toString());
try {
String responseBody = new String(error.networkResponse.data, "utf-8");
String errorMessage = responseBody.isEmpty() ? "Unable to load player data" : responseBody;
Log.i(TAG, errorMessage);
Intent intent = new Intent();
intent.putExtra("type", NEW_ACCOUNT);
intent.putExtra("error", errorMessage);
setResult(RESULT_ERROR, intent);
Tracker.trackErrorAddingAccount(AddAccountActivity.this, newAccount, errorMessage);
} catch (UnsupportedEncodingException | NullPointerException e) {
e.printStackTrace();
}
queue.stop();
final TextView nameText = (TextView) findViewById(R.id.summonerText);
nameText.setError(getString(R.string.error_adding_account));
}
});
queue.add(jsonRequest);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
use of fr.neamar.lolgamedata.volley.NoCacheRetryJsonRequest in project teamward-client by Neamar.
the class GameActivity method loadCurrentGame.
private void loadCurrentGame(final String summonerName, final String region) {
final ProgressDialog dialog = ProgressDialog.show(this, "", String.format(getString(R.string.loading_game_data), summonerName), true);
dialog.show();
// Instantiate the RequestQueue.
final RequestQueue queue = VolleyQueue.newRequestQueue(this);
try {
String version = "unknown";
try {
PackageInfo pInfo = this.getPackageManager().getPackageInfo(getPackageName(), 0);
version = pInfo.versionName;
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
String url = ((LolApplication) getApplication()).getApiUrl() + "/game/data?summoner=" + URLEncoder.encode(summonerName, "UTF-8") + "®ion=" + region + "&version=" + version;
if (BuildConfig.DEBUG && summonerName.equalsIgnoreCase("MOCK")) {
url = "https://gist.githubusercontent.com/Neamar/eb278b4d5f188546f56028c3a0310507/raw/game.json";
}
NoCacheRetryJsonRequest jsonRequest = new NoCacheRetryJsonRequest(Request.Method.GET, url, null, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try {
game = new Game(response, region, account, shouldUseRelativeTeamColor());
GameActivity.this.summonerName = summonerName;
displayGame(summonerName, game);
Log.i(TAG, "Displaying game #" + game.gameId);
String source = getIntent() != null && getIntent().hasExtra("source") && !getIntent().getStringExtra("source").isEmpty() ? getIntent().getStringExtra("source") : "unknown";
Tracker.trackGameViewed(GameActivity.this, account, game, getDefaultTabName(), shouldDisplayChampionName(), source);
} catch (JSONException e) {
e.printStackTrace();
} finally {
try {
if (dialog.isShowing()) {
dialog.dismiss();
}
} catch (IllegalArgumentException e) {
// View is not attached (rotation for instance)
}
lastLoaded = new Date();
queue.stop();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
game = null;
lastLoaded = null;
try {
if (dialog.isShowing()) {
dialog.dismiss();
}
} catch (IllegalArgumentException e) {
// View is not attached (rotation for instance)
}
Log.e(TAG, error.toString());
queue.stop();
setUiMode(UI_MODE_NO_INTERNET);
if (error instanceof NoConnectionError) {
displaySnack(getString(R.string.no_internet_connection));
return;
}
try {
String responseBody = new String(error.networkResponse.data, "utf-8");
Log.i(TAG, responseBody);
if (!responseBody.contains("ummoner not in game")) {
displaySnack(responseBody);
Tracker.trackErrorViewingGame(GameActivity.this, account, responseBody.replace("Error:", ""));
} else {
setUiMode(UI_MODE_NOT_IN_GAME);
}
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (NullPointerException e) {
// Do nothing, no text content in the HTTP reply.
}
}
});
queue.add(jsonRequest);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
Aggregations