Search in sources :

Example 1 with NoCacheRetryJsonRequest

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);
}
Also used : Response(com.android.volley.Response) VolleyError(com.android.volley.VolleyError) JSONObject(org.json.JSONObject) RequestQueue(com.android.volley.RequestQueue) NoCacheRetryJsonRequest(fr.neamar.lolgamedata.volley.NoCacheRetryJsonRequest)

Example 2 with NoCacheRetryJsonRequest

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") + "&region=" + 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();
    }
}
Also used : LolApplication(fr.neamar.lolgamedata.LolApplication) VolleyError(com.android.volley.VolleyError) SharedPreferences(android.content.SharedPreferences) NoCacheRetryJsonRequest(fr.neamar.lolgamedata.volley.NoCacheRetryJsonRequest) CounterChampionAdapter(fr.neamar.lolgamedata.adapter.CounterChampionAdapter) SnackBarActivity(fr.neamar.lolgamedata.SnackBarActivity) Activity(android.app.Activity) JSONException(org.json.JSONException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) NoConnectionError(com.android.volley.NoConnectionError) Response(com.android.volley.Response) JSONObject(org.json.JSONObject) RequestQueue(com.android.volley.RequestQueue) Counters(fr.neamar.lolgamedata.pojo.Counters)

Example 3 with NoCacheRetryJsonRequest

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") + "&region=" + 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);
}
Also used : Response(com.android.volley.Response) VolleyError(com.android.volley.VolleyError) JSONObject(org.json.JSONObject) RequestQueue(com.android.volley.RequestQueue) NoCacheRetryJsonRequest(fr.neamar.lolgamedata.volley.NoCacheRetryJsonRequest) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Example 4 with NoCacheRetryJsonRequest

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") + "&region=" + 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();
    }
}
Also used : VolleyError(com.android.volley.VolleyError) Account(fr.neamar.lolgamedata.pojo.Account) NoCacheRetryJsonRequest(fr.neamar.lolgamedata.volley.NoCacheRetryJsonRequest) UnsupportedEncodingException(java.io.UnsupportedEncodingException) Intent(android.content.Intent) ProgressDialog(android.app.ProgressDialog) Response(com.android.volley.Response) JSONObject(org.json.JSONObject) RequestQueue(com.android.volley.RequestQueue) TextView(android.widget.TextView)

Example 5 with NoCacheRetryJsonRequest

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") + "&region=" + 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();
    }
}
Also used : VolleyError(com.android.volley.VolleyError) PackageInfo(android.content.pm.PackageInfo) NoCacheRetryJsonRequest(fr.neamar.lolgamedata.volley.NoCacheRetryJsonRequest) JSONException(org.json.JSONException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) NoConnectionError(com.android.volley.NoConnectionError) ProgressDialog(android.app.ProgressDialog) Date(java.util.Date) Response(com.android.volley.Response) Game(fr.neamar.lolgamedata.pojo.Game) PackageManager(android.content.pm.PackageManager) JSONObject(org.json.JSONObject) RequestQueue(com.android.volley.RequestQueue)

Aggregations

RequestQueue (com.android.volley.RequestQueue)6 Response (com.android.volley.Response)6 VolleyError (com.android.volley.VolleyError)6 NoCacheRetryJsonRequest (fr.neamar.lolgamedata.volley.NoCacheRetryJsonRequest)6 JSONObject (org.json.JSONObject)6 UnsupportedEncodingException (java.io.UnsupportedEncodingException)5 NoConnectionError (com.android.volley.NoConnectionError)3 ProgressDialog (android.app.ProgressDialog)2 JSONException (org.json.JSONException)2 Activity (android.app.Activity)1 Intent (android.content.Intent)1 SharedPreferences (android.content.SharedPreferences)1 PackageInfo (android.content.pm.PackageInfo)1 PackageManager (android.content.pm.PackageManager)1 TextView (android.widget.TextView)1 LolApplication (fr.neamar.lolgamedata.LolApplication)1 SnackBarActivity (fr.neamar.lolgamedata.SnackBarActivity)1 CounterChampionAdapter (fr.neamar.lolgamedata.adapter.CounterChampionAdapter)1 Account (fr.neamar.lolgamedata.pojo.Account)1 AggregatedPerformance (fr.neamar.lolgamedata.pojo.AggregatedPerformance)1